From 8a51372f188a2f7a471bbe127849177fd00d7d19 Mon Sep 17 00:00:00 2001 From: Mark Russell Date: Thu, 5 Nov 2026 18:55:21 -0500 Subject: [PATCH 1/3] bench(client): the cost of the live limits at their edges (CLI-101) A bench with one arm and its A/A pair, each case in a fresh process with --expose-gc: the heap of a key at 256 and 16,384 bytes, the heap of a topic at 4,096 topics, the time of live.set at both byte edges, a snapshot of a full topic, and one window event of a full topic to 1 and 10 subscribers. The workflow live-limits only reports, on a dispatch. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/live-limits.yml | 115 ++++++++++++++ packages/client/bench/limits-arm.ts | 200 +++++++++++++++++++++++++ packages/client/bench/limits-cases.ts | 69 +++++++++ packages/client/bench/limits-report.ts | 62 ++++++++ packages/client/bench/limits-run.ts | 34 +++++ 5 files changed, 480 insertions(+) create mode 100644 .github/workflows/live-limits.yml create mode 100644 packages/client/bench/limits-arm.ts create mode 100644 packages/client/bench/limits-cases.ts create mode 100644 packages/client/bench/limits-report.ts create mode 100644 packages/client/bench/limits-run.ts diff --git a/.github/workflows/live-limits.yml b/.github/workflows/live-limits.yml new file mode 100644 index 000000000..64903ccb2 --- /dev/null +++ b/.github/workflows/live-limits.yml @@ -0,0 +1,115 @@ +name: Live limits + +# The cost of the three limits of the live state at their edges (roadmap CLI-101): the heap of a key and of a topic, +# the time of a set, a snapshot of a full topic, and one window event of a full topic to 1 and 10 subscribers. Each +# sample is a fresh process with --expose-gc. Each job has an A/A pair. The report pools the jobs. +# +# The workflow only reports: only a dispatch starts it, and no ruleset needs its checks. + +on: + workflow_dispatch: + inputs: + prediction: + description: The id of the prediction that this run scores. + required: true + type: string + runs: + description: The jobs, from 1 to 9. Five or more for a limit. + type: string + default: "5" + rounds: + description: The rounds of each job, 6 or more for the interval of a case. + type: string + default: "6" + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + plan: + runs-on: ubuntu-latest + timeout-minutes: 5 + outputs: + runs: ${{ steps.plan.outputs.runs }} + steps: + - name: Plan the jobs + id: plan + env: + RUNS: ${{ inputs.runs }} + ROUNDS: ${{ inputs.rounds }} + run: | + [[ "$RUNS" =~ ^[1-9]$ ]] || { echo "runs must be a number from 1 to 9, not $RUNS"; exit 1; } + [[ "$ROUNDS" =~ ^[0-9]+$ ]] && (( ROUNDS >= 6 )) || { echo "rounds must be 6 or more, not $ROUNDS"; exit 1; } + echo "runs=[$(seq -s, 1 "$RUNS")]" >> "$GITHUB_OUTPUT" + + bench: + needs: plan + name: live limits (job ${{ matrix.job }}) + runs-on: ubuntu-latest + timeout-minutes: 45 + strategy: + fail-fast: false + # The runners are shared by all repositories of the account (the rule runner-budget) + max-parallel: 3 + matrix: + job: ${{ fromJSON(needs.plan.outputs.runs) }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + - run: pnpm install --frozen-lockfile --filter mark1russell7-monorepo --filter "@procedures/client..." + - name: Run the job + working-directory: packages/client + env: + JOB: ${{ matrix.job }} + ROUNDS: ${{ inputs.rounds }} + run: node --import tsx bench/limits-run.ts --job "$JOB" --rounds "$ROUNDS" --out "bench-out/job-$JOB.json" + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: live-limits-job-${{ matrix.job }} + path: | + packages/client/bench-out/job-${{ matrix.job }}.json + packages/client/bench-out/job-${{ matrix.job }}.json.counts.jsonl + if-no-files-found: error + + report: + needs: bench + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + - run: pnpm install --frozen-lockfile --filter mark1russell7-monorepo --filter "@procedures/client..." + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: live-limits-job-* + merge-multiple: true + path: packages/client/bench-out + - name: Pool the jobs + working-directory: packages/client + env: + RUN: ${{ github.run_id }} + PREDICTION: ${{ inputs.prediction }} + run: | + node --import tsx bench/limits-report.ts --jobs bench-out --out limits-report.json --run "$RUN" + { echo "Prediction: $PREDICTION"; echo '```json'; cat limits-report.json; echo '```'; } >> "$GITHUB_STEP_SUMMARY" + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: live-limits-report + path: packages/client/limits-report.json + if-no-files-found: error diff --git a/packages/client/bench/limits-arm.ts b/packages/client/bench/limits-arm.ts new file mode 100644 index 000000000..1f6a4bf39 --- /dev/null +++ b/packages/client/bench/limits-arm.ts @@ -0,0 +1,200 @@ +/** + * One child of the bench of the live limits (roadmap CLI-101): `--case --job --counts `. The child + * needs `--expose-gc`. It prints the JSON line of `freshRun`, and adds its record to the counts file. + * + * The frame of a subscriber stops at `JSON.stringify`: no socket is in this bench. The time of a real socket write + * is not measured. + */ + +import { appendFileSync } from "node:fs"; +import { getHeapStatistics } from "node:v8"; +import { parseArgs } from "node:util"; +import { median } from "@mark1russell7/rigor-statistics"; +import { minutes } from "../src/duration.js"; +import { createEventBus } from "../src/events/bus.js"; +import type { LiveState } from "../src/events/live-state.js"; +import { EventPublisher } from "../src/events/publisher.js"; +import type { EventItem } from "../src/events/remote.js"; +import { invokeProcedure } from "../src/procedures/invoke.js"; +import type { AnyProcedure } from "../src/procedures/types.js"; +import { KEYS, VALUE_BYTES, parseCase, valueText } from "./limits-cases.js"; + +const { values } = parseArgs({ options: { case: { type: "string" }, job: { type: "string" }, counts: { type: "string" } } }); +if (values.case === undefined || values.counts === undefined) throw new RangeError("this child needs --case and --counts"); +const gc = (globalThis as { gc?: () => void }).gc; +if (!gc) throw new Error("this child needs --expose-gc"); +const id = values.case; +const spec = parseCase(id); + +const TOPIC = "live.room"; +const bus = createEventBus(); +const publisher = new EventPublisher({ bus, topics: ["live.*"], live: { expiresAfter: minutes(30), admitSet: () => ({}) } }); +const live = publisher.live as LiveState; + +/** The procedure of the live state with the given last segment. */ +const procedureOf = (name: "set" | "snapshot"): AnyProcedure => live.procedures.find((procedure) => procedure.path[1] === name)!; + +/** The heap after a full collection, in bytes. */ +function heapNow(): number { + gc!(); + gc!(); + return process.memoryUsage().heapUsed; +} + +/** This function fills topics with keys, each with its own value. */ +function fill(topics: number, keys: number, bytes: number, round = 0): void { + for (let t = 0; t < topics; t++) { + const topic = topics === 1 ? TOPIC : `${TOPIC}${t}`; + for (let k = 0; k < keys; k++) live.set(topic, `k${k}`, JSON.parse(valueText(bytes, (round * topics + t) * keys + k))); + } +} + +const record: Record = { case: id, arm: "edge", job: values.job ?? "0", heapLimit: getHeapStatistics().heap_size_limit }; +let ms: number; +let hash: string; + +const nextTask = (): Promise => new Promise((resolve) => setImmediate(resolve)); +const sleep = (time: number): Promise => new Promise((resolve) => setTimeout(resolve, time)); + +switch (spec.kind) { + case "heap": + case "topics": { + const topics = spec.topics; + const keys = spec.kind === "heap" ? KEYS : 1; + const bytes = spec.kind === "heap" ? spec.bytes : 256; + const before = heapNow(); + const start = performance.now(); + fill(topics, keys, bytes); + ms = performance.now() - start; + const after = heapNow(); + record["keys"] = topics * keys; + record["heapBytes"] = after - before; + record["heapPerKey"] = (after - before) / (topics * keys); + record["heapPerTopic"] = (after - before) / topics; + hash = String(live.size); + break; + } + case "set": { + const set = procedureOf("set"); + const count = spec.bytes > 1024 ? 10_000 : 40_000; + const pool = Array.from({ length: 256 }, (_, n) => JSON.parse(valueText(spec.bytes, n, spec.wide)) as unknown); + const call = (n: number): Promise => invokeProcedure(set, { topic: TOPIC, key: `k${n % 100}`, value: pool[n % pool.length] }); + for (let n = 0; n < 2_000; n++) await call(n); + const start = performance.now(); + for (let n = 0; n < count; n++) await call(n); + ms = performance.now() - start; + record["calls"] = count; + record["setUs"] = (ms * 1000) / count; + record["valueBytes"] = Buffer.byteLength(valueText(spec.bytes, 0, spec.wide)); + hash = String(live.snapshot(TOPIC).length); + break; + } + case "snapshot": { + fill(1, KEYS, VALUE_BYTES); + const snapshot = procedureOf("snapshot"); + const calls: number[] = []; + const texts: number[] = []; + let bytes = 0; + for (let n = 0; n < 7; n++) { + const start = performance.now(); + const output = await invokeProcedure(snapshot, { topic: TOPIC }); + const called = performance.now(); + const text = JSON.stringify(output.kind === "value" ? output.value : null); + const done = performance.now(); + bytes = Buffer.byteLength(text); + // The first two are the warm-up + if (n >= 2) { + calls.push(called - start); + texts.push(done - called); + } + } + record["snapshotBytes"] = bytes; + record["snapshotCallMs"] = median(calls); + record["stringifyMs"] = median(texts); + ms = median(calls.map((call, n) => call + texts[n]!)); + // The digits of the times can change the bytes: the hash is the count of the entries + hash = String(live.snapshot(TOPIC).length); + break; + } + case "window": { + let frames = 0; + let target = 0; + let delivered: (() => void) | undefined; + let bytes = 0; + const controller = new AbortController(); + for (let s = 0; s < spec.subscribers; s++) { + const output = await invokeProcedure(publisher.procedure, { topics: [TOPIC] }, { signal: controller.signal }); + if (output.kind !== "stream") throw new Error("events.subscribe gives a stream"); + const items = output.items as AsyncGenerator; + await items.next(); + void (async () => { + for await (const item of items) { + bytes += JSON.stringify(item).length; + if (++frames === target) delivered?.(); + } + })(); + } + let published = 0; + let eventKeys = 0; + bus.on<{ set?: unknown[] }>(TOPIC, (event) => { + if ((event.set?.length ?? 0) > 1) { + published = performance.now(); + eventKeys = event.set!.length; + } + }); + const pools = [0, 1].map((round) => Array.from({ length: KEYS }, (_, k) => JSON.parse(valueText(VALUE_BYTES, round * KEYS + k)) as unknown)); + const blocks: number[] = []; + const totals: number[] = []; + for (let round = 0; round < 5; round++) { + const pool = pools[round % 2]!; + // The first set on an idle topic goes at once and opens the window + frames = 0; + target = spec.subscribers; + let first = new Promise((resolve) => (delivered = resolve)); + live.set(TOPIC, "k0", pool[0]); + await first; + // The other keys wait in the window. One event at its end carries them all + frames = 0; + target = spec.subscribers; + first = new Promise((resolve) => (delivered = resolve)); + for (let k = 1; k < KEYS; k++) live.set(TOPIC, `k${k}`, pool[k]); + await nextTask(); + let longest = 0; + let probing = true; + let last = performance.now(); + const probe = (): void => { + const now = performance.now(); + longest = Math.max(longest, now - last); + last = now; + if (probing) setImmediate(probe); + }; + setImmediate(probe); + await first; + const end = performance.now(); + await nextTask(); + await nextTask(); + probing = false; + // The first two rounds are the warm-up + if (round >= 2) { + blocks.push(longest); + totals.push(end - published); + } + // The window after the event ends, so the topic is idle again + await sleep(400); + } + controller.abort(); + record["eventKeys"] = eventKeys; + record["blockMs"] = median(blocks); + record["blockMaxMs"] = Math.max(...blocks); + record["deliverMs"] = median(totals); + record["frameBytes"] = bytes; + ms = Math.max(...blocks); + hash = String(eventKeys); + break; + } +} + +record["ms"] = ms; +appendFileSync(values.counts, `${JSON.stringify(record)}\n`); +console.log(JSON.stringify({ ms, hash })); +process.exit(0); diff --git a/packages/client/bench/limits-cases.ts b/packages/client/bench/limits-cases.ts new file mode 100644 index 000000000..d4bcea064 --- /dev/null +++ b/packages/client/bench/limits-cases.ts @@ -0,0 +1,69 @@ +/** + * The cases of the bench of the live limits (roadmap CLI-101). Each case reads one limit of the live state at its + * edge. The limits are the keys of a topic, the bytes of a value and the topics of a state. + * + * - `heap-`: one topic with 1,024 keys, each with a value of the given JSON bytes. The record gives the heap + * of each key. + * - `heap-16x16384`: 16 full topics, so the heap of a key is read at a larger heap too. + * - `topics-4096`: 4,096 topics with one key of 256 bytes each. The record gives the heap of each topic. + * - `set-` and `set-wide`: the time of one call of `live.set`. The value of `set-wide` has 3-byte characters + * up to the byte limit. + * - `snapshot-full`: the call of `live.snapshot` on a full topic, and the JSON of its output. + * - `window-s`: one window event of a full topic, delivered to n subscribers with a JSON frame for each. + */ + +/** The ids of the cases. */ +export const LIMITS_CASES: readonly string[] = [ + "heap-256", + "heap-16384", + "heap-16x16384", + "topics-4096", + "set-256", + "set-16384", + "set-wide", + "snapshot-full", + "window-s1", + "window-s10", +]; + +/** The default limits of the live state, as the bench reads them. */ +export const KEYS = 1024; +export const VALUE_BYTES = 16384; +export const TOPICS = 4096; + +/** A case of the bench. */ +export type LimitsCase = + | { readonly kind: "heap"; readonly topics: number; readonly bytes: number } + | { readonly kind: "topics"; readonly topics: number } + | { readonly kind: "set"; readonly bytes: number; readonly wide: boolean } + | { readonly kind: "snapshot" } + | { readonly kind: "window"; readonly subscribers: number }; + +/** This function reads a case from its id. */ +export function parseCase(id: string): LimitsCase { + const heap = /^heap-(?:(\d+)x)?(\d+)$/.exec(id); + if (heap) return { kind: "heap", topics: Number(heap[1] ?? 1), bytes: Number(heap[2]) }; + const topics = /^topics-(\d+)$/.exec(id); + if (topics) return { kind: "topics", topics: Number(topics[1]) }; + if (id === "set-wide") return { kind: "set", bytes: VALUE_BYTES, wide: true }; + const set = /^set-(\d+)$/.exec(id); + if (set) return { kind: "set", bytes: Number(set[1]), wide: false }; + if (id === "snapshot-full") return { kind: "snapshot" }; + const window = /^window-s(\d+)$/.exec(id); + if (window) return { kind: "window", subscribers: Number(window[1]) }; + throw new RangeError(`no case ${id}`); +} + +/** + * This function gives the JSON text of a value `{"t": "..."}` with the given bytes at most, unique for `id`. A value + * with `wide` has the 3-byte character U+20AC. The text goes through `JSON.parse`, as a value from the wire does, so + * each value has its own flat string. + */ +export function valueText(bytes: number, id: number, wide = false): string { + const tag = id.toString(36).padStart(4, "0"); + // `{"t":"` and `"}` are 8 bytes + const room = bytes - 8 - tag.length; + if (room < 0) throw new RangeError(`a value of ${bytes} bytes has no room for its id`); + const fill = wide ? "€".repeat(Math.floor(room / 3)) : "x".repeat(room); + return `{"t":"${tag}${fill}"}`; +} diff --git a/packages/client/bench/limits-report.ts b/packages/client/bench/limits-report.ts new file mode 100644 index 000000000..47a76c50c --- /dev/null +++ b/packages/client/bench/limits-report.ts @@ -0,0 +1,62 @@ +/** + * The report of the bench of the live limits (roadmap CLI-101): `--jobs --out [--run ]`. For each + * case, the report gives the median and the worst of each measure over the children, and the spread of the A/A pair. + * It also gives the full topics that fit under the heap limit. The report only reports: no limit comes from one run. + */ + +import { readdirSync, readFileSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { parseArgs } from "node:util"; +import { aaBand, type PairedTime } from "@mark1russell7/rigor-bench"; +import { median } from "@mark1russell7/rigor-statistics"; +import { KEYS, LIMITS_CASES, TOPICS } from "./limits-cases.js"; + +const { values } = parseArgs({ options: { jobs: { type: "string" }, out: { type: "string" }, run: { type: "string" } } }); +if (values.jobs === undefined || values.out === undefined) throw new RangeError("this report needs --jobs and --out"); + +const folder = values.jobs; +const files = readdirSync(folder); +const rows = files + .filter((name) => name.endsWith(".counts.jsonl")) + .flatMap((name) => readFileSync(join(folder, name), "utf8").trim().split("\n")) + .filter((line) => line.length > 0) + .map((line) => JSON.parse(line) as Record & { case: string; heapLimit: number }); +const jobs = files + .filter((name) => name.endsWith(".json")) + .map((name) => JSON.parse(readFileSync(join(folder, name), "utf8")) as { job: string; env: unknown; aa: PairedTime[] }); + +const round = (value: number, digits = 3): number => Number(value.toFixed(digits)); + +/** The median and the worst of each numeric field of the records of a case. */ +function measures(own: ReadonlyArray>): Record { + const fields = new Set(own.flatMap((row) => Object.keys(row).filter((key) => typeof row[key] === "number"))); + return Object.fromEntries([...fields].sort().map((field) => { + const found = own.map((row) => row[field]).filter((value): value is number => typeof value === "number"); + return [field, { median: round(median(found)), min: round(Math.min(...found)), max: round(Math.max(...found)) }]; + })); +} + +const cases = LIMITS_CASES.map((name) => { + const own = rows.filter((row) => row.case === name); + return { case: name, children: own.length, measures: own.length > 0 ? measures(own) : {} }; +}); + +const perKey = cases.find((entry) => entry.case === "heap-16384")?.measures["heapPerKey"]?.median; +const heapLimit = rows.length > 0 ? median(rows.map((row) => row.heapLimit)) : 0; +const fullTopic = perKey === undefined ? undefined : perKey * KEYS; +const band = aaBand(jobs.flatMap((job) => job.aa)); + +const report = { + schema: "live-limits-bench/1", + run: values.run ?? "", + children: rows.length, + heapLimit, + fullTopicBytes: fullTopic === undefined ? null : Math.round(fullTopic), + fullTopicsFit: fullTopic === undefined ? null : Math.floor(heapLimit / fullTopic), + fullStateBytes: fullTopic === undefined ? null : Math.round(fullTopic * TOPICS), + aa: { noise: round(band.noise, 4), high: round(band.high), median: round(band.median), cases: band.cases }, + cases, + env: jobs.map(({ job, env }) => ({ job, env })), +}; +writeFileSync(values.out, `${JSON.stringify(report, null, 2)}\n`); +console.log(`The bench of the live limits: ${rows.length} children in ${LIMITS_CASES.length} cases.`); diff --git a/packages/client/bench/limits-run.ts b/packages/client/bench/limits-run.ts new file mode 100644 index 000000000..86ccb74bf --- /dev/null +++ b/packages/client/bench/limits-run.ts @@ -0,0 +1,34 @@ +/** + * One job of the bench of the live limits (roadmap CLI-101): `--job --rounds --out `. Each case of each + * round runs in a fresh process. The bench has one arm: the job gives the A/A pair of it, for the noise of each case. + * The records of each child go to `.counts.jsonl`. + */ + +import { mkdirSync, writeFileSync } from "node:fs"; +import { dirname } from "node:path"; +import { parseArgs } from "node:util"; +import { benchEnv, freshRun } from "@mark1russell7/rigor-bench/fresh"; +import { LIMITS_CASES } from "./limits-cases.js"; + +const { values } = parseArgs({ options: { job: { type: "string" }, rounds: { type: "string" }, out: { type: "string" } } }); +const job = Number(values.job); +const rounds = Number(values.rounds); +if (!Number.isInteger(job) || !Number.isInteger(rounds) || values.out === undefined) throw new RangeError("this job needs --job, --rounds and --out"); + +mkdirSync(dirname(values.out), { recursive: true }); +const counts = `${values.out}.counts.jsonl`; + +const run = await freshRun({ + base: { + id: "edge", + command: [process.execPath, "--expose-gc", "--import", "tsx", "bench/limits-arm.ts", "--job", String(job), "--counts", counts], + }, + candidates: [], + cases: LIMITS_CASES, + rounds, + roundOffset: (job - 1) * rounds, + job: String(job), + timeoutMs: 120_000, +}); + +writeFileSync(values.out, `${JSON.stringify({ job: String(job), env: benchEnv(), aa: run.pairs["edge~aa"] })}\n`); From 6e1b7e36cc2950f86027e31b52f22995aafacd50 Mon Sep 17 00:00:00 2001 From: Mark Russell Date: Thu, 5 Nov 2026 19:02:39 -0500 Subject: [PATCH 2/3] bench(client): the live limits at 1 MiB a topic, the default of the fix (CLI-101) snapshot-k64, window-s10-k64 and window-s100-k64 read a topic of 64 keys of 16,384 bytes. The report gives the heap of a topic and of a state at the limits of bytes of #465, and the share of the heap limit. Co-Authored-By: Claude Opus 5.5 --- packages/client/bench/limits-arm.ts | 7 ++++--- packages/client/bench/limits-cases.ts | 20 +++++++++++++++----- packages/client/bench/limits-report.ts | 6 +++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/client/bench/limits-arm.ts b/packages/client/bench/limits-arm.ts index 1f6a4bf39..e6471b5bc 100644 --- a/packages/client/bench/limits-arm.ts +++ b/packages/client/bench/limits-arm.ts @@ -90,7 +90,7 @@ switch (spec.kind) { break; } case "snapshot": { - fill(1, KEYS, VALUE_BYTES); + fill(1, spec.keys, VALUE_BYTES); const snapshot = procedureOf("snapshot"); const calls: number[] = []; const texts: number[] = []; @@ -142,7 +142,8 @@ switch (spec.kind) { eventKeys = event.set!.length; } }); - const pools = [0, 1].map((round) => Array.from({ length: KEYS }, (_, k) => JSON.parse(valueText(VALUE_BYTES, round * KEYS + k)) as unknown)); + const keys = spec.keys; + const pools = [0, 1].map((round) => Array.from({ length: keys }, (_, k) => JSON.parse(valueText(VALUE_BYTES, round * keys + k)) as unknown)); const blocks: number[] = []; const totals: number[] = []; for (let round = 0; round < 5; round++) { @@ -157,7 +158,7 @@ switch (spec.kind) { frames = 0; target = spec.subscribers; first = new Promise((resolve) => (delivered = resolve)); - for (let k = 1; k < KEYS; k++) live.set(TOPIC, `k${k}`, pool[k]); + for (let k = 1; k < keys; k++) live.set(TOPIC, `k${k}`, pool[k]); await nextTask(); let longest = 0; let probing = true; diff --git a/packages/client/bench/limits-cases.ts b/packages/client/bench/limits-cases.ts index d4bcea064..105fb3eb7 100644 --- a/packages/client/bench/limits-cases.ts +++ b/packages/client/bench/limits-cases.ts @@ -10,6 +10,8 @@ * up to the byte limit. * - `snapshot-full`: the call of `live.snapshot` on a full topic, and the JSON of its output. * - `window-s`: one window event of a full topic, delivered to n subscribers with a JSON frame for each. + * - `snapshot-k64` and `window-s-k64`: the same at 64 keys of 16,384 bytes. That is 1 MiB, the default limit of + * the bytes of a topic in the fix of the roadmap item. */ /** The ids of the cases. */ @@ -24,20 +26,26 @@ export const LIMITS_CASES: readonly string[] = [ "snapshot-full", "window-s1", "window-s10", + "snapshot-k64", + "window-s10-k64", + "window-s100-k64", ]; /** The default limits of the live state, as the bench reads them. */ export const KEYS = 1024; export const VALUE_BYTES = 16384; export const TOPICS = 4096; +/** The limits of the bytes of a topic and of a state in the fix, as the report reads them. */ +export const TOPIC_BYTES = 1_048_576; +export const STATE_BYTES = 67_108_864; /** A case of the bench. */ export type LimitsCase = | { readonly kind: "heap"; readonly topics: number; readonly bytes: number } | { readonly kind: "topics"; readonly topics: number } | { readonly kind: "set"; readonly bytes: number; readonly wide: boolean } - | { readonly kind: "snapshot" } - | { readonly kind: "window"; readonly subscribers: number }; + | { readonly kind: "snapshot"; readonly keys: number } + | { readonly kind: "window"; readonly subscribers: number; readonly keys: number }; /** This function reads a case from its id. */ export function parseCase(id: string): LimitsCase { @@ -48,9 +56,11 @@ export function parseCase(id: string): LimitsCase { if (id === "set-wide") return { kind: "set", bytes: VALUE_BYTES, wide: true }; const set = /^set-(\d+)$/.exec(id); if (set) return { kind: "set", bytes: Number(set[1]), wide: false }; - if (id === "snapshot-full") return { kind: "snapshot" }; - const window = /^window-s(\d+)$/.exec(id); - if (window) return { kind: "window", subscribers: Number(window[1]) }; + if (id === "snapshot-full") return { kind: "snapshot", keys: KEYS }; + const snapshot = /^snapshot-k(\d+)$/.exec(id); + if (snapshot) return { kind: "snapshot", keys: Number(snapshot[1]) }; + const window = /^window-s(\d+)(?:-k(\d+))?$/.exec(id); + if (window) return { kind: "window", subscribers: Number(window[1]), keys: Number(window[2] ?? KEYS) }; throw new RangeError(`no case ${id}`); } diff --git a/packages/client/bench/limits-report.ts b/packages/client/bench/limits-report.ts index 47a76c50c..449857eef 100644 --- a/packages/client/bench/limits-report.ts +++ b/packages/client/bench/limits-report.ts @@ -9,7 +9,7 @@ import { join } from "node:path"; import { parseArgs } from "node:util"; import { aaBand, type PairedTime } from "@mark1russell7/rigor-bench"; import { median } from "@mark1russell7/rigor-statistics"; -import { KEYS, LIMITS_CASES, TOPICS } from "./limits-cases.js"; +import { KEYS, LIMITS_CASES, STATE_BYTES, TOPICS, TOPIC_BYTES, VALUE_BYTES } from "./limits-cases.js"; const { values } = parseArgs({ options: { jobs: { type: "string" }, out: { type: "string" }, run: { type: "string" } } }); if (values.jobs === undefined || values.out === undefined) throw new RangeError("this report needs --jobs and --out"); @@ -54,6 +54,10 @@ const report = { fullTopicBytes: fullTopic === undefined ? null : Math.round(fullTopic), fullTopicsFit: fullTopic === undefined ? null : Math.floor(heapLimit / fullTopic), fullStateBytes: fullTopic === undefined ? null : Math.round(fullTopic * TOPICS), + // The heap of a state at the limits of bytes of the fix: the heap of a key at the byte edge, for each value of that size + topicLimitHeap: perKey === undefined ? null : Math.round((perKey * TOPIC_BYTES) / VALUE_BYTES), + stateLimitHeap: perKey === undefined ? null : Math.round((perKey * STATE_BYTES) / VALUE_BYTES), + stateLimitShareOfHeap: perKey === undefined || heapLimit === 0 ? null : round((perKey * STATE_BYTES) / VALUE_BYTES / heapLimit, 4), aa: { noise: round(band.noise, 4), high: round(band.high), median: round(band.median), cases: band.cases }, cases, env: jobs.map(({ job, env }) => ({ job, env })), From a5099c3c0f6909fd072460a71abd24842d08ac87 Mon Sep 17 00:00:00 2001 From: Mark Russell Date: Thu, 5 Nov 2026 20:09:26 -0500 Subject: [PATCH 3/3] chore(changeset): the live-limits bench has no release (CLI-101) Co-Authored-By: Claude Opus 5.5 --- .changeset/live-limits-bench.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .changeset/live-limits-bench.md diff --git a/.changeset/live-limits-bench.md b/.changeset/live-limits-bench.md new file mode 100644 index 000000000..23c7958c0 --- /dev/null +++ b/.changeset/live-limits-bench.md @@ -0,0 +1,4 @@ +--- +--- + +The bench of the cost of the live limits (roadmap CLI-101). It changes only bench files, so it has no release.