diff --git a/README.md b/README.md index c549c5f..b705fea 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,13 @@ See [action.yml](action.yml) # # Default: 'latest' php-version: '' - + + # CPU throttling rate to apply in Chromium. + # For example, 4 simulates a 4x CPU slowdown. + # + # Default: '' + cpu-throttling-rate: '' + # Number of times the tests should be repeated. # # Default: 2 diff --git a/action.yml b/action.yml index bf5e4c0..120c79a 100644 --- a/action.yml +++ b/action.yml @@ -48,6 +48,10 @@ inputs: required: false description: 'PHP version to use.' default: '8.2' + cpu-throttling-rate: + required: false + description: 'CPU throttling rate to apply in Chromium.' + default: '' shard: required: false description: 'Shard to use if running tests in parallel in a matrix.' @@ -204,6 +208,7 @@ runs: SHARD: ${{ inputs.shard != '' && inputs.shard || '' }} ADDITIONAL_ARGS: ${{ inputs.shard != '' && format('-- --shard={0}', inputs.shard) || '' }} URLS_TO_TEST: ${{ inputs.urls }} + CPU_THROTTLING_RATE: ${{ inputs.cpu-throttling-rate }} DEBUG: ${{ inputs.debug == 'true' }} TEST_ITERATIONS: ${{ inputs.iterations }} TEST_REPETITIONS: ${{ inputs.repetitions }} diff --git a/env/tests/performance/specs/main.spec.ts b/env/tests/performance/specs/main.spec.ts index 3996e75..c7d44b8 100644 --- a/env/tests/performance/specs/main.spec.ts +++ b/env/tests/performance/specs/main.spec.ts @@ -1,3 +1,4 @@ +import type { Page } from '@playwright/test'; import { test } from '@wordpress/e2e-test-utils-playwright'; import { camelCaseDashes } from '../utils'; @@ -32,13 +33,20 @@ test.describe( 'Tests', () => { .filter( Boolean ); const iterations = Number( process.env.TEST_ITERATIONS ); + const cpuThrottlingRate = getCpuThrottlingRate(); for ( const url of urlsToTest ) { for ( let i = 1; i <= iterations; i++ ) { test( `URL: "${ url }" (${ i } of ${ iterations })`, async ( { + browserName, page, metrics, } ) => { + await applyCpuThrottling( + page, + browserName, + cpuThrottlingRate + ); await page.goto( `${ url.replace( /\/$/, '' ) }/?i=${ i }` ); const serverTiming = await metrics.getServerTiming(); @@ -63,3 +71,32 @@ test.describe( 'Tests', () => { } } } ); + +function getCpuThrottlingRate() { + const rate = ( process.env.CPU_THROTTLING_RATE || '' ).trim(); + + if ( rate === '' ) { + return 0; + } + + const parsedRate = Number( rate ); + + if ( ! Number.isFinite( parsedRate ) || parsedRate <= 0 ) { + throw new Error( 'CPU_THROTTLING_RATE must be a positive number.' ); + } + + return parsedRate; +} + +async function applyCpuThrottling( + page: Page, + browserName: string, + rate: number +) { + if ( rate === 0 || browserName !== 'chromium' ) { + return; + } + + const session = await page.context().newCDPSession( page ); + await session.send( 'Emulation.setCPUThrottlingRate', { rate } ); +}