ENG-10164 Add event-loop performance benchmark suite

Merged
AL
2mo ago
+5964-11

What changed

  • add a documented, owned performance-testing structure with deterministic CodSpeed benchmarks and opt-in wall-time suites
  • expand event processing into cold/warm, same-token, independent-token, and burst scenarios
  • benchmark event-loop queueing, task lifecycle, streaming updates, payloads, serialization, state deltas, state managers, routes, headers, proxies, and Var operations
  • add wall-time event-loop attribution with loop-lag percentiles, queue/task gauges, 80/20 hot-token load, handler-shape coverage, ordering checks, trace output, and failure diagnostics
  • add manual real-Redis latency/contention, production Socket.IO load, memory retention, compiler/hot-reload lifecycle, and browser/bundle suites
  • run event-loop and browser smoke coverage in the existing PR performance workflow

Review fixes

  • make loop-probe reset coverage independent of platform timer resolution
  • preserve every repeated pipeline-stage pair for streaming handlers
  • keep the event loop's default executor usable after load-client runs
  • exercise the real BaseStateToken hierarchy and persisted warm-state path in memory and Redis benchmarks
  • measure hot reload through one persistent reflex run --backend-only watcher and an observable live source-version endpoint

Review fixes (round 2)

  • drop the out-of-scope nightly workflow, comparison script, and PERFORMANCE.md
  • guard the Redis helper: REFLEX_PERF_REDIS_URL must name an explicit db index and the database must be empty; cleanup is exception-safe
  • measure the bundle budget against the shipped client bundle (.web/build/client, measured 238 KB gzip) instead of all of .web; 500 KB budget overridable via REFLEX_PERFORMANCE_JS_GZIP_BUDGET
  • bound all PR-smoke event-future waits with scale-aware timeouts and give the benchmarks CI job a timeout-minutes
  • benchmark real product code: reflex.app._decode_asgi_headers (extracted, behavior identical) instead of a local copy, and re.purge() per round so route-table construction is actually cold
  • move processor lifecycle out of measured regions, assert shutdown drain actually drains, and raise the drain budget so instrumentation slowdown cannot flip drain into cancel
  • de-flake the wall-time suites: token-cleanup polling instead of a fixed sleep, concurrency-scaled client timeouts with one retry, holder-process kill fallback, untraced warmup cycles before the leak gate, dev-backend child sweep + port-race retry
  • move the browser smoke into its own CI job and upload performance smoke artifacts from both jobs

Benchmark model

tests/benchmarks/ contains deterministic CPU work suitable for CodSpeed instrumentation. tests/performance/ contains wall-time, service, process, memory, compiler, load, and browser scenarios; it is skipped unless --run-performance is supplied.

Shared schema-v1 reporting, environment metadata, percentiles, loop probes, pipeline traces, state/app fixtures, Redis isolation, Socket.IO clients, and diagnostics live under tests/benchmarks/support/.

CI scope

This draft adds pull-request smoke coverage to the existing performance workflow, plus artifact upload of the smoke reports. Per scope decision, it intentionally excludes all other former step-4 work: nightly or release workflows, automated profiler collection, runner validation, comparison tooling, and standalone external-load tooling.

Validation

  • uv run pytest tests/benchmarks --codspeed -q — 88 passed
  • uv run pytest tests/units -q — 6645 passed, 78.04% coverage
  • uv run pytest tests/performance -q — skipped by default; event-loop and memory smoke ran for real (2 passed), plus a real production export to measure the shipped bundle
  • Ruff format/check passed; Pyright passed with 0 errors; workflow YAML validated

Review notes

The wall-time suites emit measurements without performance thresholds; regression gating should only be enabled after enough comparable baseline runs establish variance. Two narrow correctness gates are intentional: the shipped-bundle gzip budget (2x measured headroom, env-overridable) and the loop-probe detection bound (2 ms against a deliberately induced 10 ms block).