From 27bc48ef685890f7c70d943734226400531f04ae Mon Sep 17 00:00:00 2001 From: Olivier Gorzalka Date: Tue, 24 Nov 2026 16:48:26 +0100 Subject: [PATCH] test(e2e): record how fast the machine was next to the nightly performance metrics --- tests/e2e/fixtures/perf/calibration.php | 50 +++++++++++++++++++++++++ tests/e2e/specs/perf-metrics.spec.ts | 25 ++++++++++++- tests/e2e/support/perf.test.ts | 38 +++++++++++++++++++ tests/e2e/support/perf.ts | 36 ++++++++++++++++-- 4 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 tests/e2e/fixtures/perf/calibration.php diff --git a/tests/e2e/fixtures/perf/calibration.php b/tests/e2e/fixtures/perf/calibration.php new file mode 100644 index 00000000..ae615bfc --- /dev/null +++ b/tests/e2e/fixtures/perf/calibration.php @@ -0,0 +1,50 @@ + round($cpu, 1), 'fs_ref_ms' => round($disk, 1)], JSON_THROW_ON_ERROR); diff --git a/tests/e2e/specs/perf-metrics.spec.ts b/tests/e2e/specs/perf-metrics.spec.ts index d83cd923..c9484cd5 100644 --- a/tests/e2e/specs/perf-metrics.spec.ts +++ b/tests/e2e/specs/perf-metrics.spec.ts @@ -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' }); @@ -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. */ @@ -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); +}); diff --git a/tests/e2e/support/perf.test.ts b/tests/e2e/support/perf.test.ts index d74b1041..1279e4a1 100644 --- a/tests/e2e/support/perf.test.ts +++ b/tests/e2e/support/perf.test.ts @@ -2,6 +2,7 @@ import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import { compareMetrics, + describeCalibration, describeComparison, judgeRun, type Diagnostics, @@ -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, calibration?: Record) => ({ 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')); + }); +}); diff --git a/tests/e2e/support/perf.ts b/tests/e2e/support/perf.ts index 30db71ef..c05424be 100644 --- a/tests/e2e/support/perf.ts +++ b/tests/e2e/support/perf.ts @@ -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; -/** 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 }; @@ -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[] }; @@ -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 }; }