Skip to content
Merged
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
50 changes: 50 additions & 0 deletions tests/e2e/fixtures/perf/calibration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* Time two fixed pieces of work that do not depend on the framework, run through `wp eval`.
*
* The nightly timings (boot, thumbnail) move with the machine the job lands on: the same commit
* measured 40 ms one night and 73 ms the next. These two numbers say how fast the machine was:
* `cpu_ref_ms` is a chain of hashes (processor), `fs_ref_ms` writes and reads a few hundred small
* files (disk and file system). They are recorded with the metrics and printed by the comparison,
* never judged: they exist to learn whether the timings can be told apart from the machine.
*/
$start = hrtime(true);
$digest = '';

for ($i = 0; $i < 250_000; $i++) {
$digest = hash('sha256', $digest.$i);
}

$cpu = (hrtime(true) - $start) / 1_000_000;

$directory = sys_get_temp_dir().'/e2e-perf-calibration-'.bin2hex(random_bytes(4));
mkdir($directory);

$start = hrtime(true);

for ($i = 0; $i < 400; $i++) {
file_put_contents($directory.'/'.$i.'.txt', str_repeat('x', 2048));
}

$read = 0;

for ($i = 0; $i < 400; $i++) {
$read += strlen((string) file_get_contents($directory.'/'.$i.'.txt'));
}

$disk = (hrtime(true) - $start) / 1_000_000;

for ($i = 0; $i < 400; $i++) {
unlink($directory.'/'.$i.'.txt');
}

rmdir($directory);

if ($read !== 400 * 2048) {
throw new RuntimeException('The calibration read back less than it wrote: the measure would be of nothing.');
}

echo json_encode(['cpu_ref_ms' => round($cpu, 1), 'fs_ref_ms' => round($disk, 1)], JSON_THROW_ON_ERROR);
25 changes: 24 additions & 1 deletion tests/e2e/specs/perf-metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ import { artisan, homeUrl, runId, wp } from '../support/site';
* boot_ms time from the start of the request to wp_loaded, warm (REST index)
* cold_boot_ms the same, on the first request after `discovery:clear`
* thumbnail_ms wp_generate_attachment_metadata() for one 2400x1600 JPEG, every size
*
* Next to them the report carries `calibration`: how fast this machine ran two fixed pieces of work
* (cpu_ref_ms, fs_ref_ms). The shared runners differ by up to a factor of two from one night to the
* next, which the comparison cannot tell from a regression; these are recorded to find out how the
* timings follow the machine. They are printed, never judged.
*/
const out = process.env.E2E_PERF_OUT ?? './test-results/perf-metrics.json';
const warmRuns = 7;
const coldRuns = 3;
const thumbnailRuns = 3;
const calibrationRuns = 5;
const metrics: Metrics = {};
const calibration: Metrics = {};

test.skip(process.env.E2E_PERF !== '1', 'set E2E_PERF=1 to measure');
test.describe.configure({ mode: 'serial' });
Expand All @@ -43,7 +50,7 @@ test.afterAll(() => {
}

mkdirSync(join(out, '..'), { recursive: true });
writeFileSync(out, `${JSON.stringify({ measuredAt: new Date().toISOString(), run: runId, metrics }, null, 2)}\n`);
writeFileSync(out, `${JSON.stringify({ measuredAt: new Date().toISOString(), run: runId, metrics, calibration }, null, 2)}\n`);
});

/** One REST index request, and the boot time the framework reported for it. */
Expand Down Expand Up @@ -96,3 +103,19 @@ test('thumbnail generation', () => {

metrics.thumbnail_ms = median(samples);
});

test('calibration', () => {
const script = readFileSync(join(import.meta.dirname, '..', 'fixtures', 'perf', 'calibration.php'), 'utf8').replace(/^<\?php\s*/, '');
const cpu: number[] = [];
const disk: number[] = [];

for (let run = 0; run < calibrationRuns; run++) {
const result = JSON.parse(wp('eval', script)) as { cpu_ref_ms: number; fs_ref_ms: number };

cpu.push(result.cpu_ref_ms);
disk.push(result.fs_ref_ms);
}

calibration.cpu_ref_ms = median(cpu);
calibration.fs_ref_ms = median(disk);
});
38 changes: 38 additions & 0 deletions tests/e2e/support/perf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import {
compareMetrics,
describeCalibration,
describeComparison,
judgeRun,
type Diagnostics,
Expand Down Expand Up @@ -189,3 +190,40 @@ describe('judgeRun', () => {
assert.equal(judgeRun(run({ boot_ms: 109 }), run({ boot_ms: 100 })).failed, false);
});
});

