mirror of
https://github.com/hanzoai/react.git
synced 2026-09-24 17:02:27 +00:00
This PR adds a benchmark fixture for measuring the performance overhead of the React Server Components (RSC) Flight rendering compared to plain Fizz server-side rendering. ### Motivation Performance discussions around RSC (e.g. #36143, #35125) have highlighted the need for reproducible benchmarks that accurately measure the cost that Flight adds on top of Fizz. This fixture provides multiple benchmark modes that can be used to track performance improvements across commits, compare Node vs Edge (web streams) overhead, and identify bottlenecks in Flight serialization and deserialization. ### What it measures The benchmark renders a dashboard app with ~25 components (16 client components), 200 product rows with nested data (~325KB Flight payload), and ~250 Suspense boundaries in the async variant. It compares 8 render variants: Fizz-only and Flight+Fizz, across Node and Edge stream APIs, with both synchronous and asynchronous apps. ### Benchmark modes - **`yarn bench`** runs a sequential in-process benchmark with realistic Flight script injection (tee + `TransformStream`/`Transform` buffered injection), matching what real frameworks do when inlining the RSC payload into the HTML response for hydration. - **`yarn bench:bare`** runs the same benchmark without script injection, isolating the React-internal rendering cost. This is best for tracking changes to Flight serialization or Fizz rendering. - **`yarn bench:server`** starts an HTTP server and uses `autocannon` to measure real req/s at `c=1` and `c=10`. The `c=1` results provide a clean signal for tracking React-internal changes, while `c=10` reflects throughput under concurrent load. - **`yarn bench:concurrent`** runs an in-process concurrent benchmark with 50 in-flight renders via `Promise.all`, measuring throughput without HTTP overhead. - **`yarn bench:profile`** collects CPU profiles via the V8 inspector and reports the top functions by self-time along with GC pause data. - **`yarn start`** starts the HTTP server for manual browser testing. Appending `.rsc` to any Flight URL serves the raw Flight payload. ### Key findings during development On Node 22, the Flight+Fizz overhead compared to Fizz-only rendering is roughly: - **Without script injection** (`bench:bare`): ~2.2x for sync, ~1.3x for async - **With script injection** (`bench:server`, c=1): ~2.9x for sync, ~1.8x for async - **Edge vs Node** adds another ~30% for sync and ~10% for async, driven by the stream plumbing for script injection (tee + `TransformStream` buffering) The async variant better represents real-world applications where server components fetch data asynchronously. Its lower overhead reflects the fact that Flight serialization and Fizz rendering can overlap with I/O wait times, making the added Flight cost a smaller fraction of total request time. The benchmark also revealed that the Edge vs Node gap is negligible for Fizz-only rendering (~1-2%) but grows to ~15% for Flight+Fizz sync even without script injection. With script injection (tee + `TransformStream` buffering), the gap roughly doubles to ~30% for sync. The async variants show smaller gaps (~5% without, ~10% with injection).
330 lines
9.2 KiB
JavaScript
330 lines
9.2 KiB
JavaScript
'use strict';
|
|
|
|
const {PassThrough, Transform} = require('stream');
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Fizz (Node) — renders App directly via Node streams.
|
|
// Returns a Node Readable stream of HTML.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function renderFizzNode(AppComponent, itemCount) {
|
|
const React = require('react');
|
|
const {renderToPipeableStream} = require('react-dom/server');
|
|
|
|
const output = new PassThrough();
|
|
const {pipe} = renderToPipeableStream(
|
|
React.createElement(AppComponent, {itemCount}),
|
|
{
|
|
onShellReady() {
|
|
pipe(output);
|
|
},
|
|
onError(e) {
|
|
console.error('Fizz Node error:', e);
|
|
output.destroy(e);
|
|
},
|
|
}
|
|
);
|
|
return output;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Fizz (Edge) — renders App directly via web streams.
|
|
// Returns a promise that resolves to a web ReadableStream of HTML.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function renderFizzEdge(AppComponent, itemCount) {
|
|
const React = require('react');
|
|
const {renderToReadableStream} = require('react-dom/server');
|
|
|
|
return renderToReadableStream(React.createElement(AppComponent, {itemCount}));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Flight + Fizz (Node) — RSC render → tee → Fizz + script injection.
|
|
// HTML chunks are buffered within a tick to avoid injecting scripts mid-tag.
|
|
// Returns a Node Readable stream of HTML with injected Flight scripts.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function renderFlightFizzNode(
|
|
renderRSCNode,
|
|
AppComponent,
|
|
itemCount,
|
|
clientManifest,
|
|
ssrManifest,
|
|
opts
|
|
) {
|
|
const inject = !opts || opts.inject !== false;
|
|
const React = require('react');
|
|
const {renderToPipeableStream} = require('react-dom/server');
|
|
const {createFromNodeStream} = require('react-server-dom-webpack/client');
|
|
|
|
const {pipe: rscPipe} = renderRSCNode(
|
|
clientManifest,
|
|
AppComponent,
|
|
itemCount
|
|
);
|
|
|
|
let flightStream;
|
|
let flightScripts = '';
|
|
if (inject) {
|
|
// Tee the Flight stream into SSR + script injection
|
|
const trunk = new PassThrough();
|
|
const forSsr = new PassThrough();
|
|
const forInline = new PassThrough();
|
|
trunk.pipe(forSsr);
|
|
trunk.pipe(forInline);
|
|
|
|
forInline.on('data', function (chunk) {
|
|
flightScripts +=
|
|
'<script>(self.__FLIGHT_DATA||=[]).push(' +
|
|
JSON.stringify(chunk.toString()) +
|
|
')</script>';
|
|
});
|
|
|
|
rscPipe(trunk);
|
|
flightStream = forSsr;
|
|
} else {
|
|
flightStream = new PassThrough();
|
|
rscPipe(flightStream);
|
|
}
|
|
|
|
let cachedResult;
|
|
function Root() {
|
|
if (!cachedResult) {
|
|
cachedResult = createFromNodeStream(flightStream, ssrManifest);
|
|
}
|
|
return React.use(cachedResult);
|
|
}
|
|
|
|
const output = new PassThrough();
|
|
|
|
const {pipe} = renderToPipeableStream(React.createElement(Root), {
|
|
onShellReady() {
|
|
if (inject) {
|
|
// Buffer HTML chunks within a tick to avoid injecting scripts mid-tag.
|
|
const trailer = '</body></html>';
|
|
let buffered = [];
|
|
let timeout = null;
|
|
const injector = new Transform({
|
|
transform(chunk, _encoding, cb) {
|
|
buffered.push(chunk);
|
|
if (!timeout) {
|
|
timeout = setTimeout(() => {
|
|
for (const buf of buffered) {
|
|
let str = buf.toString();
|
|
if (str.endsWith(trailer)) {
|
|
str = str.slice(0, -trailer.length);
|
|
}
|
|
this.push(str);
|
|
}
|
|
buffered.length = 0;
|
|
timeout = null;
|
|
if (flightScripts) {
|
|
this.push(flightScripts);
|
|
flightScripts = '';
|
|
}
|
|
}, 0);
|
|
}
|
|
cb();
|
|
},
|
|
flush(cb) {
|
|
if (timeout) {
|
|
clearTimeout(timeout);
|
|
for (const buf of buffered) {
|
|
let str = buf.toString();
|
|
if (str.endsWith(trailer)) {
|
|
str = str.slice(0, -trailer.length);
|
|
}
|
|
this.push(str);
|
|
}
|
|
buffered.length = 0;
|
|
}
|
|
if (flightScripts) {
|
|
this.push(flightScripts);
|
|
flightScripts = '';
|
|
}
|
|
this.push(trailer);
|
|
cb();
|
|
},
|
|
});
|
|
pipe(injector);
|
|
injector.pipe(output);
|
|
} else {
|
|
pipe(output);
|
|
}
|
|
},
|
|
onError(e) {
|
|
console.error('Flight+Fizz Node error:', e);
|
|
output.destroy(e);
|
|
},
|
|
});
|
|
|
|
return output;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Flight + Fizz (Edge) — RSC render → tee → Fizz + script injection via web
|
|
// streams. HTML chunks are buffered within a tick to avoid injecting scripts
|
|
// mid-tag. The </body></html> trailer is stripped, Flight scripts injected,
|
|
// and the trailer re-added at flush.
|
|
// Returns a promise that resolves to a web ReadableStream.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function renderFlightFizzEdge(
|
|
renderRSCEdge,
|
|
AppComponent,
|
|
itemCount,
|
|
clientManifest,
|
|
ssrManifest,
|
|
opts
|
|
) {
|
|
const inject = !opts || opts.inject !== false;
|
|
const React = require('react');
|
|
const {renderToReadableStream} = require('react-dom/server');
|
|
const {
|
|
createFromReadableStream,
|
|
} = require('react-server-dom-webpack/client.edge');
|
|
|
|
const webStream = renderRSCEdge(clientManifest, AppComponent, itemCount);
|
|
|
|
let forSsr;
|
|
let injector;
|
|
|
|
if (inject) {
|
|
const htmlTrailer = '</body></html>';
|
|
const enc = new TextEncoder();
|
|
|
|
let forInline;
|
|
[forSsr, forInline] = webStream.tee();
|
|
|
|
let resolveInline;
|
|
const inlinePromise = new Promise(function (r) {
|
|
resolveInline = r;
|
|
});
|
|
const htmlDecoder = new TextDecoder();
|
|
let buffered = [];
|
|
let timeout = null;
|
|
|
|
function flushBuffered(controller) {
|
|
for (const chunk of buffered) {
|
|
let buf = htmlDecoder.decode(chunk, {stream: true});
|
|
if (buf.endsWith(htmlTrailer)) {
|
|
buf = buf.slice(0, -htmlTrailer.length);
|
|
}
|
|
controller.enqueue(enc.encode(buf));
|
|
}
|
|
const remaining = htmlDecoder.decode();
|
|
if (remaining.length) {
|
|
let buf = remaining;
|
|
if (buf.endsWith(htmlTrailer)) {
|
|
buf = buf.slice(0, -htmlTrailer.length);
|
|
}
|
|
controller.enqueue(enc.encode(buf));
|
|
}
|
|
buffered.length = 0;
|
|
timeout = null;
|
|
}
|
|
|
|
function writeFlightChunk(data, controller) {
|
|
controller.enqueue(
|
|
enc.encode(
|
|
'<script>(self.__FLIGHT_DATA||=[]).push(' +
|
|
JSON.stringify(data) +
|
|
')</script>'
|
|
)
|
|
);
|
|
}
|
|
|
|
injector = new TransformStream({
|
|
start(controller) {
|
|
(async function () {
|
|
const reader = forInline.getReader();
|
|
const decoder = new TextDecoder('utf-8', {fatal: true});
|
|
for (;;) {
|
|
const {done, value} = await reader.read();
|
|
if (done) break;
|
|
writeFlightChunk(decoder.decode(value, {stream: true}), controller);
|
|
}
|
|
const remaining = decoder.decode();
|
|
if (remaining.length) {
|
|
writeFlightChunk(remaining, controller);
|
|
}
|
|
resolveInline();
|
|
})();
|
|
},
|
|
transform(chunk, controller) {
|
|
buffered.push(chunk);
|
|
if (!timeout) {
|
|
timeout = setTimeout(function () {
|
|
flushBuffered(controller);
|
|
}, 0);
|
|
}
|
|
},
|
|
async flush(controller) {
|
|
await inlinePromise;
|
|
if (timeout) {
|
|
clearTimeout(timeout);
|
|
flushBuffered(controller);
|
|
}
|
|
controller.enqueue(enc.encode(htmlTrailer));
|
|
},
|
|
});
|
|
} else {
|
|
forSsr = webStream;
|
|
}
|
|
|
|
const cachedResult = createFromReadableStream(forSsr, {
|
|
serverConsumerManifest: ssrManifest,
|
|
});
|
|
function Root() {
|
|
return React.use(cachedResult);
|
|
}
|
|
|
|
return renderToReadableStream(React.createElement(Root)).then(
|
|
function (htmlStream) {
|
|
return injector ? htmlStream.pipeThrough(injector) : htmlStream;
|
|
}
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Utilities: collect streams into strings.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function nodeStreamToString(nodeStream) {
|
|
return new Promise(function (resolve, reject) {
|
|
const chunks = [];
|
|
nodeStream.on('data', function (chunk) {
|
|
chunks.push(chunk);
|
|
});
|
|
nodeStream.on('end', function () {
|
|
resolve(Buffer.concat(chunks).toString('utf-8'));
|
|
});
|
|
nodeStream.on('error', reject);
|
|
});
|
|
}
|
|
|
|
function webStreamToString(webStream) {
|
|
const reader = webStream.getReader();
|
|
const chunks = [];
|
|
function read() {
|
|
return reader.read().then(function ({done, value}) {
|
|
if (done) {
|
|
return Buffer.concat(chunks).toString('utf-8');
|
|
}
|
|
chunks.push(Buffer.from(value));
|
|
return read();
|
|
});
|
|
}
|
|
return read();
|
|
}
|
|
|
|
module.exports = {
|
|
renderFizzNode,
|
|
renderFizzEdge,
|
|
renderFlightFizzNode,
|
|
renderFlightFizzEdge,
|
|
nodeStreamToString,
|
|
webStreamToString,
|
|
};
|