# Store stress rig `cmd/torly-stress` drives `pkg/store` directly, without WebSocket or signature verification. It is meant to measure the database engine, not the network path. ## Running ```sh go run ./cmd/torly-stress \ -events 100000 -authors 1000 \ -iterations 20 -duration 10s -readers 8 -writers 2 \ -data-dir /tmp/torly-stress ``` The tool seeds a deterministic event set, warms the query plans, reports p50, p95 and p99 latency for point, kind, author, tag, search and graph reads, runs a mixed reader/writer phase, compacts the index runs, and finally measures close and reopen time. It prints one JSON summary on stdout and progress on stderr. Use `-keep` (or an explicit `-data-dir`) to retain the database for inspection. `cmd/torly-graph-stats` audits the graph index without opening the store: ```sh go run ./cmd/torly-graph-stats \ -idx /home/mleku/.local/share/torly/db/idx -top 20 ``` It reports author events, p-tag edges and e-tag edges per event kind, with per-event p/e percentiles and the number of follow lists above 512 entries. ## Local baseline On an 8-core local host, with 100,000 synthetic events and 1,000 authors, after the bounded reverse query scan: ```text seed 100,000 events in 58s (1,718 events/s) id p50 0.0 ms kind 1, limit 1 p50 0.0 ms kind 1, limit 100 p50 0.2 ms kind 1, limit 5000 p50 11.4 ms author, limit 20 p50 0.1 ms tag, limit 20 p50 0.2 ms search, limit 100 p50 111 ms empty, limit 100 p50 0.2 ms graph pp depth 2 p50 0.2 ms mixed 8r/2w, 10s p50 1.4 ms, p95 2.5 ms compact 184 ms close / reopen 532 / 126 ms ``` At 500,000 events, the same kind-1 and empty filters remain sub-millisecond for limits up to 100, and `limit:5000` takes about 14 ms. Search remains a full word-index candidate walk and takes about 845 ms, so it is the next obvious engine target. The mixed phase still serializes on the engine mutex, but once broad kind queries stopped scanning the whole index, it sustained roughly 3,600 saves in five seconds locally instead of a few hundred. The production relay remains slower for these same queries because its database domain has a queue of client requests and still serves broad `limit:5000` and search requests. ## Production graph audit The production graph index was dominated by non-graph event kinds before the kind policy was introduced. The largest populations were kind 1059 (520k events), kind 7 (343k), kind 1 (230k), kind 3 (1.1k live at audit time), kind 6 (107k) and kind 1111 (10k). Restricting maintenance to kinds 1, 3, 6, 7 and 1111 leaves roughly 693,000 author events instead of 1,495,807 raw records. After `torly reindex-graph` on 2026-10-24: ```text kind 3 2,752 events 1,437,336 p edges 975 lists above 512 p tags kind 7 343,440 events 354,879 p edges 151,208 e edges kind 1 229,790 events 172,398 p edges 79,700 e edges kind 6 107,282 events 17,869 p edges 57,575 e edges kind 1111 10,199 events 15,009 p edges 3,400 e edges total 693,463 author events 1,997,491 p edges 291,883 e edges ``` The kind-3 figure is large because follow lists are deliberately uncapped; lists above 512 p tags are queued to the low-priority graph backlog rather than dropped. The reindex took 8m41s for 1,462,659 live events. It also removes stale raw records from replaced/deleted events, so subsequent audits are meaningful. ## What was found and fixed 1. **Index run explosion.** Reads called `flushPending`, creating a new run per index on every query. 367k run files made every scan open and merge tens of thousands of files. Reads no longer flush; runs are capped at 32 per index and compacted without rewriting the main index; `torly compact` repairs old stores. 2. **Dropped query responses.** A 64-frame worker outbound buffer silently lost EVENT/EOSE frames on responses above 64 events. Overflow is now queued per connection and flushed by the network domain. 3. **Graph index scope.** Graph records are now restricted to kinds 1, 3 and 1111. Follow lists are uncapped; lists above 512 p tags are deferred to the graph backlog. Other kinds remain fully queryable over NIP-01. 4. **Broad query scans.** Kind and empty queries still scan the candidate timestamp range. At 100k events that is tens of milliseconds; at production scale it is hundreds of milliseconds. This is the next optimization target. `torly reindex-graph` clears the graph indexes and rebuilds them from live events. Run it while the service is stopped; it is the migration path after a graph policy change.