Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down Expand Up @@ -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 }}
Expand Down
37 changes: 37 additions & 0 deletions env/tests/performance/specs/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Page } from '@playwright/test';
import { test } from '@wordpress/e2e-test-utils-playwright';
import { camelCaseDashes } from '../utils';

Expand Down Expand Up @@ -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();
Expand All @@ -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.' );
}
Comment on lines +84 to +86
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The validation logic is inconsistent: an empty string returns 0 (disabling throttling), but an explicit "0" string causes an error. Additionally, the error message 'positive number' technically excludes zero. Allowing 0 as a valid input for 'no throttling' would be more consistent.

Suggested change
if ( ! Number.isFinite( parsedRate ) || parsedRate <= 0 ) {
throw new Error( 'CPU_THROTTLING_RATE must be a positive number.' );
}
if ( ! Number.isFinite( parsedRate ) || parsedRate < 0 ) {
throw new Error( 'CPU_THROTTLING_RATE must be a non-negative number.' );
}


return parsedRate;
}

async function applyCpuThrottling(
page: Page,
browserName: string,
rate: number
) {
if ( rate === 0 || browserName !== 'chromium' ) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

A CPU throttling rate of 1 represents no slowdown. Since Playwright provides a fresh page for each test iteration, we can optimize by skipping the CDP session creation and command if the rate is 1 or less.

Suggested change
if ( rate === 0 || browserName !== 'chromium' ) {
if ( rate <= 1 || browserName !== 'chromium' ) {

return;
}

const session = await page.context().newCDPSession( page );
await session.send( 'Emulation.setCPUThrottlingRate', { rate } );
}
Loading