Execution ~300x Slower when page backgrounding take a place (headful mode)
See original GitHub issue- Puppeteer version: 1.19.0
- Platform / OS version: Windows 10
- Node.js version: doesn’t matter
Here is a simple test which shows the problem. In the test we measure the time of the mouse movement, when the page is visible execution of the test is almost instant, but as soon as we minimize chrome, everything gets incredibly slow. But actually the problem is not in minimization, it is just the fastest way to reproduce the issue, in production I noticed the same behavior even when the page is maximized but not visible because another maximized windows is on top, also the same behavior detected when we open new tab in chrome and make it active. Even more when the page goes to this ‘slow’ mode, some js function just stops to work, for example IntersectionObserver callback never called in this mode (https://github.com/GoogleChrome/puppeteer/issues/3156#issuecomment-522600282). This object is used by a puppeteer in click function, and as result Click doesn’t work.
In the test, I used mouse movement from (1,1) to (100,100) and back x25 times. I tried to change the distance from (1,1) to (10,10) but the execution time is not changed it doesn’t depend on distance. But as far I removed one of two calls of mouse movent inside cycle and changed it to move to i, execution time reduced twice:
for(var i=0;i<25;i++){
await page.mouse.move(1, 1);
await page.mouse.move(100, 100);
}
changed to =>>>>
for(var i=0;i<25;i++){
await page.mouse.move(1, i);
}
Also I recorder timeline of page buy using provided api ‘page.tracing.start’ and here is files: trace_ok.json.log trace_slow.json.log Files can be viewed via https://chromedevtools.github.io/timeline-viewer/
As we can see events executes only every 5 seconds. We should have a way to disable this behavior in chrome and do not throttle execution. I tried to use these command-line switches but no result: “–disable-background-timer-throttling”, “–disable-backgrounding-occluded-windows”
Code to reproduce issue:
const puppeteer = require('puppeteer');
(async () => {
var baseTime=Date.now();
const browser = await puppeteer.launch({
headless: false,
// executablePath :"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
});
const page = await browser.newPage();
await page.goto('https://github.com/',{waitUntil:'load'});
console.log('Start test#1');
await page.tracing.start({path: 'trace_ok.json'});
var hrstart = process.hrtime()
for(var i=0;i<25;i++){
await page.mouse.move(1, 1);
await page.mouse.move(100, 100);
}
await page.tracing.stop();
hrend = process.hrtime(hrstart)
console.info('Execution time normal mode: %ds %dms', hrend[0], hrend[1] / 1000000)
console.log('Please minimize chrome now');
process.stdout.write('\x07');
console.log('Wait for user to minimize chrome');
await sleep(5000);
console.log('Start test#2');
hrstart = process.hrtime()
await page.tracing.start({path: 'trace_slow.json'});
for(var i=0;i<25;i++){
await page.mouse.move(1, 1);
await page.mouse.move(100, 100);
}
await page.tracing.stop();
hrend = process.hrtime(hrstart)
console.info('Execution time throttled mode: %ds %dms', hrend[0], hrend[1] / 1000000)
function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}
})();
output:
Start test#1 Execution time normal mode: 0s 823.829ms Please minimize chrome now Wait for user to minimize chrome Start test#2 Execution time throttled mode: 250s 121.6708ms
What is the expected result? Execution time should be similar whenever the application is minimized or in a normal state when devtools is active.
What happens instead? When page window is minimized, execution performance 1000x slower, and some js features doesn’t work.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Top GitHub Comments
Update: Quick and dirty fix for this bug is to use Devtools protocol and to send 2 next commands: DOM.enable Overlay.enable
@Gdocal Thanks for your dirty fix! Have you (in the meantime) found another way/fix? It’s working, but it really feels dirty.
Edit: I struggle with the exact same issue.