Skip to content

Commit e394732

Browse files
committed
ci: fail the benchmark report when a js-framework round writes no result
A round that throws before writeFileSync was dropped the same way as the byte directory, which never contains that file. The surviving round was compared alone, so ABBA drift cancellation was lost and a base-round crash could still produce a green headline.
1 parent 781753b commit e394732

4 files changed

Lines changed: 169 additions & 11 deletions

File tree

‎.github/workflows/pr-bench.yml‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,19 @@ jobs:
8989
side=${round%%:*}
9090
tree=$GITHUB_WORKSPACE
9191
if [ "$side" = base ]; then tree=$BASE_TREE; fi
92-
if (cd "$tree" && node benchmarks/bench.mjs --results-dir="$RESULTS/$side-js-${round##*:}" js-framework); then :
93-
elif [ "$side" = head ]; then status=1
94-
else echo "::warning::base js-framework round failed"; fi
92+
dir="$RESULTS/$side-js-${round##*:}"
93+
if (cd "$tree" && node benchmarks/bench.mjs --results-dir="$dir" js-framework); then :
94+
else
95+
# A throw before writeFileSync leaves no JSON, which the report
96+
# cannot tell from the byte directory. Record the failed round.
97+
if [ ! -f "$dir/js-framework.json" ]; then
98+
mkdir -p "$dir"
99+
name=${dir##*/}
100+
printf '{"suite":"js-framework","iterations":null,"harnessExit":1,"failed":"js-framework failed before writing a result (%s)","targets":[]}\n' "$name" > "$dir/js-framework.json"
101+
fi
102+
if [ "$side" = head ]; then status=1
103+
else echo "::warning::base js-framework round failed"; fi
104+
fi
95105
done
96106
echo "status=$status" >> "$GITHUB_OUTPUT"
97107

‎benchmarks/pr-report.mjs‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
// Several directories per side are rounds of the same commit. CI measures
88
// js-framework base, head, head, base so linear runner drift cancels in each
99
// side's mean; disagreement between rounds widens the margin of error.
10+
// A directory that already holds a different suite is not a round of this one.
11+
// A round that died before writing JSON fails the merged suite, so the leftover
12+
// round is not compared on its own.
1013
//
1114
// Deterministic operations (bytes, DOM-operation and call counts) report every
1215
// change exactly. A timing operation is only called faster or slower outside its
@@ -231,15 +234,27 @@ function mergeStat(stats) {
231234
}
232235

233236
export function mergeRounds(rounds) {
237+
const missing = rounds.filter((round) => !round).length;
234238
const results = rounds.filter(Boolean);
235-
if (results.length <= 1) return results[0] ?? null;
236-
const failed = results.map((result) => failureOf(result)).filter(Boolean);
239+
// A missing round is a crash before writeFileSync, not an empty slot. One
240+
// survivor is not a balanced ABBA mean and must not be returned clean.
241+
if (missing === 0 && results.length <= 1) return results[0] ?? null;
242+
const failed = [
243+
...results.map((result) => failureOf(result)).filter(Boolean),
244+
...(missing === 0
245+
? []
246+
: [
247+
missing === 1
248+
? '1 round failed before writing a result'
249+
: `${missing} rounds failed before writing a result`,
250+
]),
251+
];
237252
const names = [
238253
...new Set(results.flatMap((result) => result.targets.map((target) => target.name))),
239254
];
240255
return {
241-
suite: results[0].suite,
242-
iterations: results[0].iterations,
256+
suite: results[0]?.suite ?? null,
257+
iterations: results[0]?.iterations ?? null,
243258
harnessExit: failed.length ? 1 : 0,
244259
...(failed.length ? { failed: failed.join(' | ') } : null),
245260
targets: names.map((name) => {
@@ -257,15 +272,40 @@ export function mergeRounds(rounds) {
257272
};
258273
}
259274

275+
function readRound(directory, suite) {
276+
const file = path.join(directory, `${suite}.json`);
277+
if (fs.existsSync(file)) return JSON.parse(fs.readFileSync(file, 'utf8'));
278+
// Byte results and js-framework rounds share one directory list. A directory
279+
// that published a different suite never ran this one.
280+
const holdsOtherSuite = SUITES.some(
281+
(name) => name !== suite && fs.existsSync(path.join(directory, `${name}.json`)),
282+
);
283+
if (holdsOtherSuite || !fs.existsSync(directory) || suite !== 'js-framework') return null;
284+
// bench.mjs leaves build-*.log / server-*.log when startup throws before
285+
// writeFileSync. Those files mark a js-framework round, not a byte suite.
286+
const diedDuringStartup = fs
287+
.readdirSync(directory)
288+
.some((name) => name.startsWith('build-') || name.startsWith('server-'));
289+
if (!diedDuringStartup) return null;
290+
return {
291+
suite,
292+
iterations: null,
293+
harnessExit: 1,
294+
failed: `${suite} failed before writing a result (${path.basename(directory)})`,
295+
targets: [],
296+
};
297+
}
298+
260299
function readSuites(directories) {
261300
return Object.fromEntries(
262301
SUITES.map((suite) => [
263302
suite,
264303
mergeRounds(
265-
directories.split(',').map((directory) => {
266-
const file = path.join(directory, `${suite}.json`);
267-
return fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : null;
268-
}),
304+
directories
305+
.split(',')
306+
.filter(Boolean)
307+
.map((directory) => readRound(directory, suite))
308+
.filter((round) => round != null),
269309
),
270310
]),
271311
);

‎benchmarks/pr-report.test.mjs‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import assert from 'node:assert/strict';
2+
import { spawnSync } from 'node:child_process';
3+
import fs from 'node:fs';
4+
import os from 'node:os';
5+
import path from 'node:path';
26
import { test } from 'node:test';
7+
import { fileURLToPath } from 'node:url';
38
import { COMMENT_MARKER, compareSuite, mergeRounds, renderReport } from './pr-report.mjs';
49

10+
const reportScript = path.join(path.dirname(fileURLToPath(import.meta.url)), 'pr-report.mjs');
11+
512
const bytes = (value) => ({ median: value, min: value, samples: 1 });
613
const timed = (score, min, rme) => ({ score, median: score, min, rme, scoreRme: rme, samples: 8 });
714
const suite = (name, targets) => ({ suite: name, iterations: 1, harnessExit: 0, targets });
@@ -65,6 +72,20 @@ test('a failed round fails the merged suite', () => {
6572
assert.equal(mergeRounds([ok, { ...ok, harnessExit: 1, failed: 'gate' }]).failed, 'gate');
6673
});
6774

75+
test('a missing round fails the merged suite instead of leaving the survivor', () => {
76+
const ok = suite('js-framework', [{ name: 'octane-tsrx', ops: { run: timed(10, 9, 3) } }]);
77+
const merged = mergeRounds([ok, null]);
78+
assert.match(merged.failed, /1 round failed before writing a result/);
79+
assert.equal(merged.harnessExit, 1);
80+
const body = renderReport({
81+
suites: ['js-framework'],
82+
base: { 'js-framework': merged },
83+
head: { 'js-framework': ok },
84+
});
85+
assert.match(body, /⚠️ js-framework failed on the base commit and was not compared/);
86+
assert.doesNotMatch(body, /🟢 No size increases/);
87+
});
88+
6889
test('timing verdicts never turn the headline red', () => {
6990
const base = {
7091
'js-framework': suite('js-framework', [{ name: 'octane-tsrx', ops: { run: timed(10, 9, 2) } }]),
@@ -108,6 +129,88 @@ test('a suite that failed on the base commit keeps the headline from reporting g
108129
assert.doesNotMatch(body, /🟢 No size increases/);
109130
});
110131

132+
function writeJson(dir, name, value) {
133+
fs.mkdirSync(dir, { recursive: true });
134+
fs.writeFileSync(path.join(dir, name), JSON.stringify(value));
135+
}
136+
137+
function renderDirectories(baseDirs, headDirs) {
138+
const out = spawnSync(
139+
process.execPath,
140+
[reportScript, `--base=${baseDirs}`, `--head=${headDirs}`],
141+
{
142+
encoding: 'utf8',
143+
},
144+
);
145+
assert.equal(out.status, 0, out.stderr);
146+
return out.stdout;
147+
}
148+
149+
const byteSuites = (dir) => {
150+
writeJson(
151+
dir,
152+
'bundle-size.json',
153+
suite('bundle-size', [{ name: 'octane-tsrx', ops: { js_gzip: bytes(1000) } }]),
154+
);
155+
writeJson(
156+
dir,
157+
'bundle-reachability.json',
158+
suite('bundle-reachability', [{ name: 'octane', ops: { raw: bytes(1000) } }]),
159+
);
160+
};
161+
162+
test('a js-framework round that dies before writing JSON is not compared alone', () => {
163+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'pr-report-'));
164+
try {
165+
const base = path.join(root, 'base');
166+
const baseRound = path.join(root, 'base-js-1');
167+
const crashed = path.join(root, 'base-js-2');
168+
const head = path.join(root, 'head');
169+
byteSuites(base);
170+
byteSuites(head);
171+
const timing = suite('js-framework', [{ name: 'octane-tsrx', ops: { run: timed(10, 9, 3) } }]);
172+
writeJson(baseRound, 'js-framework.json', timing);
173+
fs.mkdirSync(crashed);
174+
fs.writeFileSync(path.join(crashed, 'build-octane-tsrx-jsbench.log'), 'build failed');
175+
writeJson(head, 'js-framework.json', timing);
176+
const body = renderDirectories([base, baseRound, crashed].join(','), head);
177+
assert.match(body, /js-framework failed before writing a result \(base-js-2\)/);
178+
assert.match(body, /⚠️ js-framework failed on the base commit and was not compared/);
179+
assert.doesNotMatch(body, /bundle-size failed/);
180+
assert.doesNotMatch(body, /🟢 No size increases/);
181+
} finally {
182+
fs.rmSync(root, { recursive: true, force: true });
183+
}
184+
});
185+
186+
test('startup logs in a round directory are not a failed byte suite', () => {
187+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'pr-report-'));
188+
try {
189+
const base = path.join(root, 'base');
190+
const round1 = path.join(root, 'base-js-1');
191+
const round2 = path.join(root, 'base-js-2');
192+
const head = path.join(root, 'head');
193+
byteSuites(base);
194+
byteSuites(head);
195+
const round = (score) =>
196+
suite('js-framework', [{ name: 'octane-tsrx', ops: { run: timed(score, score - 0.5, 3) } }]);
197+
writeJson(round1, 'js-framework.json', round(10));
198+
fs.writeFileSync(path.join(round1, 'server-5176.log'), 'ready');
199+
writeJson(round2, 'js-framework.json', round(13));
200+
writeJson(head, 'js-framework.json', round(11.5));
201+
const empty = path.join(root, 'empty');
202+
fs.mkdirSync(empty);
203+
const body = renderDirectories(
204+
[base, round1, round2, empty, path.join(root, 'missing')].join(','),
205+
head,
206+
);
207+
assert.match(body, /🟢 No size increases and no timing regressions outside noise\./);
208+
assert.doesNotMatch(body, /failed before writing a result/);
209+
} finally {
210+
fs.rmSync(root, { recursive: true, force: true });
211+
}
212+
});
213+
111214
test('an unchanged pull request reports no regressions', () => {
112215
const results = {
113216
'bundle-size': suite('bundle-size', [{ name: 'octane-tsrx', ops: { js_gzip: bytes(1000) } }]),

‎scripts/ci-workflow.test.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,11 @@ describe('Pull request benchmark report', () => {
16761676
assert.ok(
16771677
prBenchWorkflow.includes('--head="$RESULTS/head,$RESULTS/head-js-1,$RESULTS/head-js-2"'),
16781678
);
1679+
assert.match(
1680+
prBenchWorkflow,
1681+
/else\n\s+# A throw before writeFileSync leaves no JSON[\s\S]*?if \[ ! -f "\$dir\/js-framework\.json" \]/,
1682+
);
1683+
assert.match(prBenchWorkflow, /js-framework failed before writing a result \(%s\)/);
16791684
assert.match(packageJson.scripts['ci:workflow:test'], /benchmarks\/pr-report\.test\.mjs/);
16801685
});
16811686

0 commit comments

Comments
 (0)