Repository navigation
chore(bench): add streaming SSR benchmarks - #835
Conversation
📝 WalkthroughWalkthroughAdds a Vitest benchmark suite for server-streaming head rendering, including stream helpers, stream wrapping and cancellation cases, template preparation, and repeated suspense chunk rendering. ChangesStreaming SSR Benchmarks
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/unhead/test/bench/stream.bench.ts (1)
26-33: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueExplicitly release stream reader locks.
Although readers are garbage collected when they go out of scope, explicitly releasing the lock using a
try...finallyblock is a safe and robust practice. In high-throughput benchmark runs, this can help minimize garbage collection pressure and avoid potential dangling locks between iterations.
packages/unhead/test/bench/stream.bench.ts#L26-L33: Wrap the reading loop in atry...finallyblock and callreader.releaseLock()for thedrainhelper.packages/unhead/test/bench/stream.bench.ts#L54-L58: Wrap theread()andcancel()calls in atry...finallyblock and callreader.releaseLock()for the cancellation bench.♻️ Proposed refactor for both sites
For
drainhelper:async function drain(stream: ReadableStream<Uint8Array>): Promise<void> { const reader = stream.getReader() - while (true) { - const { done } = await reader.read() - if (done) - return + try { + while (true) { + const { done } = await reader.read() + if (done) + return + } + } finally { + reader.releaseLock() } }For the cancellation bench:
bench('wrapStream: shell first-read then cancel (100-chunk upstream)', async () => { const reader = wrapStream(makeHead(), appStream(100), TEMPLATE).getReader() - await reader.read() - await reader.cancel('bench') + try { + await reader.read() + await reader.cancel('bench') + } finally { + reader.releaseLock() + } })🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/unhead/test/bench/stream.bench.ts` around lines 26 - 33, Explicitly release stream reader locks in both affected sites: wrap the reading loop in the drain helper at packages/unhead/test/bench/stream.bench.ts:26-33 with try/finally and call reader.releaseLock(), and wrap the read/cancel operations in the cancellation benchmark at packages/unhead/test/bench/stream.bench.ts:54-58 similarly. Preserve the existing benchmark behavior while ensuring each reader is released even when operations fail.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/unhead/test/bench/stream.bench.ts`:
- Around line 26-33: Explicitly release stream reader locks in both affected
sites: wrap the reading loop in the drain helper at
packages/unhead/test/bench/stream.bench.ts:26-33 with try/finally and call
reader.releaseLock(), and wrap the read/cancel operations in the cancellation
benchmark at packages/unhead/test/bench/stream.bench.ts:54-58 similarly.
Preserve the existing benchmark behavior while ensuring each reader is released
even when operations fail.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 242dfe5c-a6f2-410c-bfcd-fa11519f44a7
📒 Files selected for processing (1)
packages/unhead/test/bench/stream.bench.ts
📦 Bundle Size✅ No notable changes All bundles (14)
⚡ Performance (directional)🟢 1 faster
All benchmarks (14)
Baseline: main @ ad8c74e · 2026-07-13 · gzipped is the headline size metric · perf is directional (shared-runner, gated) |
🔗 Linked issue
N/A
❓ Type of change
📚 Description
The bench suite covers SSR render paths (
ssr-perf,transformHtmlTemplate, template params) but nothing exercises the streaming API. Two additions:Local vitest benches in
packages/unhead/test/bench/stream.bench.ts: a no-wrapper drain baseline,wrapStreamfull drain and shell-then-cancel,prepareStreamingTemplate, andrenderSSRHeadSuspenseChunkwith five entries per chunk. Baseline on main: full 100-chunk drain throughwrapStreamruns at ~21k ops/s vs ~44k for the bare stream, shell-then-cancel at ~108k ops/s.CI perf harness (
bench/perf-ci.mjs): astreamingBenches()section that measures an end-to-endwrapStreamdrain (shell + 50 chunks + closing HTML; CPU ~4% RME, wall informational, alloc per drain) and the per-suspense-boundary patch render (CPU + alloc), against shipped dist like the existing signals. These now appear in the bundle-size sticky comment with base-vs-PR deltas. Async-awaremeasureTimesAsync/measureAllocAsynctwins keep the sync render benches free of per-call await overhead, and the section skips cleanly on base refs that predate the streaming dist.Useful as a before/after reference for #827 and future streaming changes.