Skip to content

Commit 5704cf2

Browse files
committed
Add CPU throttling option
1 parent a369601 commit 5704cf2

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ See [action.yml](action.yml)
101101
#
102102
# Default: 'latest'
103103
php-version: ''
104-
104+
105+
# CPU throttling rate to apply in Chromium.
106+
# For example, 4 simulates a 4x CPU slowdown.
107+
#
108+
# Default: ''
109+
cpu-throttling-rate: ''
110+
105111
# Number of times the tests should be repeated.
106112
#
107113
# Default: 2

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ inputs:
4848
required: false
4949
description: 'PHP version to use.'
5050
default: '8.2'
51+
cpu-throttling-rate:
52+
required: false
53+
description: 'CPU throttling rate to apply in Chromium.'
54+
default: ''
5155
shard:
5256
required: false
5357
description: 'Shard to use if running tests in parallel in a matrix.'
@@ -204,6 +208,7 @@ runs:
204208
SHARD: ${{ inputs.shard != '' && inputs.shard || '' }}
205209
ADDITIONAL_ARGS: ${{ inputs.shard != '' && format('-- --shard={0}', inputs.shard) || '' }}
206210
URLS_TO_TEST: ${{ inputs.urls }}
211+
CPU_THROTTLING_RATE: ${{ inputs.cpu-throttling-rate }}
207212
DEBUG: ${{ inputs.debug == 'true' }}
208213
TEST_ITERATIONS: ${{ inputs.iterations }}
209214
TEST_REPETITIONS: ${{ inputs.repetitions }}

env/tests/performance/specs/main.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Page } from '@playwright/test';
12
import { test } from '@wordpress/e2e-test-utils-playwright';
23
import { camelCaseDashes } from '../utils';
34

@@ -32,13 +33,20 @@ test.describe( 'Tests', () => {
3233
.filter( Boolean );
3334

3435
const iterations = Number( process.env.TEST_ITERATIONS );
36+
const cpuThrottlingRate = getCpuThrottlingRate();
3537

3638
for ( const url of urlsToTest ) {
3739
for ( let i = 1; i <= iterations; i++ ) {
3840
test( `URL: "${ url }" (${ i } of ${ iterations })`, async ( {
41+
browserName,
3942
page,
4043
metrics,
4144
} ) => {
45+
await applyCpuThrottling(
46+
page,
47+
browserName,
48+
cpuThrottlingRate
49+
);
4250
await page.goto( `${ url.replace( /\/$/, '' ) }/?i=${ i }` );
4351

4452
const serverTiming = await metrics.getServerTiming();
@@ -63,3 +71,32 @@ test.describe( 'Tests', () => {
6371
}
6472
}
6573
} );
74+
75+
function getCpuThrottlingRate() {
76+
const rate = ( process.env.CPU_THROTTLING_RATE || '' ).trim();
77+
78+
if ( rate === '' ) {
79+
return 0;
80+
}
81+
82+
const parsedRate = Number( rate );
83+
84+
if ( ! Number.isFinite( parsedRate ) || parsedRate <= 0 ) {
85+
throw new Error( 'CPU_THROTTLING_RATE must be a positive number.' );
86+
}
87+
88+
return parsedRate;
89+
}
90+
91+
async function applyCpuThrottling(
92+
page: Page,
93+
browserName: string,
94+
rate: number
95+
) {
96+
if ( rate === 0 || browserName !== 'chromium' ) {
97+
return;
98+
}
99+
100+
const session = await page.context().newCDPSession( page );
101+
await session.send( 'Emulation.setCPUThrottlingRate', { rate } );
102+
}

0 commit comments

Comments
 (0)