CI: fail pull requests whose benchmark ratios regress #16

Merged
sionzee merged 1 commit from bench-regressions into main 2026-11-03 21:41:20 +01:00
Owner

Benchmark regressions in CI: npm run bench:check, plus a Forgejo workflow that runs it on pull requests.

What it does

scripts/bench-check.mjs runs:

  • npm run bench: match(...) through the transform against a hand-written switch. The README says that ratio is about 1x (it's 4–6x without the transform), so the check is that it doesn't grow.
  • npm run bench:loops: callbacks against loops for every case and size. The four chains the transform rewrites (forEach, map, filter.map.reduce, filter.map.some) must keep their advantage. filter, reduce and some are printed as info only, because no claim depends on them moving either way.
  • Every ```bench block in docs/ (errors, inline ×2, loops, shapes; and whatever PRs add later). Each is built the way docs/.vitepress/config.mts builds it (as written vs the native transform's output, or the two compare examples) and timed the way theme/bench.worker.ts times it: batches of at least 25 ms, 12 alternating rounds, median, ×3. If the variants return different results, the check fails.

It compares each ratio to scripts/bench-baseline.json, never absolute ns. The runner is shared and isn't the machine the baseline came from, but two variants timed in the same run on the same machine still compare, and the ratios are what the docs claim.

Tolerance, and why it's loose

A ratio fails when it is more than 2x past its baseline in the direction that breaks its claim. A ratio can have its own tolerance; filter.map.some at 1,000 elements has 3x, because it's bimodal here (35x or 69x). When anything fails, everything is measured again and each ratio keeps the better of its two results, so a single noisy stretch doesn't fail the build.

This catches a rewrite that stops happening (loops.md's 3.6x falling to 1x, match going back to 4–6x) or a docs claim the engine broke. It won't catch a 10–20% drift, and the ratios near 1x (map at 10/1,000 elements) can only fail if they really collapse. The README note says so.

Before picking the tolerance I measured each ratio's spread over 5 runs (max/min up to 1.5, plus the one bimodal case). The baseline is the median of those 5 runs (Node 26.13). npm run bench:check -- --update rewrites it and keeps per-ratio tolerances.

Tested here

  • 10 passing runs in a row: 7 sequential and 3 concurrent (to load the machine like a shared runner), exit 0 every time, and none needed the re-measure. About 18 s per run, or about 36 s when it re-measures.
  • A forced regression (baseline for loops.md set to 20x, for match/switch to 0.3x) exits 1 after the re-measure, names both ratios, and prints how to update the baseline.
  • Not run on a Forgejo runner yet: this PR's own workflow run is the first. If the runner's hardware moves some ratio past 2x of this machine's, the fix is a baseline from the runner (--update there) or a per-ratio tolerance. I'll check the run.

Files

scripts/bench-check.mjs, scripts/bench-baseline.json, .forgejo/workflows/bench.yml (pull_request + workflow_dispatch; Node 26, Rust, npm ci, napi build, npm run bench:check, styled after pages.yml), the bench:check npm script, and a README note under "Run it".

Not done / uncertain

  • The bench blocks added by #3 (no-accumulating-copy) and #8 (stable-shape) will show as new until the baseline is updated after they merge. A ratio with no baseline doesn't fail.
  • No absolute-time tracking, and no history of results across runs.

🤖 Generated with Claude Code

Benchmark regressions in CI: `npm run bench:check`, plus a Forgejo workflow that runs it on pull requests. ## What it does `scripts/bench-check.mjs` runs: - `npm run bench`: `match(...)` through the transform against a hand-written switch. The README says that ratio is about 1x (it's 4–6x without the transform), so the check is that it doesn't grow. - `npm run bench:loops`: callbacks against loops for every case and size. The four chains the transform rewrites (forEach, map, filter.map.reduce, filter.map.some) must keep their advantage. `filter`, `reduce` and `some` are printed as `info` only, because no claim depends on them moving either way. - **Every ```bench block in `docs/`** (errors, inline ×2, loops, shapes; and whatever PRs add later). Each is built the way `docs/.vitepress/config.mts` builds it (as written vs the native transform's output, or the two `compare` examples) and timed the way `theme/bench.worker.ts` times it: batches of at least 25 ms, 12 alternating rounds, median, ×3. If the variants return different results, the check fails. It compares each **ratio** to `scripts/bench-baseline.json`, never absolute ns. The runner is shared and isn't the machine the baseline came from, but two variants timed in the same run on the same machine still compare, and the ratios are what the docs claim. ## Tolerance, and why it's loose A ratio fails when it is more than **2x** past its baseline in the direction that breaks its claim. A ratio can have its own tolerance; `filter.map.some` at 1,000 elements has 3x, because it's bimodal here (35x or 69x). When anything fails, **everything is measured again** and each ratio keeps the better of its two results, so a single noisy stretch doesn't fail the build. This catches a rewrite that stops happening (loops.md's 3.6x falling to 1x, `match` going back to 4–6x) or a docs claim the engine broke. It won't catch a 10–20% drift, and the ratios near 1x (map at 10/1,000 elements) can only fail if they really collapse. The README note says so. Before picking the tolerance I measured each ratio's spread over 5 runs (max/min up to 1.5, plus the one bimodal case). The baseline is the median of those 5 runs (Node 26.13). `npm run bench:check -- --update` rewrites it and keeps per-ratio tolerances. ## Tested here - **10 passing runs in a row**: 7 sequential and 3 concurrent (to load the machine like a shared runner), exit 0 every time, and none needed the re-measure. About 18 s per run, or about 36 s when it re-measures. - A forced regression (baseline for loops.md set to 20x, for match/switch to 0.3x) exits 1 after the re-measure, names both ratios, and prints how to update the baseline. - Not run on a Forgejo runner yet: this PR's own workflow run is the first. If the runner's hardware moves some ratio past 2x of this machine's, the fix is a baseline from the runner (`--update` there) or a per-ratio tolerance. I'll check the run. ## Files `scripts/bench-check.mjs`, `scripts/bench-baseline.json`, `.forgejo/workflows/bench.yml` (pull_request + workflow_dispatch; Node 26, Rust, `npm ci`, napi build, `npm run bench:check`, styled after pages.yml), the `bench:check` npm script, and a README note under "Run it". ## Not done / uncertain - The bench blocks added by #3 (no-accumulating-copy) and #8 (stable-shape) will show as `new` until the baseline is updated after they merge. A ratio with no baseline doesn't fail. - No absolute-time tracking, and no history of results across runs. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
CI: fail pull requests whose benchmark ratios regress
All checks were successful
Benchmarks / bench (pull_request) Successful in 1m36s
408eb60d20
scripts/bench-check.mjs runs `npm run bench`, `npm run bench:loops` and every ```bench block in
docs/ (built and timed the way the docs site does), and compares the ratios the docs claim to
scripts/bench-baseline.json: match through the transform against a switch, callbacks
against loops, as written against compiled. Ratios of two variants timed in the same run,
not absolute times, because the runner is shared and isn't the machine the baseline came
from.

A ratio fails when it is more than 2x past its baseline in the direction that breaks its
claim, in two measurements (everything is measured again before failing). That is loose on
purpose: it catches a rewrite that stops happening (a 3-4x advantage falling to 1x), not a
10% drift. Passed 10 runs in a row here, 3 of them concurrently.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch bench-regressions 2026-11-03 21:41:20 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!16
No description provided.