Search code examples
memory-leaksprotractorperformance-testing

What is the most efficient way to track browser memory consumed by the execution of a Protractor test?


The idea is to:

  1. Measure usedJSHeapSize before starting the test.
  2. Measure usedJSHeapSize after completing the test.
  3. Comparing values from 1 and 2 and if the size increases above a defined threshold, then fail the scenario.

So far I have tried:

Any other ideas?


Solution

  • This can be done by invoking browser.executeScript()

    Use window.performance.memory for Chrome to fetch the performance parameters The below code worked all good for me.

    https://docs.webplatform.org/wiki/apis/timing/properties/memory

    it('Dummy Test', function(){
        //Fetch the browser memory parameters before execution
        browser.executeScript('return window.performance.memory').then(function(memoryInfo){
            console.log(memoryInfo)
            var beforejsHeapSizeLimit = memoryInfo.jsHeapSizeLimit;
            var beforeusedJSHeapSize = memoryInfo.usedJSHeapSize;
            var beforetotalJSHeapSize = memoryInfo.totalJSHeapSize;
    
            // Have all your code to open browser .. navigate pages etc
            browser.driver.get("https://wordpress.com/");
            browser.driver.get("http://www.adobe.com/software/flash/about/");
            // Once you are done compare before and after values
    
            //Fetch the browser memory parameters after execution and compare
            browser.executeScript('return window.performance.memory').then(function(aftermemoryInfo) {
                console.log(aftermemoryInfo)
                var afterjsHeapSizeLimit = aftermemoryInfo.jsHeapSizeLimit;
                var afterusedJSHeapSize = aftermemoryInfo.usedJSHeapSize;
                var aftertotalJSHeapSize = aftermemoryInfo.totalJSHeapSize;
                expect((parseInt(afterusedJSHeapSize)-parseInt(beforeusedJSHeapSize))<10000000).toBe.true;
            });
        });
    
    });