feat: add page.emulateCPUThrottling (#7343)

This commit is contained in:
Jan Scheffler 2021-06-22 13:43:11 +02:00 committed by GitHub
parent 7e74a9d606
commit 4ce4110628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 0 deletions

View File

@ -138,6 +138,7 @@
* [page.coverage](#pagecoverage) * [page.coverage](#pagecoverage)
* [page.deleteCookie(...cookies)](#pagedeletecookiecookies) * [page.deleteCookie(...cookies)](#pagedeletecookiecookies)
* [page.emulate(options)](#pageemulateoptions) * [page.emulate(options)](#pageemulateoptions)
* [page.emulateCPUThrottling(factor)](#pageemulatecputhrottlingfactor)
* [page.emulateIdleState(overrides)](#pageemulateidlestateoverrides) * [page.emulateIdleState(overrides)](#pageemulateidlestateoverrides)
* [page.emulateMediaFeatures(features)](#pageemulatemediafeaturesfeatures) * [page.emulateMediaFeatures(features)](#pageemulatemediafeaturesfeatures)
* [page.emulateMediaType(type)](#pageemulatemediatypetype) * [page.emulateMediaType(type)](#pageemulatemediatypetype)
@ -1535,6 +1536,27 @@ const iPhone = puppeteer.devices['iPhone 6'];
List of all available devices is available in the source code: [src/common/DeviceDescriptors.ts](https://github.com/puppeteer/puppeteer/blob/main/src/common/DeviceDescriptors.ts). List of all available devices is available in the source code: [src/common/DeviceDescriptors.ts](https://github.com/puppeteer/puppeteer/blob/main/src/common/DeviceDescriptors.ts).
#### page.emulateCPUThrottling(factor)
- `factor` <?[number]> Factor at which the CPU will be throttled (2x, 2.5x. 3x, ...). Passing `null` disables cpu throttling.
- returns: <[Promise]>
> **NOTE** Real device CPU performance is impacted by many factors that are not trivial to emulate via the Chrome DevTools Protocol / Puppeteer. e.g core count, L1/L2 cache, thermal throttling impacting performance, architecture etc. Simulating CPU performance can be a good guideline, but ideally also verify any numbers you see on a real mobile device.
```js
const puppeteer = require('puppeteer');
const slow3G = puppeteer.networkConditions['Slow 3G'];
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulateCPUThrottling(2);
await page.goto('https://www.google.com');
// other actions...
await browser.close();
})();
```
#### page.emulateIdleState(overrides) #### page.emulateIdleState(overrides)
- `overrides` <?[Object]> If not set, clears emulation - `overrides` <?[Object]> If not set, clears emulation

View File

@ -1587,6 +1587,16 @@ export class Page extends EventEmitter {
}); });
} }
async emulateCPUThrottling(factor: number | null): Promise<void> {
assert(
factor === null || factor >= 1,
'Throttling rate should be greater or equal to 1'
);
await this._client.send('Emulation.setCPUThrottlingRate', {
rate: factor !== null ? factor : 1,
});
}
/** /**
* @param features - `<?Array<Object>>` Given an array of media feature * @param features - `<?Array<Object>>` Given an array of media feature
* objects, emulates CSS media features on the page. Each media feature object * objects, emulates CSS media features on the page. Each media feature object

View File

@ -408,4 +408,13 @@ describe('Emulation', () => {
await page.emulateNetworkConditions(null); await page.emulateNetworkConditions(null);
}); });
}); });
describeFailsFirefox('Page.emulateCPUThrottling', function () {
it('should change the CPU throttling rate successfully', async () => {
const { page } = getTestState();
await page.emulateCPUThrottling(100);
await page.emulateCPUThrottling(null);
});
});
}); });