0801. Load generator: raw-socket Node workers, ADR 0020 load model, latency timestamps in payloads
Status: Accepted (2026-11-27). Work package H (load test). Code: tools/loadtest/.
Context
WP-H must prove ADR 0020’s capacity table with 100k+ WebSockets. That means a generator cheap enough that it is not the bottleneck, behaving like the real sync client: auth, hello/subscribe, resends, reconnects. It must measure latency end to end across processes, and check validation verdicts, not only throughput. ADR 0020 gives the load model in prose (“20% editors”, “80% view”), which leaves room for interpretation.
Decision
Generator. A coordinator (run.ts) spawns N worker processes (worker.ts), JSON lines over stdin/stdout.
- Each worker drives a few thousand clients over raw
node:net sockets with a minimal client framing: zero mask key, in-place frame parsing. Per-connection state lives in typed arrays.
- Outgoing payloads are built with the reference codec (
shared/src/codec.ts). Incoming frames are read in place by small parsers, tested against decodeMsg.
- Clients behave like
packages/sync-client:
- Upgrade with a realtime token minted from
LINORA_TOKEN_KEYS (server-common helpers).
hello, with the clientGrant on reconnect; subscribe(sinceSeq = last seen seq, pendingFromClientSeq).
- After
busy/rate_limited they resend from the floor (ADR 0202), and they resend unacked batches after reconnects.
- Reconnect with full-jitter backoff (
RECONNECT_BASE_MS..RECONNECT_MAX_MS), or at going_away.retryAfterMs.
- App-level PING every 25 s.
Load model (profile adr0020, scaled linearly below 100k).
- 20,000 rooms x 3, 1,000 x 30, 10 x 1,000; one file and page per room; one user per connection (the per-user clientId and op caps then never bind).
- Editors are exactly 20% of connections: 6 per 30-room, 2 presenters per 1,000-room, and the remainder as one editor in 13,980 of the 3-rooms.
- This keeps op fan-out near ADR 0020’s ~300-450k sends/s. Giving every 1,000-room 20% editors would add ~4M sends/s, which is not the model.
- About 15k rooms have an active editor, which matches ADR 0020’s ~300k presence room-ticks/s.
- Editors send 20 Hz cursors (selection every 5 s) and 2 batches/s of 5 ops at independent phases. Viewers send one presence every 30 s.
- Batches: 85% plain SETs; 10% structural (CREATE a scratch node, reparent the previous one into the editor’s frame, DELETE the one before); 5% contain one deliberately invalid op.
- Ten invalid kinds cover stateless and structural verdicts: NaN, out of range, foreign id, bad key, missing node, missing parent, reparent under a rect, delete root, prop not allowed, bad kind.
- A unit test checks every generated batch’s expected verdicts against
DocCore.apply. At run time every ACK is compared with the expectation: matched, missed, wrong reason, or unexpected.
Latency. Timestamps travel in the payloads, on a clock shared by the processes on one host (performance.timeOrigin + now(), relative to a run epoch).
- Encoding: two f32-exact integers, whole ms mod 1e6 and microseconds. Ops carry them as the X/Y of a SET on the editor’s anchor node (op 0 of every steady batch, a legal move). Presence carries them as the cursor.
- Receivers record into log-linear histograms (~6% buckets) per room class:
- op broadcast (author send to peer receive)
- ACK (author)
- presence (sender to peer; ignored for 1 s after
synced, because the joiner’s full-state batch carries old cursors)
- PONG RTT
- connect and reconnect time
- Server-side numbers come from
/metrics: ScanBatch/check ns/op, COPY ms, owner ACK ms, rates, RSS, CPU, and the container cgroup in Docker mode.
Report. Acceptance table first, then setup, ramp and idle memory (after a forced GC through the server’s SIGUSR2 hook, raw RSS shown too), per-window latency, server and generator tables, kill and storm timelines. The JSON next to it holds samples and histogram buckets. Presence is judged on rooms up to 200 subscribers; rooms above that tick every 200 ms by design (ADR 0007) and are reported separately. Runs below 100k mark scale-dependent checks n/a and show linear projections.
Scenarios.
steady: measurement window.
kill: SIGKILL, then compare every room’s highest acknowledged seq with page_ops (head and no gaps), restart, and time until all clients are live.
storm: SIGTERM drain with going_away, restart, time to all live.
Alternatives
- uWS.js as the client: v20.69 exposes no WebSocket client.
- Node’s built-in WebSocket: a full WHATWG object per socket and event dispatch per message. At 100k sockets that is several GB of generator heap and more CPU per frame than raw framing.
- External tools (k6, artillery, tsung): new dependencies, they cannot speak the binary codec without a second implementation, and they don’t check verdicts.
- Correlating sends and receives by id across processes instead of timestamps: needs a shared store at ~2M frames/s.
Consequences
- The generator costs about as much CPU per delivered frame as the server: ~0.5 core per 2,500 clients at full ADR 0020 rates on the reference machine. The full-rate 100k run needs ~20 generator cores, hence ADR 0802.
- The report flags generator saturation (worker CPU > 90% or event-loop lag p99 > 50 ms). Client-side latencies are then upper bounds; server metrics are unaffected.
- Cross-host latency needs synchronized clocks (chrony, < 1 ms). PONG RTT and the server’s owner-ACK histogram are clock-independent cross-checks.