diff --git a/docs/api.md b/docs/api.md index 2e7e35965f58f..9862a0f704a25 100644 --- a/docs/api.md +++ b/docs/api.md @@ -138,6 +138,7 @@ * [page.coverage](#pagecoverage) * [page.deleteCookie(...cookies)](#pagedeletecookiecookies) * [page.emulate(options)](#pageemulateoptions) + * [page.emulateCPUThrottling(factor)](#pageemulatecputhrottlingfactor) * [page.emulateIdleState(overrides)](#pageemulateidlestateoverrides) * [page.emulateMediaFeatures(features)](#pageemulatemediafeaturesfeatures) * [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). +#### page.emulateCPUThrottling(factor) + +- `factor` 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) - `overrides` If not set, clears emulation diff --git a/src/common/Page.ts b/src/common/Page.ts index eb2e4d4c90b06..c5b696089fd90 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -1587,6 +1587,16 @@ export class Page extends EventEmitter { }); } + async emulateCPUThrottling(factor: number | null): Promise { + 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 - `>` Given an array of media feature * objects, emulates CSS media features on the page. Each media feature object diff --git a/test/emulation.spec.ts b/test/emulation.spec.ts index eb6f1b957ca84..ded262a77c170 100644 --- a/test/emulation.spec.ts +++ b/test/emulation.spec.ts @@ -408,4 +408,13 @@ describe('Emulation', () => { 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); + }); + }); });