|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * benchmark-trend.mjs — performance trend tracking across pushes. |
| 4 | + * |
| 5 | + * Appends this run's benchmark medians to benchmarks/perf-history.jsonl |
| 6 | + * (one JSON object per line) and fails when a median drifts more than 2x |
| 7 | + * from the trailing five-entry baseline for its series. Cold CI runners |
| 8 | + * are noisy, so 2x is the regression bar — not a tuning target. |
| 9 | + * |
| 10 | + * node scripts/benchmark-trend.mjs --file <report.json> # append + gate |
| 11 | + * node scripts/benchmark-trend.mjs --status # gate only, no append |
| 12 | + * |
| 13 | + * Report contract (the --json output of both benchmark runners): |
| 14 | + * { benchmark: string, stats: { median: number, ... }, gate: "PASS"|"FAIL", ... } |
| 15 | + * |
| 16 | + * Gates (fail-closed, exit 1): |
| 17 | + * - the report's own gate must be PASS (never record a failing run as a |
| 18 | + * trend point) |
| 19 | + * - each tracked series (benchmark name) with >= 5 history entries must |
| 20 | + * have median <= 2x the median-of-medians of its last 5 entries; |
| 21 | + * the offender names baseline, current, and ratio |
| 22 | + * - the history file must stay parseable: torn/truncated lines are |
| 23 | + * reported and skipped, never silently re-written |
| 24 | + * |
| 25 | + * History is one line per push, append-only: perf history is evidence, |
| 26 | + * not state to be rewritten. |
| 27 | + */ |
| 28 | + |
| 29 | +import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs"; |
| 30 | +import { dirname, join } from "node:path"; |
| 31 | +import { fileURLToPath } from "node:url"; |
| 32 | + |
| 33 | +const REPO = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 34 | +const HISTORY = join(REPO, "benchmarks", "perf-history.jsonl"); |
| 35 | +const BASELINE_WINDOW = 5; |
| 36 | +const DRIFT_FACTOR = 2; |
| 37 | + |
| 38 | +function parseHistory() { |
| 39 | + if (!existsSync(HISTORY)) return { entries: [], torn: 0 }; |
| 40 | + const lines = readFileSync(HISTORY, "utf8").split("\n").filter((l) => l.trim() !== ""); |
| 41 | + const entries = []; |
| 42 | + let torn = 0; |
| 43 | + for (const line of lines) { |
| 44 | + try { |
| 45 | + const e = JSON.parse(line); |
| 46 | + if (e && e.benchmark && e.stats && typeof e.stats.median === "number") entries.push(e); |
| 47 | + else torn += 1; |
| 48 | + } catch { |
| 49 | + torn += 1; |
| 50 | + } |
| 51 | + } |
| 52 | + return { entries, torn }; |
| 53 | +} |
| 54 | + |
| 55 | +function medianOf(values) { |
| 56 | + const sorted = [...values].sort((a, b) => a - b); |
| 57 | + const mid = Math.floor(sorted.length / 2); |
| 58 | + return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; |
| 59 | +} |
| 60 | + |
| 61 | +const fileArg = process.argv.indexOf("--file"); |
| 62 | +const statusOnly = process.argv.includes("--status"); |
| 63 | +const dryRun = process.argv.includes("--dry-run"); // gate, report, write nothing (PR runs) |
| 64 | +if (fileArg === -1 && !statusOnly) { |
| 65 | + console.error("benchmark-trend: pass --file <benchmark-report.json> (append+gate) or --status (gate only)"); |
| 66 | + process.exit(2); |
| 67 | +} |
| 68 | + |
| 69 | +let report = null; |
| 70 | +if (fileArg > -1) { |
| 71 | + const path = process.argv[fileArg + 1]; |
| 72 | + report = JSON.parse(readFileSync(path, "utf8")); |
| 73 | + if (!report?.benchmark || !report?.stats || typeof report.stats.median !== "number") { |
| 74 | + console.error("benchmark-trend: report is missing benchmark/stats.median — not a benchmark --json output"); |
| 75 | + process.exit(2); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const { entries, torn } = parseHistory(); |
| 80 | + |
| 81 | +// baseline: trailing BASELINE_WINDOW entries of the same series |
| 82 | +const series = entries.filter((e) => e.benchmark === report?.benchmark); |
| 83 | +const baseline = series.slice(-BASELINE_WINDOW); |
| 84 | +const failures = []; |
| 85 | + |
| 86 | +if (report && baseline.length >= BASELINE_WINDOW) { |
| 87 | + const baseMedian = medianOf(baseline.map((e) => e.stats.median)); |
| 88 | + const ratio = report.stats.median / baseMedian; |
| 89 | + if (ratio > DRIFT_FACTOR) { |
| 90 | + failures.push( |
| 91 | + `median ${report.stats.median}ms is ${ratio.toFixed(2)}x the ${BASELINE_WINDOW}-run baseline ` + |
| 92 | + `(${baseMedian}ms) — drift beyond the ${DRIFT_FACTOR}x regression bar for ${report.benchmark}` |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +if (report && report.gate !== "PASS") { |
| 98 | + failures.push(`report gate is ${report.gate} — a failing run is never recorded as a trend point`); |
| 99 | +} |
| 100 | + |
| 101 | +const record = report && failures.length === 0 |
| 102 | + ? { |
| 103 | + benchmark: report.benchmark, |
| 104 | + utc: new Date().toISOString(), |
| 105 | + // runner identity: machine noise is real; the 2x bar absorbs it, the |
| 106 | + // field makes it visible when comparing CI vs local entries |
| 107 | + runner: process.env.RUNNER_OS ?? process.platform, |
| 108 | + stats: report.stats, |
| 109 | + gate: report.gate, |
| 110 | + } |
| 111 | + : null; |
| 112 | + |
| 113 | +if (!statusOnly && !dryRun && record) { |
| 114 | + mkdirSync(dirname(HISTORY), { recursive: true }); |
| 115 | + writeFileSync(HISTORY, JSON.stringify(record) + "\n", { flag: "a" }); |
| 116 | +} |
| 117 | + |
| 118 | +const status = { |
| 119 | + trend: "benchmark-trend", |
| 120 | + file: "benchmarks/perf-history.jsonl", |
| 121 | + totalEntries: entries.length + (record ? 1 : 0), |
| 122 | + seriesEntries: report ? series.length + (record ? 1 : 0) : undefined, |
| 123 | + tornLines: torn, |
| 124 | + baselineWindow: baseline.length, |
| 125 | + recorded: !!record && !dryRun, |
| 126 | + dryRun, |
| 127 | + gate: failures.length === 0 ? "PASS" : "FAIL", |
| 128 | + failures, |
| 129 | +}; |
| 130 | + |
| 131 | +console.log(JSON.stringify(status, null, 2)); |
| 132 | +process.exitCode = failures.length === 0 ? 0 : 1; |
0 commit comments