Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/bench-gate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

bench-gate: decide #868's cutover rule over two schema-2 artifacts (`src/scripts/bench-gate.ts`)
1 change: 1 addition & 0 deletions .knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"src/import-events.ts!",
"src/cli/index.ts!",
"src/scripts/benchmark-queries.ts!",
"src/scripts/bench-gate.ts!",
"knexfile.js!"
],
"project": [
Expand Down
95 changes: 80 additions & 15 deletions docs/rewrite/benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ rows and only `create` speaks for ingest.

### D3 The five read shapes, and the access path each reaches

Each shape is one filter through `find_by_filters`, drained to rows with `collect_records`, so
the measurement covers the whole page and not just its first batch. The LMDB planner picks the
path in `crates/nostream-storage/src/lmdb/events.rs`:
Each of the four REQ shapes — `by_id`, `author_kind`, `kind_since` and `tag` — is one filter
through `find_by_filters`, drained to rows with `collect_records`, so the measurement covers the
whole page and not just its first batch. `count` is the fifth read shape and is not one of them:
it goes through `count_by_filters`. The LMDB planner picks the path in
`crates/nostream-storage/src/lmdb/events.rs`:

- `by_id` — `ids`, limit 1: the `events` key is the id, so the range runs in id order
(`events.rs:1303-1316`).
Expand Down Expand Up @@ -241,18 +243,81 @@ nor the Postgres it needs can be started here. That column, and the side-by-side
gate reads, is filed as #1132 and must be run from a checkout of `origin/main` on a host with a
Docker daemon.

### D8 No threshold is asserted

This document carries evidence, not a pass/fail. The gate is #868: REQ p95 within 1.5× of the
Postgres implementation and ingest ≥ 1×, at the 1M and 10M corpora (#868). That p95 comparison
reads the band, not one run: the artifact carries `p95_us` per shape across repeats so the median
and the width are both available, and a single run's p95 inside a band as wide as `read_back`'s
above cannot decide a margin of one and a half on its own. #868's rule does not say which statistic
of the band the ratio applies to, and a band that straddles the threshold — one repeat inside the
margin, another outside it — is exactly the case that rule has to settle; this document measures
and does not decide it. The board's 50 000 events/s figure is a target tracked in #864, not a
measurement, and nothing here is compared with it. No ratio, floor or comparison against another
backend appears in this document.
### D8 The cutover gate, and the rule it applies

The gate is `src/scripts/bench-gate.ts`, run as `pnpm run bench:gate <lmdb-artifact>
<postgres-artifact>`. It takes two schema-2 artifacts — one `backend: "lmdb"`, one
`backend: "postgres"` — at one of the two corpora the rule covers, 1000000 or 10000000 events, and
decides #868's rule:

- each of the four REQ read shapes — `by_id`, `author_kind`, `kind_since`, `tag` — must have its
`p95_us` band within 1.5× of the Postgres band's;
- the ingest shape, `create`, must have its `per_second` band at or above 1× the Postgres band's.

The rule's definition is the named constants at the top of `src/scripts/bench-gate.ts` —
`REQ_MARGIN_NUM`/`REQ_MARGIN_DEN`, `COVERED_CORPORA`, `MIN_LMDB_REPEATS` and
`MIN_POSTGRES_REPEATS` — and this section is their reading, as D9 is the reading of the rig's
workload constants: a number changes in the script first, and in this section with it, so no value
here has a second source. The script's own header says the same from the other side.

There is no single statistic: each item is decided by two comparisons against the same bound — the median ratio, and the failing band edge (REQ p90 / ingest p10) against the same bound. For REQ
that is `2 × lmdb.median ≤ 3 × pg.median` and `2 × lmdb.p90 ≤ 3 × pg.p90`; for ingest it is
`lmdb.median ≥ pg.median` and `lmdb.p10 ≥ pg.p10`. All arithmetic is integer cross-multiplication,
so a zero Postgres rate is well defined. An item whose median is inside the margin but whose
failing edge is outside it is a straddle, and a straddle fails: the slowest repeat is not evidence
that the shape sits inside the margin.

The edges are the band's order statistics, not named repeats. `p10` is the band's smallest value and
`p90` its largest, and what that means depends on the metric: for a latency band the smallest value
is the fastest repeat and the largest the slowest, while for a rate band it is the other way round —
`p10` is the slowest rate and `p90` the fastest. The gate therefore tests only the edge that can
fail its bound: `p90` for the four REQ latency bands, where a slower repeat is worse against a
ceiling, and `p10` for the ingest rate band, where a slower repeat is worse against a floor. p90 is interior from ten repeats and p10 is interior from eleven, so
from ten repeats on the band is a statement over interior order statistics and the extreme repeat
is not recoverable from the artifact. The Postgres side may carry one repeat — the rig defaults to
one repeat, and CI's Postgres arm runs one — so with a one-repeat Postgres artifact only the LMDB edge is tested, because its three band fields are one value.

A gated LMDB artifact must carry `workload.repeats >= 3`, and the Postgres artifact at least 1; a
one- or two-repeat LMDB band is one or two points and cannot decide a 1.5× margin. A pair whose
`workload` echoes differ — apart from `workload.repeats`, which says how often each was measured —
is refused, as is a differing `seed`, `events` or `volatile.machine.profile`. An artifact that
records less work than its own corpus declares is refused before any band is compared: its
`event_count_read_back` must equal its `events`, and each of the four REQ shapes and `create` must
have read non-zero rows — the rig writes the artifact before `verify()` runs, so a run whose reads
failed still leaves a file on disk. `cpu_model`, `os` and `arch` are printed on the gate's first
line and not compared — a null `cpu_model`, which is what a host with no `/proc` writes, prints as
`unknown` — because both artifacts must be produced on the same host, and the gate does not refuse
a cross-host pair because the gate does not verify it. The gate exits 0 on a pass, 1 on a failed
item and 2 with one `bench-gate: refused: <detail>` line when it cannot decide; stdout stays empty.
The board's 50 000 events/s figure is a target tracked in #864, not a measurement, and the gate
does not compare with it.

Until a Postgres artifact exists the gate refuses rather than passing: it never fabricates a
baseline and never falls back to the LMDB band. No Postgres artifact is committed — every artifact
in the tree is LMDB — and CI writes `/tmp/bench-postgres.json` at 2000 events with one repeat and
discards it. With one LMDB artifact and no Postgres artifact the gate can only refuse. Run against
the committed 50000-event artifact, the gate stops at the corpus check:

$ node -r ts-node/register src/scripts/bench-gate.ts docs/rewrite/benchmarks/lmdb-50k-seed7.json docs/rewrite/benchmarks/lmdb-50k-seed7.json
bench-gate: refused: docs/rewrite/benchmarks/lmdb-50k-seed7.json: events 50000 is not a corpus the rule covers (covered: 1000000, 10000000)

exit code 2, with nothing on stdout. Reaching that line proves the file was read, parsed and
schema-checked; the comparison itself is proved by the fixtures in
`test/unit/scripts/bench-gate.spec.ts`, which spawn the gate on synthetic artifact pairs.

What the gate does not cover yet. The Postgres column needs #1132's host, and that host must be the
same host that produced the LMDB artifact; the 1000000 and 10000000 corpora need a host with the
disk and a quiet box. The rig refuses a corpus above `MAX_CORPUS_EVENTS = 200_000` unless
`--allow-large-corpus` is passed, and the rig defaults to one repeat, so a gated LMDB arm needs at
least `--repeats 3`, both arms need the same `--seed`, and the command is:

cargo run --release -p nostream-storage --example bench -- --backend lmdb \
--events 1000000 --seed 7 --repeats 3 --allow-large-corpus --verify --out /tmp/bench-lmdb-1m.json

At the recorded 1889.8 bytes per event the 10M corpus projects to 18898000000 bytes of `data.mdb`,
which does not fit this host's available memory, so D4's warm-read premise covers 1000000 and the warm-read premise stops holding at 10M.
The gate's own covered set stays 1000000 and 10000000 regardless of the rig's local ceiling: the
ceiling is a rig guard, not the gate's rule.

The artifact's `schema` integer is its layout version: the artifact this document reads — the
committed `docs/rewrite/benchmarks/lmdb-50k-seed7.json` — is schema 2, and a field whose meaning
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"db:migrate": "knex migrate:latest",
"db:migrate:rollback": "knex migrate:rollback",
"db:seed": "knex seed:run",
"bench:gate": "node -r ts-node/register src/scripts/bench-gate.ts",
"db:benchmark": "node --env-file-if-exists=.env -r ts-node/register src/scripts/benchmark-queries.ts",
"db:verify-index-impact": "node --env-file-if-exists=.env -r ts-node/register scripts/verify-index-impact.ts",
"pretest:unit": "pnpm run build && node -e \"require('fs').mkdirSync('.test-reports/unit', {recursive: true})\"",
Expand Down
Loading
Loading