| 1 | + | /** |
| 2 | + | * Benchmark harness for @lazarv/react-server production performance. |
| 3 | + | * |
| 4 | + | * Usage: |
| 5 | + | * 1. pnpm --filter @lazarv/react-server-example-benchmark build |
| 6 | + | * 2. node bench.mjs [--save <label>] [--compare <file>] [--cluster <n>] |
| 7 | + | * |
| 8 | + | * Options: |
| 9 | + | * --save <label> Save results to results-<label>.json |
| 10 | + | * --compare <file> Compare against a previous results JSON file |
| 11 | + | * --cluster <n> Run in cluster mode with n workers (uses react-server start) |
| 12 | + | * |
| 13 | + | * Runs autocannon against each benchmark route and prints a summary table. |
| 14 | + | */ |
| 15 | + | import { createServer } from "node:http"; |
| 16 | + | import { spawn } from "node:child_process"; |
| 17 | + | import { writeFileSync, readFileSync } from "node:fs"; |
| 18 | + | import { execSync } from "node:child_process"; |
| 19 | + | |
| 20 | + | process.env.NODE_ENV = "production"; |
| 21 | + | |
| 22 | + | // ── CLI args ───────────────────────────────────────────────────────────────── |
| 23 | + | |
| 24 | + | const args = process.argv.slice(2); |
| 25 | + | const saveLabel = args.includes("--save") |
| 26 | + | ? args[args.indexOf("--save") + 1] |
| 27 | + | : null; |
| 28 | + | const compareFile = args.includes("--compare") |
| 29 | + | ? args[args.indexOf("--compare") + 1] |
| 30 | + | : null; |
| 31 | + | |
| 32 | + | function parseCluster() { |
| 33 | + | const idx = args.findIndex((a) => a.startsWith("--cluster")); |
| 34 | + | if (idx === -1) return 0; |
| 35 | + | // --cluster=4 or --cluster 4 |
| 36 | + | if (args[idx].includes("=")) return parseInt(args[idx].split("=")[1], 10); |
| 37 | + | return parseInt(args[idx + 1], 10); |
| 38 | + | } |
| 39 | + | const clusterSize = parseCluster(); |
| 40 | + | |
| 41 | + | const PORT = 3210; |
| 42 | + | const DURATION = 10; // seconds per test |
| 43 | + | const CONNECTIONS = 50; |
| 44 | + | |
| 45 | + | // ── Boot production server ────────────────────────────────────────────────── |
| 46 | + | |
| 47 | + | let serverProcess = null; |
| 48 | + | |
| 49 | + | if (clusterSize > 0) { |
| 50 | + | // Cluster mode: spawn react-server start as a child process |
| 51 | + | serverProcess = await new Promise((resolve, reject) => { |
| 52 | + | const child = spawn( |
| 53 | + | "npx", |
| 54 | + | ["react-server", "start", "--port", String(PORT)], |
| 55 | + | { |
| 56 | + | stdio: ["pipe", "pipe", "pipe"], |
| 57 | + | env: { |
| 58 | + | ...process.env, |
| 59 | + | REACT_SERVER_CLUSTER: String(clusterSize), |
| 60 | + | }, |
| 61 | + | } |
| 62 | + | ); |
| 63 | + | |
| 64 | + | let ready = false; |
| 65 | + | const onData = (chunk) => { |
| 66 | + | const text = chunk.toString(); |
| 67 | + | process.stderr.write(text); |
| 68 | + | // Workers log "listening on" when ready — wait for all of them |
| 69 | + | if (!ready && text.includes("listening on")) { |
| 70 | + | ready = true; |
| 71 | + | // Give a moment for remaining workers to bind |
| 72 | + | setTimeout(() => resolve(child), 500); |
| 73 | + | } |
| 74 | + | }; |
| 75 | + | |
| 76 | + | child.stdout.on("data", onData); |
| 77 | + | child.stderr.on("data", onData); |
| 78 | + | child.on("error", reject); |
| 79 | + | child.on("exit", (code) => { |
| 80 | + | if (!ready) |
| 81 | + | reject(new Error(`react-server start exited with code ${code}`)); |
| 82 | + | }); |
| 83 | + | |
| 84 | + | // Safety timeout |
| 85 | + | setTimeout(() => { |
| 86 | + | if (!ready) { |
| 87 | + | child.kill(); |
| 88 | + | reject(new Error("Timed out waiting for cluster to start")); |
| 89 | + | } |
| 90 | + | }, 30000); |
| 91 | + | }); |
| 92 | + | } else { |
| 93 | + | // Single-process mode: use programmatic middleware API |
| 94 | + | const { reactServer } = await import("@lazarv/react-server/node"); |
| 95 | + | const { middlewares } = await reactServer({ |
| 96 | + | origin: `http://localhost:${PORT}`, |
| 97 | + | host: "localhost", |
| 98 | + | port: PORT, |
| 99 | + | outDir: ".react-server", |
| 100 | + | }); |
| 101 | + | |
| 102 | + | const server = createServer(middlewares); |
| 103 | + | await new Promise((resolve) => server.listen(PORT, resolve)); |
| 104 | + | // Store for cleanup |
| 105 | + | serverProcess = server; |
| 106 | + | } |
| 107 | + | |
| 108 | + | const mode = |
| 109 | + | clusterSize > 0 ? `cluster (${clusterSize} workers)` : "single-process"; |
| 110 | + | console.log(`\nBenchmark server on http://localhost:${PORT} [${mode}]`); |
| 111 | + | console.log(`Running ${DURATION}s per test, ${CONNECTIONS} connections\n`); |
| 112 | + | |
| 113 | + | // ── Benchmark definitions ─────────────────────────────────────────────────── |
| 114 | + | |
| 115 | + | const BENCHMARKS = [ |
| 116 | + | { name: "minimal", path: "/", desc: "Minimal SSR (tiny page)" }, |
| 117 | + | { name: "small", path: "/small", desc: "Small SSR (~20 elements)" }, |
| 118 | + | { name: "medium", path: "/medium", desc: "Medium SSR (50 products)" }, |
| 119 | + | { name: "large", path: "/large", desc: "Large SSR (500-row table)" }, |
| 120 | + | { name: "deep", path: "/deep", desc: "Deep nesting (100 levels)" }, |
| 121 | + | { name: "wide", path: "/wide", desc: "Wide tree (1000 siblings)" }, |
| 122 | + | { name: "cached", path: "/cached", desc: "Cached medium page" }, |
| 123 | + | { |
| 124 | + | name: "client-min", |
| 125 | + | path: "/client", |
| 126 | + | desc: "Client component minimal", |
| 127 | + | }, |
| 128 | + | { |
| 129 | + | name: "client-small", |
| 130 | + | path: "/client/small", |
| 131 | + | desc: "Client component small", |
| 132 | + | }, |
| 133 | + | { |
| 134 | + | name: "client-med", |
| 135 | + | path: "/client/medium", |
| 136 | + | desc: "Client component medium (50 products)", |
| 137 | + | }, |
| 138 | + | { |
| 139 | + | name: "client-large", |
| 140 | + | path: "/client/large", |
| 141 | + | desc: "Client component large (500 rows)", |
| 142 | + | }, |
| 143 | + | { |
| 144 | + | name: "client-deep", |
| 145 | + | path: "/client/deep", |
| 146 | + | desc: "Client component deep (100 levels)", |
| 147 | + | }, |
| 148 | + | { |
| 149 | + | name: "client-wide", |
| 150 | + | path: "/client/wide", |
| 151 | + | desc: "Client component wide (1000 siblings)", |
| 152 | + | }, |
| 153 | + | { |
| 154 | + | name: "static-json", |
| 155 | + | path: "/data.json", |
| 156 | + | desc: "Static file (JSON)", |
| 157 | + | }, |
| 158 | + | { |
| 159 | + | name: "static-js", |
| 160 | + | path: null, // resolved dynamically |
| 161 | + | desc: "Static file (JS bundle)", |
| 162 | + | }, |
| 163 | + | { name: "404-miss", path: "/nonexistent", desc: "404 miss → SSR" }, |
| 164 | + | ]; |
| 165 | + | |
| 166 | + | // ── Find an actual JS bundle path ─────────────────────────────────────────── |
| 167 | + | |
| 168 | + | import { readdirSync } from "node:fs"; |
| 169 | + | try { |
| 170 | + | const clientFiles = readdirSync(".react-server/client"); |
| 171 | + | const jsBundle = clientFiles.find( |
| 172 | + | (f) => f.endsWith(".mjs") && f.includes(".") |
| 173 | + | ); |
| 174 | + | if (jsBundle) { |
| 175 | + | BENCHMARKS.find((b) => b.name === "static-js").path = `/client/${jsBundle}`; |
| 176 | + | } |
| 177 | + | } catch { |
| 178 | + | // skip if not found |
| 179 | + | } |
| 180 | + | |
| 181 | + | // ── Run autocannon ────────────────────────────────────────────────────────── |
| 182 | + | |
| 183 | + | async function runAutocannon(url, extraArgs = []) { |
| 184 | + | return new Promise((resolve, reject) => { |
| 185 | + | const args = [ |
| 186 | + | "autocannon", |
| 187 | + | "-c", |
| 188 | + | String(CONNECTIONS), |
| 189 | + | "-d", |
| 190 | + | String(DURATION), |
| 191 | + | "--json", |
| 192 | + | ...extraArgs, |
| 193 | + | url, |
| 194 | + | ]; |
| 195 | + | const proc = spawn("npx", args, { stdio: ["pipe", "pipe", "pipe"] }); |
| 196 | + | let stdout = ""; |
| 197 | + | proc.stdout.on("data", (d) => (stdout += d)); |
| 198 | + | proc.on("close", (code) => { |
| 199 | + | try { |
| 200 | + | resolve(JSON.parse(stdout)); |