Skip to content

Commit bfd3df4

Browse files
perf(dde): performance trend tracking — append medians per push, gate against the trailing baseline
scripts/benchmark-trend.mjs appends each push's benchmark medians to benchmarks/perf-history.jsonl (append-only, one JSON line per run, runner tagged) and fails when a median exceeds 2x the median-of-medians of the series' trailing five entries — the cold-CI-noise regression bar. Gates are fail-closed: a failing benchmark run is never recorded as a trend point, a drift offender names baseline/current/ratio, and torn history lines are reported and skipped rather than silently rewritten. dde.yml records on main pushes (committing the history back so the next push baselines on real CI numbers) and gates PR runs dry (no recording, no push). Mutation- verified: a 10.83x drifted median goes red naming its baseline. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 199b36b commit bfd3df4

2 files changed

Lines changed: 160 additions & 2 deletions

File tree

‎.github/workflows/dde.yml‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ on:
1212
pull_request:
1313
branches: [main]
1414

15+
permissions:
16+
contents: write # trend tracking only: append perf-history.jsonl + push it (see the trend step)
17+
1518
jobs:
1619
dde:
1720
name: DDE evidence cycle (deterministic · replayed · adversarial)
@@ -62,9 +65,32 @@ jobs:
6265
- name: Wire payout gate — scenario rejection over the DDE HTTP endpoint
6366
run: node examples/agent-platform-integration/run-http-payout-gate.mjs
6467
- name: DDE SDK perf — verify budget must hold (median < 50ms)
65-
run: node scripts/benchmark-dde.mjs
68+
run: node scripts/benchmark-dde.mjs --json > /tmp/bench-sdk.json
6669
- name: DDE wire perf — full loopback /verify round trip (median < 150ms, engine split)
67-
run: node scripts/benchmark-dde-http.mjs
70+
run: node scripts/benchmark-dde-http.mjs --json > /tmp/bench-wire.json
71+
# Performance trend tracking: append this push's medians to the history
72+
# file and gate against the trailing 5-run baseline (drift beyond 2x =
73+
# red). The file is committed back so the next push baselines against
74+
# real CI numbers — history is evidence, append-only. PR runs gate but
75+
# do NOT record (only main pushes write history).
76+
- name: Performance trend — gate medians against the trailing baseline
77+
if: github.event_name == 'push'
78+
run: |
79+
node scripts/benchmark-trend.mjs --file /tmp/bench-sdk.json
80+
node scripts/benchmark-trend.mjs --file /tmp/bench-wire.json
81+
if [ -n "$(git diff --stat -- benchmarks/perf-history.jsonl)" ]; then
82+
git config user.name "coreguard-trend-bot"
83+
git config user.email "noreply@coreguard.local"
84+
git add benchmarks/perf-history.jsonl
85+
git commit -m "chore(perf): record benchmark medians for this push"
86+
git push
87+
fi
88+
- name: Performance trend — PR gate (no recording)
89+
if: github.event_name == 'pull_request'
90+
run: |
91+
node scripts/benchmark-trend.mjs --status
92+
node scripts/benchmark-trend.mjs --file /tmp/bench-sdk.json --dry-run
93+
node scripts/benchmark-trend.mjs --file /tmp/bench-wire.json --dry-run
6894
- name: DDE HTTP endpoint — live smoke over loopback
6995
run: |
7096
node --input-type=module -e "

‎scripts/benchmark-trend.mjs‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)