Open History to see the last commit
1#!/usr/bin/env bun
2/**
3 * benchmark-runner.ts — Orchestrator for the OTP crossover benchmark suite.
4 *
5 * Runs all 5 benchmarks for both runtimes (node + elixir) sequentially.
6 * The Elixir harness must be started externally before running with --runtime=elixir.
7 *
8 * Usage:
9 * bun run scripts/benchmark-runner.ts --runtime=node
10 * bun run scripts/benchmark-runner.ts --runtime=elixir
11 * bun run scripts/benchmark-runner.ts --runtime=both
12 * bun run scripts/benchmark-runner.ts --runtime=node --only=session-ramp,payload-ramp
13 * bun run scripts/benchmark-runner.ts --runtime=both --quick
14 *
15 * Options:
16 * --runtime=node|elixir|both Which runtime(s) to benchmark (default: both)
17 * --only=name,name,... Only run specific benchmarks
18 * --quick Use reduced step counts for quick verification
19 * --skip=name,name,... Skip specific benchmarks
20 */
21
22import { spawn } from "node:child_process";
23import { mkdirSync, readdirSync } from "node:fs";
24
25import {
26 BENCHMARK_SUITE,
27 expandRuntimes,
28 filterBenchmarkSuite,
29 parseBenchmarkRunnerArgv,
30} from "./benchmark-runner-cli.ts";
31
32const OUTPUT_DIR = `${process.cwd()}/output/stress-test`;
33mkdirSync(OUTPUT_DIR, { recursive: true });
34
35// ---------------------------------------------------------------------------
36// CLI parsing
37// ---------------------------------------------------------------------------
38
39const args = process.argv.slice(2);
40const { runtime: RUNTIME, only: ONLY, skip: SKIP, quick: QUICK } = parseBenchmarkRunnerArgv(args);
41const benchmarks = filterBenchmarkSuite(BENCHMARK_SUITE, ONLY, SKIP);
42
43const t0 = Date.now();
44const ts = () => `+${((Date.now() - t0) / 1000).toFixed(0)}s`;
45
46// ---------------------------------------------------------------------------
47// Run a single benchmark script as a child process
48// ---------------------------------------------------------------------------
49
50function runBenchmark(
51 script: string,
52 runtime: string,
53 extraArgs: string[] = [],
54): Promise<{ exitCode: number; duration_s: number }> {
55 return new Promise((resolve) => {
56 const start = Date.now();
57 const childArgs = ["run", `scripts/${script}`, `--runtime=${runtime}`, ...extraArgs];
58
59 console.log(` $ bun ${childArgs.join(" ")}`);
60
61 const child = spawn("bun", childArgs, {
62 cwd: process.cwd(),
63 env: process.env,
64 stdio: ["ignore", "inherit", "inherit"],
65 });
66
67 child.on("exit", (code) => {
68 resolve({
69 exitCode: code ?? 1,
70 duration_s: (Date.now() - start) / 1000,
71 });
72 });
73
74 child.on("error", () => {
75 resolve({ exitCode: 1, duration_s: (Date.now() - start) / 1000 });
76 });
77 });
78}
79
80// ---------------------------------------------------------------------------
81// Main
82// ---------------------------------------------------------------------------
83
84async function main() {
85 const runtimes = expandRuntimes(RUNTIME);
86
87 console.log("\n╔══════════════════════════════════════════════════════════════╗");
88 console.log("║ OTP Crossover Benchmark Suite ║");
89 console.log("╚══════════════════════════════════════════════════════════════╝\n");
90 console.log(` Runtimes: ${runtimes.join(", ")}`);
91 console.log(` Benchmarks: ${benchmarks.map((b) => b.name).join(", ")}`);
92 console.log(` Mode: ${QUICK ? "quick (reduced steps)" : "full"}`);
93 console.log(` Output: ${OUTPUT_DIR}/`);
94 console.log("");
95
96 if (runtimes.includes("elixir")) {
97 // Verify harness is running
98 const HARNESS_PORT = Number(process.env.T3CODE_HARNESS_PORT ?? 4321);
99 try {
100 const res = await fetch(`http://127.0.0.1:${HARNESS_PORT}/api/metrics`);
101 if (!res.ok) throw new Error(`HTTP ${res.status}`);
102 console.log(` ✓ Elixir harness detected on port ${HARNESS_PORT}\n`);
103 } catch {
104 if (RUNTIME !== "node") {
105 console.error(` ✗ Elixir harness not running on port ${HARNESS_PORT}`);
106 console.error(` Start it first: cd apps/harness && mix phx.server`);
107 console.error(` Or run with --runtime=node to skip Elixir benchmarks\n`);
108 process.exit(1);
109 }
110 }
111 }
112
113 const results: Array<{
114 benchmark: string;
115 runtime: string;
116 exitCode: number;
117 duration_s: number;
118 }> = [];
119
120 for (const runtime of runtimes) {
121 console.log(`\n${"═".repeat(62)}`);
122 console.log(` Runtime: ${runtime.toUpperCase()}`);
123 console.log(`${"═".repeat(62)}\n`);
124
125 for (const bench of benchmarks) {
126 console.log(` ${ts()} ▶ ${bench.name} (${runtime})`);
127
128 const extraArgs: string[] = [];
129 if (QUICK) {
130 extraArgs.push(`--steps=${bench.quickSteps}`);
131 // Sustained leak: also reduce duration in quick mode
132 if (bench.name === "sustained-leak") {
133 extraArgs.push("--duration=60000");
134 }
135 }
136
137 const result = await runBenchmark(bench.script, runtime, extraArgs);
138 results.push({ benchmark: bench.name, runtime, ...result });
139
140 const status = result.exitCode === 0 ? "✓" : "✗";
141 console.log(
142 `\n ${ts()} ${status} ${bench.name} (${runtime}) — ${result.duration_s.toFixed(0)}s\n`,
143 );
144
145 // Cooldown between benchmarks
146 if (benchmarks.indexOf(bench) < benchmarks.length - 1) {
147 console.log(` ${ts()} Cooling down (5s)...`);
148 await new Promise((r) => setTimeout(r, 5000));
149 }
150 }
151 }
152
153 // ---------------------------------------------------------------------------
154 // Summary
155 // ---------------------------------------------------------------------------
156
157 console.log(`\n${"═".repeat(62)}`);
158 console.log(" BENCHMARK SUITE SUMMARY");
159 console.log(`${"═".repeat(62)}\n`);
160
161 const totalDuration = (Date.now() - t0) / 1000;
162 const passed = results.filter((r) => r.exitCode === 0).length;
163 const failed = results.filter((r) => r.exitCode !== 0).length;
164
165 console.log(` Total: ${results.length} benchmark runs`);
166 console.log(` Passed: ${passed}`);
167 console.log(` Failed: ${failed}`);
168 console.log(` Duration: ${(totalDuration / 60).toFixed(1)} minutes\n`);
169
170 // Table
171 console.log(" ┌────────────────────┬──────────┬────────┬────────────┐");
172 console.log(" │ Benchmark │ Runtime │ Status │ Duration │");
173 console.log(" ├────────────────────┼──────────┼────────┼────────────┤");
174 for (const r of results) {
175 const status = r.exitCode === 0 ? " ✓ " : " ✗ ";
176 console.log(
177 ` │ ${r.benchmark.padEnd(18)} │ ${r.runtime.padEnd(8)} │${status} │ ${(r.duration_s.toFixed(0) + "s").padStart(10)} │`,
178 );
179 }
180 console.log(" └────────────────────┴──────────┴────────┴────────────┘\n");
181
182 // List output files
183 const outputFiles = readdirSync(OUTPUT_DIR)
184 .filter((f) => f.startsWith("benchmark-"))
185 .sort()
186 .slice(-results.length * 2); // last N*2 files
187
188 if (outputFiles.length > 0) {
189 console.log(" Output files:");
190 for (const f of outputFiles) {
191 console.log(` ${OUTPUT_DIR}/${f}`);
192 }
193 console.log("");
194 }
195
196 console.log(` Run analysis: bun run scripts/benchmark-analyze.ts\n`);
197
198 process.exit(failed > 0 ? 1 : 0);
199}
200
201main().catch((err) => {
202 console.error("Fatal:", err);
203 process.exit(1);
204});