@ogygia/testing
CI reports
Every report a test takes is attached as ogygia.json, and compare() and toMarkdown() turn two CI runs of the same tests, a base branch and a head branch, into a PR comment that says what moved.
On this page
A budget catches a page that got too slow. It doesn’t tell you that a pull request made a page slower while it stayed under the budget, or that it fixed a finding. For that you compare two runs of the same tests: the base branch and the head branch. The fixture keeps every report a test takes, and two helpers turn two runs of them into a comment for the pull request.
import { readFileSync } from 'node:fs';
import {
compare,
toMarkdown,
type CompareInput
} from '@ogygia/testing';
// each file: the parsed `ogygia.json` attachments of one run, gathered as below
const base = JSON.parse(
readFileSync('base/ogygia-run.json', 'utf8')
) as CompareInput[];
const head = JSON.parse(
readFileSync('head/ogygia-run.json', 'utf8')
) as CompareInput[];
console.log(toMarkdown(compare(base, head)));Attachments
After each test that took a report, the fixture attaches ogygia.json to the test’s result: { test, status, reports }, where test is the test’s title path joined with ›, status its Playwright status, and reports every report in the order the test took them. Playwright’s HTML reporter shows it, and its JSON reporter carries it.
A full profile is hundreds of KB, so the attachment keeps a slim copy of each profile: its id, target, score, forecast, findings, asset totals and files, links, a server window’s window, and its ten hottest functions. A session’s server part is slimmed the same way. The other reports go in whole. forAttachment(report) returns that same form, for a harness that writes its own attachments; set attach: false in ogygiaOptions to turn the fixture’s off. ogygia.reports holds the full reports during the test.
With Playwright’s JSON reporter, each attachment’s body is base64 in the report. This script gathers one run’s attachments into the array compare takes:
Run the suite once on each branch with --reporter=json, gather both, and post what toMarkdown prints:
import { readFileSync, writeFileSync } from 'node:fs';
import type {
JSONReport,
JSONReportSuite
} from '@playwright/test/reporter';
import type { CompareInput } from '@ogygia/testing';
const report = JSON.parse(
readFileSync(process.argv[2], 'utf8')
) as JSONReport;
const out: CompareInput[] = [];
function walk(suite: JSONReportSuite): void {
for (const spec of suite.specs)
for (const t of spec.tests)
for (const result of t.results)
for (const a of result.attachments)
if (a.name === 'ogygia.json' && a.body)
out.push(
JSON.parse(
Buffer.from(a.body, 'base64').toString('utf8')
) as CompareInput
);
for (const child of suite.suites ?? []) walk(child);
}
for (const suite of report.suites) walk(suite);
writeFileSync(process.argv[3], JSON.stringify(out));PLAYWRIGHT_JSON_OUTPUT_NAME=results.json pnpm playwright test --reporter=json
node scripts/gather.ts results.json head/ogygia-run.jsoncompare
compare(baseRun, headRun, options?) takes the two runs as arrays of { test, reports } (the attachment bodies) and returns one RunDiff per report of each head test. Tests are matched by test, and within a test each report by its kind and position: the second page() of a test against the base’s second page(). A test only the head has gets a diff with its findings as added and nothing else. A test only the base has is left out.
For each report, the diff carries:
findings:addedandfixed, by code, each ascode: message.score,renderMs,js(profiles): the score, the median server render and the JS at start, base and head.js.changesnames the files behind a move, known by what they hold since hashed names change every build:new,grew,shrank,gone,now at startandnow loads later.hydration: each island instance that stopped or started hydrating cleanly, asCarousel#2: clean → failed. Reports without instance keys (an engine before 2) compare as a count per name and status, such asClock: changed ×2.session: the counts that moved, amongerrors,shifts,slow,navigationsandcls. A session’s CLS is skipped when either side’s engine has nocounts.cls.vitals: only those that moved by more than a run’s noise (Noise).
navigation-blocked findings are left out of findings, since navigations already counts them.
import { readFileSync } from 'node:fs';
import { compare, type CompareInput } from '@ogygia/testing';
const read = (file: string) =>
JSON.parse(readFileSync(file, 'utf8')) as CompareInput[];
const diffs = compare(
read('base/ogygia-run.json'),
read('head/ogygia-run.json')
);
const broke = diffs.filter(
(d) => d.findings.added.length || d.hydration?.broke.length
);
if (broke.length) {
console.error(broke.map((d) => d.test).join('\n'));
process.exit(1);
}Noise
A vital counts as moved only when the medians differ by at least its noise. For timings, the move must also be at least 10 % of the base value. The defaults:
| Vital | Noise |
|---|---|
lcp | 150 ms |
fcp | 150 ms |
ttfb | 100 ms |
inp | 50 ms |
cls | 0.02 |
Pass your own as compare(baseRun, headRun, { noise: { lcp: 300 } }). A session’s CLS uses the cls noise too.
Several runs
Real pages are noisy, and comparing one run against one run turns that noise into false regressions, because a scripted session’s errors, shifts and slow interactions depend on the path it happened to take. Give a test several runs a side as { test, runs: [reports, reports, reports] }, and compare treats the signals differently:
- Load signals (page vitals and findings, hydration, and a profile’s score, render time and JS) are compared on the median of the runs. A finding or a broken island counts when most runs have it, and each island instance takes its most common status.
- Session signals (a session’s findings, errors, shifts, slow interactions and blocked navigations) count only when every head run has them and no base run does. A count moves only when every head run is above every base run, or every head run is below.
One run a side is the same rules on a single sample. Three runs a side is a good start for the pages whose first pass shows a possible regression. Playwright’s --repeat-each=3 gives you three results per test; group their attachments by test.
import type {
CompareInput,
OgygiaReport
} from '@ogygia/testing';
type Attachment = { test: string; reports: OgygiaReport[] };
export function groupRuns(
attachments: Attachment[]
): CompareInput[] {
const byTest = new Map<string, OgygiaReport[][]>();
for (const a of attachments)
byTest.set(a.test, [
...(byTest.get(a.test) ?? []),
a.reports
]);
return [...byTest].map(([test, runs]) => ({ test, runs }));
}toMarkdown
toMarkdown(diffs) turns compare’s answer into the body of a PR comment. It lists each test whose report moved, by kind, and leaves out the rest. A render time counts as moved at 5 ms or more, and JS at start at 5 KB or more or when a file changed. With nothing moved, it says **ogygia**: no change in the measured tests.
npx ogygia perf covers the same ground without a test suite, from a budget file (Perf budgets).
**ogygia**: what changed
- **cart.spec.ts › buying stays quick** (profile)
- score 86 → 79
- server render 142 → 188 ms
- JS at start 96 → 131 KB: new chart.js 35 KB
- **home.spec.ts › home hydrates cleanly** (hydration)
- hydration broke: Carousel#2: clean → changed