Unverified Commit 72461425 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

bench: improve render pipeline benchmark reliability (#92715)

### What?

Improves the render pipeline benchmark
(`bench/render-pipeline/benchmark.ts`) for more reliable and accurate
RPS measurement.

### Why?

The benchmark had several issues that affected measurement accuracy:
- V8 CPU profiler was on by default, adding 5-15% overhead to all
numbers
- 30 warmup requests was insufficient for V8 TurboFan JIT stabilization
- No stddev made it impossible to tell signal from noise
- Request errors crashed the whole benchmark instead of being tracked
- No port cleanup delay between mode switches could cause bind failures
- No option to isolate routes from cross-route GC/memory contamination
- Closed-loop measurement model limitations were undocumented

### How?

- Default `--capture-cpu` to `false` (use `--capture-cpu=true` for
dedicated profiling runs)
- Add `--warmup-until-stable` flag (default: true) that runs warmup in
batches and stops when mean latency delta is <5% between consecutive
batches
- Add standard deviation to `computeStats` and print it in results
- Track request errors in the under-load phase instead of aborting
- Add 2s sleep between mode switches when `--stream-mode=both`
- Add `--isolate-routes` flag to restart the server between routes
- Document the closed-loop measurement model in code and README
- Add minimal-server scenario documentation to README and benchmarking
playbook

<!-- NEXT_JS_LLM_PR -->
parent cd1b55b0
# Benchmarking Playbook (Render Pipeline / Node Streams)
# Benchmarking Playbook (Render Pipeline)
This is the practical workflow for benchmarking and profiling render pipeline changes in this repo.
......@@ -15,18 +15,17 @@ Always rebuild `next` before benchmark runs when framework source changed.
pnpm --filter=next build
```
## 2. End-to-end benchmark (full app render path)
## 2. E2E benchmark (real production server)
This measures the full request path (`renderToHTMLOrFlight`) through `bench/next-minimal-server`.
In `scenario=full` and `scenario=all`, `--capture-cpu` defaults to `true`.
This is the default scenario. It runs `next build` + `next start`, exercising the full production stack: `startServer()` → `router-server.initialize()` → `NextNodeServer` → app render.
Node streams only:
```bash
pnpm bench:render-pipeline \
--scenario=full \
--scenario=e2e \
--stream-mode=node \
--build-full=true \
--build=true \
--json-out=bench/render-pipeline/artifacts/<run>/results.json \
--artifact-dir=bench/render-pipeline/artifacts/<run>
```
......@@ -35,22 +34,37 @@ Web vs Node comparison:
```bash
pnpm bench:render-pipeline \
--scenario=full \
--scenario=e2e \
--stream-mode=both \
--build-full=true \
--build=true \
--json-out=bench/render-pipeline/artifacts/<run>/results.json \
--artifact-dir=bench/render-pipeline/artifacts/<run>
```
## 3. Route-focused stress runs
## 3. Minimal-server benchmark (isolated render path)
Use `--scenario=minimal-server` to bypass the router-server layer and measure the render pipeline in isolation. This starts a bare `NextServer` with `minimalMode: true` via `bench/next-minimal-server` — no `router-server`, no middleware, no asset serving. Prefer this when profiling changes to `app-render.tsx`, streaming internals, or Flight serialization where router overhead would add noise.
```bash
pnpm bench:render-pipeline \
--scenario=minimal-server \
--stream-mode=node \
--build=true \
--json-out=bench/render-pipeline/artifacts/<run>/results.json \
--artifact-dir=bench/render-pipeline/artifacts/<run>
```
The delta between e2e and minimal-server results for the same route reveals how much overhead the router-server layer contributes.
## 4. Route-focused stress runs
Use this when targeting streaming-heavy behavior only.
```bash
pnpm bench:render-pipeline \
--scenario=full \
--scenario=e2e \
--stream-mode=node \
--build-full=true \
--build=true \
--routes=/streaming/heavy,/streaming/chunkstorm,/streaming/wide \
--warmup-requests=10 \
--serial-requests=40 \
......@@ -70,54 +84,13 @@ Default stress routes currently include:
- `/streaming/wide`
- `/streaming/bulk`
## 4. Isolate helper-level costs (micro scenario)
Use this to quickly test helper-level changes before full runs.
```bash
pnpm bench:render-pipeline \
--scenario=micro \
--iterations=300 \
--warmup=30
```
Micro benchmark output includes cases for:
- `teeNodeReadable`
- `createBufferedTransformNode`
- `createInlinedDataNodeStream`
- `continueStaticPrerender` / `continueDynamicPrerender` / `continueDynamicHTMLResume`
Flight payload mode toggles:
```bash
# Binary-heavy flight chunks
pnpm bench:render-pipeline --scenario=micro --binary-flight=true
# UTF-8-heavy flight chunks
pnpm bench:render-pipeline --scenario=micro --binary-flight=false
```
Stress payload shape:
```bash
pnpm bench:render-pipeline \
--scenario=micro \
--iterations=300 \
--warmup=30 \
--flight-chunks=128 \
--flight-chunk-bytes=8192 \
--html-chunks=128 \
--html-chunk-bytes=32768
```
## 5. Capture CPU profiles and traces
```bash
pnpm bench:render-pipeline \
--scenario=full \
--scenario=e2e \
--stream-mode=node \
--build-full=true \
--build=true \
--capture-trace=true \
--capture-next-trace=true \
--json-out=bench/render-pipeline/artifacts/<run>/results.json \
......@@ -139,13 +112,6 @@ pnpm bench:render-pipeline:analyze \
--top=20
```
Filter only the Node-stream-relevant hotspots:
```bash
pnpm bench:render-pipeline:analyze --artifact-dir=bench/render-pipeline/artifacts/<run> --top=20 > /tmp/analyze.txt
rg "use-flight-response|encodeFlightDataChunkNode|node-stream-tee|flushPending|node-stream-helpers|htmlEscapeJsonString" /tmp/analyze.txt
```
## 7. Compare two runs quickly
```bash
......@@ -169,7 +135,7 @@ for (const b of base) {
`${b.route} ${b.phase} throughput ${throughputDelta >= 0 ? '+' : ''}${throughputDelta.toFixed(2)}% p95 ${p95Delta >= 0 ? '+' : ''}${p95Delta.toFixed(2)}%`
)
}
NODE investigation-10-boundary-data investigation-17-profile-current
NODE baseline-run candidate-run
```
## 8. Noise control rules
......@@ -181,12 +147,14 @@ Use these rules to keep measurements trustworthy:
- Repeat suspicious runs at least once (especially if one route regresses while others improve).
- Use dedicated artifact directories per run.
- Prefer relative deltas across multiple runs over one-off absolute numbers.
- When comparing e2e vs minimal-server scenarios, remember that e2e includes the full router-server overhead.
## 9. Suggested iteration loop
1. Change one thing.
2. Build.
3. Run `scenario=micro` for quick signal.
4. Run focused full stress (`heavy/chunkstorm/wide`) with CPU profile.
5. Analyze hotspots and compare deltas.
6. Keep only changes that hold up across repeat runs.
2. Build (`pnpm --filter=next build`).
3. Run `--scenario=e2e` for production-realistic numbers.
4. Run `--scenario=minimal-server` to isolate render-path-only impact (skip this if your change is in routing/middleware, not the render pipeline).
5. Run focused stress routes with CPU profile (`--capture-cpu=true`).
6. Analyze hotspots and compare deltas.
7. Keep only changes that hold up across repeat runs.
......@@ -8,30 +8,55 @@ It supports:
- CPU/heap profiling for the server process
- Node trace events and Next internal trace artifact capture
## Scenarios
### `--scenario=e2e` (default)
Runs `next build` + `next start`, exercising the full production stack:
`startServer()` → `router-server.initialize()` → `NextNodeServer` → app render.
Use this for production-realistic throughput numbers.
```bash
pnpm bench:render-pipeline --scenario=e2e --stream-mode=both
```
### `--scenario=minimal-server`
Bypasses the router-server layer entirely. Starts a bare `NextServer` with
`minimalMode: true` via `bench/next-minimal-server`. Use this to isolate the
render pipeline from routing/middleware overhead — useful when profiling changes
to `app-render.tsx`, streaming, or Flight serialization.
```bash
pnpm bench:render-pipeline --scenario=minimal-server --stream-mode=both
```
When comparing both scenarios, the delta reveals how much overhead the
router-server layer adds on top of the raw render path.
## Quick start
Run end-to-end benchmark (default stress routes):
```bash
pnpm bench:render-pipeline --scenario=full --stream-mode=both
pnpm bench:render-pipeline --scenario=e2e --stream-mode=both
```
For `scenario=full` and `scenario=all`, CPU profiles are captured by default.
Disable with `--capture-cpu=false` if you want lower-overhead runs.
CPU profiling is off by default. Enable with `--capture-cpu=true` for profiling runs.
Skip rebuild for faster iteration (after you already built once):
```bash
pnpm bench:render-pipeline --scenario=full --stream-mode=node --build-full=false
pnpm bench:render-pipeline --scenario=e2e --stream-mode=node --build=false
```
When `--stream-mode=both`, the runner forces `--build-full=true` so web/node
When `--stream-mode=both`, the runner forces `--build=true` so web/node
comparisons do not accidentally reuse stale build output.
Output JSON report:
```bash
pnpm bench:render-pipeline --scenario=full --stream-mode=both --json-out=/tmp/render-pipeline.json
pnpm bench:render-pipeline --scenario=e2e --stream-mode=both --json-out=/tmp/render-pipeline.json
```
## Profiling and traces
......@@ -40,8 +65,9 @@ Capture CPU profiles + Node trace events + Next trace logs:
```bash
pnpm bench:render-pipeline \
--scenario=full \
--scenario=e2e \
--stream-mode=both \
--capture-cpu=true \
--capture-trace=true \
--capture-next-trace=true
```
......@@ -84,12 +110,29 @@ The `streaming/*` pages now include a client boundary per Suspense chunk, so ben
Override with:
```bash
pnpm bench:render-pipeline --scenario=full --routes=/,/streaming/heavy
pnpm bench:render-pipeline --scenario=e2e --routes=/,/streaming/heavy
```
## Measurement model
The benchmark uses a **closed-loop** load generator: each concurrent worker
issues the next request only after the current one completes. This means:
- **Throughput numbers are reliable for relative comparison** (A/B between stream
modes, before/after a code change). Both sides experience the same measurement
model, so deltas are valid.
- **Latency percentiles (p95, max) under load are optimistic.** Slow requests
reduce back-pressure instead of queuing, masking tail latency. Do not compare
absolute latency values from this benchmark to open-loop tools like k6 or wrk2.
CPU profiling (`--capture-cpu`) is disabled by default to avoid inflating
measurements. Run a separate profiling pass with `--capture-cpu=true` when you
need `.cpuprofile` artifacts.
## Common tuning flags
- `--warmup-requests=30`
- `--warmup-requests=50`
- `--warmup-until-stable=true`
- `--serial-requests=120`
- `--load-requests=1200`
- `--load-concurrency=80`
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment