|
| 1 | +/** |
| 2 | + * Flight Proxy throughput benchmark. |
| 3 | + * |
| 4 | + * Spawns the proxy with a mock MCP server and measures: |
| 5 | + * - Small calls: 1000 x ~1KB requests → calls/sec |
| 6 | + * - Large calls: 50 x ~100KB requests → MB/sec |
| 7 | + * |
| 8 | + * Usage: npx tsx bench/throughput.ts |
| 9 | + */ |
| 10 | + |
| 11 | +import { spawn } from "node:child_process"; |
| 12 | +import { createInterface } from "node:readline"; |
| 13 | +import { join } from "node:path"; |
| 14 | +import { tmpdir } from "node:os"; |
| 15 | +import { rm } from "node:fs/promises"; |
| 16 | + |
| 17 | +const MOCK_SERVER = join(import.meta.dirname, "..", "test", "mock-mcp-server.ts"); |
| 18 | +const PROXY_MODULE = join(import.meta.dirname, "..", "src", "proxy.ts"); |
| 19 | + |
| 20 | +function createProxy(logDir: string) { |
| 21 | + const child = spawn("npx", ["tsx", "-e", ` |
| 22 | + import { startProxy } from "${PROXY_MODULE.replace(/\\/g, "/")}"; |
| 23 | + startProxy({ |
| 24 | + command: "npx", |
| 25 | + args: ["tsx", "${MOCK_SERVER.replace(/\\/g, "/")}"], |
| 26 | + logDir: "${logDir.replace(/\\/g, "/")}", |
| 27 | + quiet: true, |
| 28 | + }); |
| 29 | + `], { stdio: ["pipe", "pipe", "pipe"] }); |
| 30 | + |
| 31 | + let responseCount = 0; |
| 32 | + let totalResponseBytes = 0; |
| 33 | + const rl = createInterface({ input: child.stdout! }); |
| 34 | + rl.on("line", (line) => { |
| 35 | + responseCount++; |
| 36 | + totalResponseBytes += Buffer.byteLength(line); |
| 37 | + }); |
| 38 | + |
| 39 | + function send(msg: Record<string, unknown>) { |
| 40 | + child.stdin!.write(JSON.stringify(msg) + "\n"); |
| 41 | + } |
| 42 | + |
| 43 | + function waitForResponses(count: number, timeoutMs = 60000): Promise<void> { |
| 44 | + return new Promise((resolve, reject) => { |
| 45 | + const start = Date.now(); |
| 46 | + const check = () => { |
| 47 | + if (responseCount >= count) return resolve(); |
| 48 | + if (Date.now() - start > timeoutMs) return reject(new Error(`Timeout: got ${responseCount}/${count}`)); |
| 49 | + setTimeout(check, 10); |
| 50 | + }; |
| 51 | + check(); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + send, |
| 57 | + waitForResponses, |
| 58 | + close: () => { child.stdin!.end(); child.kill(); }, |
| 59 | + get count() { return responseCount; }, |
| 60 | + get bytes() { return totalResponseBytes; }, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +async function benchSmallCalls() { |
| 65 | + const logDir = join(tmpdir(), `flight-bench-small-${Date.now()}`); |
| 66 | + const proxy = createProxy(logDir); |
| 67 | + const CALL_COUNT = 1000; |
| 68 | + |
| 69 | + // Initialize |
| 70 | + proxy.send({ |
| 71 | + jsonrpc: "2.0", id: 0, method: "initialize", |
| 72 | + params: { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "bench", version: "1.0" } }, |
| 73 | + }); |
| 74 | + await proxy.waitForResponses(1); |
| 75 | + |
| 76 | + const start = Date.now(); |
| 77 | + |
| 78 | + for (let i = 1; i <= CALL_COUNT; i++) { |
| 79 | + proxy.send({ |
| 80 | + jsonrpc: "2.0", id: i, method: "tools/call", |
| 81 | + params: { name: "read_file", arguments: { path: `/file_${i}.ts` } }, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + await proxy.waitForResponses(CALL_COUNT + 1); |
| 86 | + const elapsed = Date.now() - start; |
| 87 | + const callsPerSec = Math.round(CALL_COUNT / (elapsed / 1000)); |
| 88 | + |
| 89 | + proxy.close(); |
| 90 | + await rm(logDir, { recursive: true }).catch(() => {}); |
| 91 | + |
| 92 | + return { callCount: CALL_COUNT, elapsed, callsPerSec }; |
| 93 | +} |
| 94 | + |
| 95 | +async function benchLargeCalls() { |
| 96 | + const logDir = join(tmpdir(), `flight-bench-large-${Date.now()}`); |
| 97 | + const proxy = createProxy(logDir); |
| 98 | + const CALL_COUNT = 50; |
| 99 | + |
| 100 | + // Initialize |
| 101 | + proxy.send({ |
| 102 | + jsonrpc: "2.0", id: 0, method: "initialize", |
| 103 | + params: { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "bench", version: "1.0" } }, |
| 104 | + }); |
| 105 | + await proxy.waitForResponses(1); |
| 106 | + |
| 107 | + const start = Date.now(); |
| 108 | + |
| 109 | + for (let i = 1; i <= CALL_COUNT; i++) { |
| 110 | + proxy.send({ |
| 111 | + jsonrpc: "2.0", id: i, method: "tools/call", |
| 112 | + params: { name: "list_dir", arguments: { path: `/dir_${i}` } }, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + await proxy.waitForResponses(CALL_COUNT + 1); |
| 117 | + const elapsed = Date.now() - start; |
| 118 | + const totalMB = proxy.bytes / (1024 * 1024); |
| 119 | + const mbPerSec = totalMB / (elapsed / 1000); |
| 120 | + |
| 121 | + proxy.close(); |
| 122 | + await rm(logDir, { recursive: true }).catch(() => {}); |
| 123 | + |
| 124 | + return { callCount: CALL_COUNT, elapsed, totalMB, mbPerSec }; |
| 125 | +} |
| 126 | + |
| 127 | +async function main() { |
| 128 | + console.log("Flight Proxy Throughput Benchmark"); |
| 129 | + console.log("=================================\n"); |
| 130 | + |
| 131 | + console.log("Running small-call benchmark (1000 x ~1KB)..."); |
| 132 | + const small = await benchSmallCalls(); |
| 133 | + console.log(` ${small.callCount} calls in ${small.elapsed}ms → ${small.callsPerSec} calls/sec\n`); |
| 134 | + |
| 135 | + console.log("Running large-call benchmark (50 x ~100KB)..."); |
| 136 | + const large = await benchLargeCalls(); |
| 137 | + console.log(` ${large.callCount} calls in ${large.elapsed}ms → ${large.mbPerSec.toFixed(2)} MB/sec\n`); |
| 138 | + |
| 139 | + console.log("Summary"); |
| 140 | + console.log("-------"); |
| 141 | + console.log(` Small calls: ${small.callsPerSec} calls/sec`); |
| 142 | + console.log(` Large calls: ${large.mbPerSec.toFixed(2)} MB/sec`); |
| 143 | +} |
| 144 | + |
| 145 | +main().catch((err) => { |
| 146 | + console.error("Benchmark failed:", err); |
| 147 | + process.exit(1); |
| 148 | +}); |
0 commit comments