forked from tester-army/e2e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench-ab.ts
More file actions
323 lines (300 loc) · 14.7 KB
/
Copy pathbench-ab.ts
File metadata and controls
323 lines (300 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Main vs this branch, measured the same way every time. Builds the merge
* base in a worktree under the system temp directory, builds the working
* tree, then runs one benchmark suite through each build's own CLI in pairs,
* in a seeded random order, until the suite's time change resolves against
* the threshold or the pair budget runs out.
*
* Usage:
*
* node scripts/bench-ab.ts [options] [-- <e2e run arguments>]
*
* --base <ref> compare against the merge base with this ref (origin/main)
* --suite <app> the app under apps/ whose suite runs (web-benchmark)
* --config <file> the suite config, relative to the app (its default)
* --workers <n> e2e run --workers (1)
* --threshold <pct> the change that counts, in percent (5)
* --floor <ms> the smallest paired difference that counts (10)
* --min-pairs <n> pairs before the first verdict (6)
* --max-pairs <n> pairs at most (20)
* --batch <n> pairs between verdicts (2)
* --warmup <n> pairs run first and discarded (1)
* --timeout <min> wall clock for the measured pairs (30)
* --seed <n> order and bootstrap seed (1)
* --out <dir> where result.json, summary.md, and the reports go
* --no-build reuse the working tree's current build
* --rebuild-base build the base worktree again
*
* The harness sets `e2e run --output` itself, so a forwarded `--output` (or
* the removed `--artifacts`) is refused.
*
* Runs go with `CI=1`, so committed replay-cache recordings replay read-only,
* `reuseExisting` servers start fresh per run, and no `GITHUB_*` variable is
* passed, so the GitHub reporter never posts. A step without a recording
* calls the model, which needs its key in the environment.
*
* Behavior is checked before time: a test whose status or step sequence
* differs between the builds, or between two runs of one build, is listed
* and left out of the timing rows. Wall-time verdicts from shared CI runners
* are noise; read the behavior and counter sections there.
*/
import { spawn, execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { parseArgs } from 'node:util';
import { compareSamples, SUITE } from './bench-ab/compare.ts';
import { renderMarkdown } from './bench-ab/markdown.ts';
import { sampleFromReport, type ReportDocument, type Sample } from './bench-ab/sample.ts';
import { seededRandom } from './bench-ab/stats.ts';
const REPO_ROOT = path.resolve(import.meta.dirname, '..');
const { values: options, positionals: forwarded } = parseArgs({
allowPositionals: true,
options: {
base: { type: 'string', default: 'origin/main' },
suite: { type: 'string', default: 'web-benchmark' },
config: { type: 'string' },
workers: { type: 'string', default: '1' },
threshold: { type: 'string', default: '5' },
floor: { type: 'string', default: '10' },
'min-pairs': { type: 'string', default: '6' },
'max-pairs': { type: 'string', default: '20' },
batch: { type: 'string', default: '2' },
warmup: { type: 'string', default: '1' },
timeout: { type: 'string', default: '30' },
seed: { type: 'string', default: '1' },
out: { type: 'string' },
'no-build': { type: 'boolean', default: false },
'rebuild-base': { type: 'boolean', default: false },
},
});
/** A positive integer option, or exit 2 naming it. */
function integer(name: keyof typeof options, min = 1): number {
const value = Number(options[name]);
if (!Number.isInteger(value) || value < min) fail(`--${name} must be an integer of at least ${min}`);
return value;
}
function fail(message: string): never {
process.stderr.write(`bench-ab: ${message}\n`);
process.exit(2);
}
function log(message: string): void {
process.stderr.write(`bench-ab: ${message}\n`);
}
function git(...args: string[]): string {
return execFileSync('git', args, { cwd: REPO_ROOT, encoding: 'utf8' }).trim();
}
/** Runs a build command in `cwd`, streaming nothing unless it fails. */
function build(cwd: string, command: string, args: string[]): void {
try {
execFileSync(command, args, { cwd, stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8' });
} catch (error) {
const output = error as { stdout?: string; stderr?: string };
fail(`${command} ${args.join(' ')} failed in ${cwd}\n${(output.stdout ?? '').slice(-4000)}${(output.stderr ?? '').slice(-4000)}`);
}
}
/** The root build plus the suite app's own `build` script, when it has one. */
function buildCheckout(root: string, suite: string): void {
build(root, 'pnpm', ['run', 'build']);
const manifest = JSON.parse(readFileSync(path.join(root, 'apps', suite, 'package.json'), 'utf8')) as { scripts?: Record<string, string> };
if (manifest.scripts?.['build'] !== undefined) build(path.join(root, 'apps', suite), 'pnpm', ['run', 'build']);
}
/** A worktree of `sha` under the temp directory, installed and built once. */
function baseCheckout(sha: string, suite: string): string {
const root = path.join(os.tmpdir(), 'e2e-bench-ab', sha);
const marker = path.join(root, '.bench-ab-built');
if (!existsSync(root)) {
log(`adding worktree ${root}`);
git('worktree', 'add', '--detach', root, sha);
}
if (options['rebuild-base'] || !existsSync(marker) || readFileSync(marker, 'utf8') !== suite) {
log(`installing and building base ${sha.slice(0, 8)}`);
build(root, 'pnpm', ['install', '--frozen-lockfile', '--prefer-offline']);
buildCheckout(root, suite);
writeFileSync(marker, suite);
}
return root;
}
/** Digest of what a suite runs: its tests, configs, and committed recordings. */
function suiteDigest(appDir: string): string {
const hash = createHash('sha256');
const visit = (file: string): void => {
if (!existsSync(file)) return;
if (statSync(file).isDirectory()) {
for (const entry of readdirSync(file).toSorted()) visit(path.join(file, entry));
return;
}
hash.update(path.relative(appDir, file)).update(readFileSync(file));
};
for (const entry of readdirSync(appDir).toSorted()) {
if (entry.startsWith('tests') || /^e2e\..*config\.m?ts$/.test(entry)) visit(path.join(appDir, entry));
}
visit(path.join(appDir, '.e2e', 'cache'));
return hash.digest('hex');
}
/** The environment every run gets: CI semantics, no telemetry, no GitHub posting. */
function runEnvironment(): NodeJS.ProcessEnv {
const env: NodeJS.ProcessEnv = { ...process.env, CI: '1', E2E_TELEMETRY_DISABLED: '1', NO_COLOR: '1' };
for (const name of Object.keys(env)) if (name.startsWith('GITHUB_')) delete env[name];
return env;
}
/** The report's run-level errors, one per line, or nothing when there is no parsable report. */
function runErrors(text: string): string {
try {
const report = JSON.parse(text) as { run?: { errors?: readonly { code: string; message: string }[] } };
return (report.run?.errors ?? []).map((error) => `${error.code}: ${error.message}\n`).join('');
} catch {
return '';
}
}
/**
* Where one run's results go, and what to remove once it ended. A build with
* `--output` keeps them in a directory inside the app, which the flag
* requires; an older build (the merge base, before the flag) takes
* `--artifacts` under `outDir`.
*/
function resultArgs(root: string, appDir: string, outDir: string): { args: string[]; written: string } {
const cli = readFileSync(path.join(root, 'packages', 'e2e', 'dist', 'cli', 'index.js'), 'utf8');
if (cli.includes("'--output <dir>'")) {
const written = path.join(appDir, '.e2e-bench-ab');
return { args: ['--output', written], written };
}
const written = path.join(outDir, 'artifacts');
return { args: ['--artifacts', written], written };
}
/** One `e2e run` through the build at `root`; exits 2 on a run that did not produce test results. */
function runOnce(root: string, suite: string, runArgs: readonly string[], outDir: string): Promise<Sample> {
const appDir = path.join(root, 'apps', suite);
const results = resultArgs(root, appDir, outDir);
const bin = path.join(appDir, 'node_modules', 'e2e', 'dist', 'cli', 'bin.js');
const args = [bin, 'run', '--reporter', 'json', ...results.args, ...runArgs];
return new Promise((resolve) => {
const started = process.hrtime.bigint();
const child = spawn(process.execPath, args, { cwd: appDir, env: runEnvironment(), stdio: ['ignore', 'pipe', 'pipe'] });
const stdout: Buffer[] = [];
let stderr = '';
child.stdout.on('data', (chunk: Buffer) => stdout.push(chunk));
child.stderr.on('data', (chunk: Buffer) => { stderr = (stderr + chunk.toString()).slice(-8000); });
child.on('close', (code) => {
const wallMs = Number(process.hrtime.bigint() - started) / 1e6;
const text = Buffer.concat(stdout).toString('utf8');
writeFileSync(path.join(outDir, 'report.json'), text);
rmSync(results.written, { recursive: true, force: true });
const errors = runErrors(text);
// Exit 1 is failed tests, which compare like any others; a run-level
// error or a run that attempted nothing measured no test at all.
if ((code !== 0 && code !== 1) || errors !== '') fail(`e2e run exited ${code} in ${appDir}\n${errors}${stderr}`);
const sample = sampleFromReport(JSON.parse(text) as ReportDocument, wallMs);
if (![...sample.cases.values()].some((entry) => entry.durationMs > 0)) fail(`e2e run attempted no test in ${appDir}\n${stderr}`);
resolve(sample);
});
});
}
function machine(): string {
const cpus = os.cpus();
const load = os.loadavg()[0]!;
return `${os.platform()} ${os.arch()}, ${cpus[0]?.model ?? 'unknown CPU'} x${cpus.length}, Node ${process.versions.node}, load ${load.toFixed(1)}`;
}
const suite = options.suite;
const threshold = Number(options.threshold) / 100;
if (!(threshold > 0)) fail('--threshold must be a positive percent');
const floorMs = integer('floor', 0);
const minPairs = integer('min-pairs', 2);
const maxPairs = Math.max(integer('max-pairs', 2), minPairs);
const batch = integer('batch');
const warmup = integer('warmup', 0);
const timeoutMs = integer('timeout') * 60_000;
const seed = integer('seed', 0);
if (!existsSync(path.join(REPO_ROOT, 'apps', suite, 'package.json'))) fail(`no app at apps/${suite}`);
const baseSha = git('merge-base', options.base, 'HEAD');
const headSha = git('rev-parse', 'HEAD');
const dirty = git('status', '--porcelain').length > 0;
// The harness owns where results go, and removes them after each run.
const resultFlag = forwarded.find((arg) => /^--(output|artifacts)(=|$)/.test(arg));
if (resultFlag !== undefined) fail(`${resultFlag.split('=')[0]!} is set by bench:ab; leave it out of the e2e run arguments`);
const runArgs = [
'--workers', options.workers,
...(options.config === undefined ? [] : ['--config', options.config]),
...forwarded,
];
const baseRoot = baseCheckout(baseSha, suite);
if (!options['no-build']) {
log('building head');
buildCheckout(REPO_ROOT, suite);
}
const suiteDiffers = suiteDigest(path.join(baseRoot, 'apps', suite)) !== suiteDigest(path.join(REPO_ROOT, 'apps', suite));
if (suiteDiffers) log('the suite differs between base and head; each side runs its own');
const cores = os.cpus().length;
if (os.loadavg()[0]! > cores / 2) log(`load average ${os.loadavg()[0]!.toFixed(1)} on ${cores} cores; timings will be noisy`);
const outRoot = path.resolve(options.out ?? path.join(os.tmpdir(), 'e2e-bench-ab', 'runs', new Date().toISOString().replaceAll(':', '-')));
// Reports are redacted, but they carry the app's screens; keep them the user's own.
mkdirSync(outRoot, { recursive: true, mode: 0o700 });
const random = seededRandom(seed);
const base: Sample[] = [];
const head: Sample[] = [];
const order: ('base-first' | 'head-first')[] = [];
/** Runs one pair in a coin-flip order; warmup pairs are measured and dropped. */
async function pair(index: number, measured: boolean): Promise<void> {
const headFirst = random() < 0.5;
const dir = path.join(outRoot, measured ? `pair-${index}` : `warmup-${index}`);
const sides = headFirst ? (['head', 'base'] as const) : (['base', 'head'] as const);
const results: Partial<Record<'base' | 'head', Sample>> = {};
for (const side of sides) {
const outDir = path.join(dir, side);
mkdirSync(outDir, { recursive: true });
results[side] = await runOnce(side === 'base' ? baseRoot : REPO_ROOT, suite, runArgs, outDir);
}
if (!measured) return;
base.push(results.base!);
head.push(results.head!);
order.push(headFirst ? 'head-first' : 'base-first');
const suiteMs = (sample: Sample) => [...sample.cases.values()].reduce((sum, entry) => sum + entry.durationMs, 0);
const b = suiteMs(results.base!);
const h = suiteMs(results.head!);
log(`pair ${index}: base ${(b / 1000).toFixed(2)} s, head ${(h / 1000).toFixed(2)} s (${headFirst ? 'head' : 'base'} first)`);
}
for (let index = 1; index <= warmup; index += 1) {
log(`warmup pair ${index}`);
await pair(index, false);
}
const started = Date.now();
/** The suite row's verdict over the tests whose behavior matched so far. */
const headlineVerdict = () => compareSamples(base, head, { threshold, floorMs }).timings.find((row) => row.name === SUITE)?.delta.verdict ?? 'unresolved';
let stop: string | undefined;
while (stop === undefined && base.length < maxPairs) {
await pair(base.length + 1, true);
const due = base.length >= minPairs && ((base.length - minPairs) % batch === 0 || base.length === maxPairs);
if (!due) continue;
if (headlineVerdict() !== 'unresolved') stop = `resolved after ${base.length} pairs`;
else if (Date.now() - started > timeoutMs) stop = `stopped at the ${options.timeout}-minute timeout, unresolved`;
}
stop ??= `stopped at the ${maxPairs}-pair budget, unresolved`;
const comparison = compareSamples(base, head, { threshold, floorMs });
const header = {
base: { ref: `merge base with ${options.base}`, sha: baseSha },
head: { sha: headSha, dirty },
suite: `apps/${suite}`,
command: ['e2e run', ...runArgs].join(' '),
threshold,
floorMs,
stop,
machine: machine(),
suiteDiffers,
};
const markdown = renderMarkdown(header, comparison);
const serialize = (sample: Sample) => ({
exitCode: sample.exitCode,
wallMs: sample.wallMs,
counters: sample.counters,
cases: Object.fromEntries([...sample.cases].map(([key, entry]) => [key, { durationMs: entry.durationMs, timings: entry.timings }])),
});
writeFileSync(path.join(outRoot, 'summary.md'), markdown);
writeFileSync(path.join(outRoot, 'result.json'), `${JSON.stringify({
version: 'bench-ab/1',
header,
comparison,
pairs: base.map((sample, index) => ({ pair: index + 1, order: order[index], base: serialize(sample), head: serialize(head[index]!) })),
}, null, 2)}\n`);
process.stdout.write(markdown);
log(`wrote ${path.join(outRoot, 'summary.md')} and result.json`);