describe('describeCalibration', () => {
it('says nothing when the run has no calibration', () => {
assert.deepEqual(describeCalibration(undefined, { cpu_ref_ms: 50 }), []);
});

it("prints this night's numbers alone when the reference night has none", () => {
assert.deepEqual(describeCalibration({ cpu_ref_ms: 60 }, undefined), ['calibration cpu_ref_ms: 60 ms']);
assert.deepEqual(describeCalibration({ cpu_ref_ms: 60, fs_ref_ms: 9 }, { cpu_ref_ms: 0 }), ['calibration cpu_ref_ms: 60 ms', 'calibration fs_ref_ms: 9 ms']);
});

it('compares each number with the reference night', () => {
assert.deepEqual(describeCalibration({ cpu_ref_ms: 75, fs_ref_ms: 8 }, { cpu_ref_ms: 50, fs_ref_ms: 10 }), [
'calibration cpu_ref_ms: 75 ms (reference night 50 ms, +50.0 %)',
'calibration fs_ref_ms: 8 ms (reference night 10 ms, -20.0 %)',
]);
});
});

describe('judgeRun and the calibration', () => {
const report = (metrics: Record<string, number>, calibration?: Record<string, number>) => ({ measuredAt: '2026-11-18T03:30:00Z', run: '1', metrics, calibration });

it('never fails on a calibration that moved: the machine is not the code', () => {
const verdict = judgeRun(report({ boot_ms: 100 }, { cpu_ref_ms: 200 }), report({ boot_ms: 100 }, { cpu_ref_ms: 50 }));

assert.equal(verdict.failed, false);
assert.ok(verdict.lines.includes('calibration cpu_ref_ms: 200 ms (reference night 50 ms, +300.0 %)'));
});

it('still fails on a regression, calibration or not', () => {
assert.equal(judgeRun(report({ boot_ms: 150 }, { cpu_ref_ms: 50 }), report({ boot_ms: 100 })).failed, true);
});

it('prints the calibration on the first night too', () => {
assert.ok(judgeRun(report({ boot_ms: 100 }, { cpu_ref_ms: 50 }), null).lines.includes('calibration cpu_ref_ms: 50 ms'));
});
});
36 changes: 32 additions & 4 deletions tests/e2e/support/perf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,14 @@ export function median(values: number[]): number {
/** The nightly measures, in milliseconds. Each is the median of several runs. */
export type Metrics = Record<string, number>;

/** What one nightly run writes: the metrics and what they were measured on. */
export type MetricsReport = { measuredAt: string; run: string; metrics: Metrics };
/**
* What one nightly run writes: the metrics, and how fast the machine was.
*
* `calibration` holds fixed pieces of work that do not depend on the framework (see
* fixtures/perf/calibration.php). It is printed next to the verdict and never judged: the
* reports of the nights before it existed have none.
*/
export type MetricsReport = { measuredAt: string; run: string; metrics: Metrics; calibration?: Metrics };

export type MetricFinding = { metric: string; baseline: number; current: number; change: number };

Expand Down Expand Up @@ -226,6 +232,28 @@ export function describeComparison(comparison: Comparison, tolerance = 0.1): str
return lines;
}

/**
* The calibration as lines for the CI log: this night's, and the reference night's when it has one.
* Information only: the machine is not the code, so it never fails a run.
*/
export function describeCalibration(current: Metrics | undefined, baseline: Metrics | undefined): string[] {
if (current === undefined) {
return [];
}

return Object.entries(current).map(([name, value]) => {
const before = baseline?.[name];

if (before === undefined || before <= 0) {
return `calibration ${name}: ${value} ms`;
}

const change = (value - before) / before;

return `calibration ${name}: ${value} ms (reference night ${before} ms, ${change >= 0 ? '+' : ''}${(change * 100).toFixed(1)} %)`;
});
}

/** The verdict of a nightly run: what to print and whether the job fails. */
export type Verdict = { failed: boolean; lines: string[] };

Expand All @@ -238,11 +266,11 @@ export type Verdict = { failed: boolean; lines: string[] };
*/
export function judgeRun(current: MetricsReport, baseline: MetricsReport | null, tolerance = 0.1): Verdict {
if (baseline === null) {
return { failed: false, lines: ['No baseline yet: this run becomes the reference.', ...Object.entries(current.metrics).map(([metric, value]) => `${metric}: ${value} ms`)] };
return { failed: false, lines: ['No baseline yet: this run becomes the reference.', ...Object.entries(current.metrics).map(([metric, value]) => `${metric}: ${value} ms`), ...describeCalibration(current.calibration, undefined)] };
}

const comparison = compareMetrics(current.metrics, baseline.metrics, tolerance);
const lines = [`Baseline of ${baseline.measuredAt} (run ${baseline.run})`, ...describeComparison(comparison, tolerance)];
const lines = [`Baseline of ${baseline.measuredAt} (run ${baseline.run})`, ...describeComparison(comparison, tolerance), ...describeCalibration(current.calibration, baseline.calibration)];

return { failed: comparison.regressions.length > 0 || comparison.missing.length > 0, lines };
}
Loading