Skip to content

chore(bench): add streaming SSR benchmarks - #835

Merged
harlan-zw merged 2 commits into
mainfrom
bench/streaming
Jul 14, 2026
Merged

harlan-zw merged 2 commits into
mainfrom
bench/streaming

Conversation

@harlan-zw

@harlan-zw harlan-zw commented Jul 14, 2026 •

Copy link
Copy Markdown
Collaborator

🔗 Linked issue

N/A

❓ Type of change

  • 📖 Documentation
  • 🐞 Bug fix
  • 👌 Enhancement
  • ✨ New feature
  • 🧹 Chore
  • ⚠️ Breaking 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, wrapStream full drain and shell-then-cancel, prepareStreamingTemplate, and renderSSRHeadSuspenseChunk with five entries per chunk. Baseline on main: full 100-chunk drain through wrapStream runs at ~21k ops/s vs ~44k for the bare stream, shell-then-cancel at ~108k ops/s.

CI perf harness (bench/perf-ci.mjs): a streamingBenches() section that measures an end-to-end wrapStream drain (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-aware measureTimesAsync/measureAllocAsync twins 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.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a Vitest benchmark suite for server-streaming head rendering, including stream helpers, stream wrapping and cancellation cases, template preparation, and repeated suspense chunk rendering.

Changes

Streaming SSR Benchmarks

Layer / File(s) Summary
Stream benchmark harness
packages/unhead/test/bench/stream.bench.ts
Defines shared constants and helpers for creating byte streams, draining them, and initializing streamable heads with metadata.
Streaming SSR benchmark scenarios
packages/unhead/test/bench/stream.bench.ts
Measures baseline and wrapped stream draining, cancellation, repeated template preparation, and suspense chunk rendering with incrementally added head entries.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise, conventional, and accurately summarizes the new streaming SSR benchmark work.
Description check ✅ Passed The description matches the template and clearly explains the change, type, and benchmark scope.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bench/streaming

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/unhead/test/bench/stream.bench.ts (1)

26-33: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Explicitly release stream reader locks.

Although readers are garbage collected when they go out of scope, explicitly releasing the lock using a try...finally block 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 a try...finally block and call reader.releaseLock() for the drain helper.
  • packages/unhead/test/bench/stream.bench.ts#L54-L58: Wrap the read() and cancel() calls in a try...finally block and call reader.releaseLock() for the cancellation bench.
♻️ Proposed refactor for both sites

For drain helper:

 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

📥 Commits

Reviewing files that changed from the base of the PR and between ad8c74e and 627303d.

📒 Files selected for processing (1)
  • packages/unhead/test/bench/stream.bench.ts

@github-actions

github-actions Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

📦 Bundle Size

✅ No notable changes

All bundles (14)
Bundle Gzipped Brotli Raw
Core
Client (Minimal) 5.1 kB 4.7 kB 12.8 kB ✅
Client (Full) 8.5 kB 7.7 kB 22 kB ✅
Client (Self-Contained) 5.5 kB 4.9 kB 13.5 kB ✅
Server (Minimal) 4.5 kB 4.1 kB 11.3 kB ✅
Server (Self-Contained) 4.9 kB 4.4 kB 12 kB ✅
Vue
Vue Client (Minimal) 5.6 kB 5.1 kB 13.8 kB ✅
Vue Client (Full) 9.2 kB 8.4 kB 23.9 kB ✅
Vue Server (Minimal) 5 kB 4.5 kB 12.3 kB ✅
React
React Client (Minimal) 5.6 kB 5 kB 13.8 kB ✅
React Client (Full) 9.4 kB 8.5 kB 24.4 kB ✅
React Server (Minimal) 4.8 kB 4.4 kB 12 kB ✅
Schema.org
Schema.org (Minimal) 9.6 kB 8.7 kB 26.4 kB ✅
Schema.org Imports 0.1 kB 0.1 kB 0.1 kB ✅
Schema.org Vue Meta 0.4 kB 0.3 kB 0.8 kB ✅

⚡ Performance (directional)

🟢 1 faster

Benchmark base → PR Δ
SSR allocated / render 273.6 KiB → 267.5 KiB 🟢 -6.1 KiB (-2.2%)
All benchmarks (14)
Benchmark PR Δ RME
SSR render (CPU) 0.427 ms ~ noise ±11.2%
SSR render (wall) 0.285 ms ~ noise ±5.7%
SSR allocated / render 267.5 KiB 🟢 -6.1 KiB (-2.2%) —
Schema.org cached render (CPU) 0.384 ms ~ noise ±5.3%
Schema.org cached render (wall) 0.257 ms ~ noise ±2.1%
Schema.org cached allocated / render 145.4 KiB ~ noise —
Streaming wrapStream drain (CPU) 0.240 ms ~ noise ±2.1%
Streaming wrapStream drain (wall) 0.145 ms ~ noise ±2.1%
Streaming allocated / drain 127.2 KiB ~ noise —
Streaming suspense chunk (CPU) 0.010 ms ~ noise ±8.5%
Streaming allocated / suspense chunk 4.4 KiB ~ noise —
CSR DOM mutations / nav 38 ~ noise —
CSR re-render (CPU) 0.857 ms ~ noise ±4.3%
CSR re-render (wall) 0.484 ms ~ noise ±2.0%

Baseline: main @ ad8c74e · 2026-07-13 · gzipped is the headline size metric · perf is directional (shared-runner, gated)

@harlan-zw
harlan-zw merged commit 5f37694 into main Jul 14, 2026
7 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Jul 19, 2026
4 of 6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant