|
| 1 | +const DURATION_MS = 10_000; |
| 2 | +const CONCURRENCY = 50; |
| 3 | +const WARMUP_MS = 2_000; |
| 4 | + |
| 5 | +async function loadTest(url: string, durationMs: number, concurrency: number): Promise<{ |
| 6 | + rps: number; |
| 7 | + avgLatencyMs: number; |
| 8 | + p99LatencyMs: number; |
| 9 | + errors: number; |
| 10 | + total: number; |
| 11 | +}> { |
| 12 | + let completed = 0; |
| 13 | + let errors = 0; |
| 14 | + const latencies: number[] = []; |
| 15 | + const deadline = Date.now() + durationMs; |
| 16 | + |
| 17 | + async function worker() { |
| 18 | + while (Date.now() < deadline) { |
| 19 | + const start = performance.now(); |
| 20 | + try { |
| 21 | + const res = await fetch(url); |
| 22 | + await res.text(); |
| 23 | + latencies.push(performance.now() - start); |
| 24 | + completed++; |
| 25 | + } catch { |
| 26 | + errors++; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + const workers = Array.from({ length: concurrency }, () => worker()); |
| 32 | + await Promise.all(workers); |
| 33 | + |
| 34 | + latencies.sort((a, b) => a - b); |
| 35 | + const avg = latencies.reduce((s, v) => s + v, 0) / latencies.length; |
| 36 | + const p99 = latencies[Math.floor(latencies.length * 0.99)] ?? 0; |
| 37 | + |
| 38 | + return { |
| 39 | + rps: Math.round((completed / durationMs) * 1000), |
| 40 | + avgLatencyMs: Math.round(avg * 100) / 100, |
| 41 | + p99LatencyMs: Math.round(p99 * 100) / 100, |
| 42 | + errors, |
| 43 | + total: completed, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +async function waitReady(url: string, timeoutMs = 5000) { |
| 48 | + const deadline = Date.now() + timeoutMs; |
| 49 | + while (Date.now() < deadline) { |
| 50 | + try { |
| 51 | + await fetch(url); |
| 52 | + return; |
| 53 | + } catch { |
| 54 | + await new Promise((r) => setTimeout(r, 100)); |
| 55 | + } |
| 56 | + } |
| 57 | + throw new Error(`Server not ready: ${url}`); |
| 58 | +} |
| 59 | + |
| 60 | +async function bench(name: string, url: string) { |
| 61 | + console.log(`\n[${name}] ${url}`); |
| 62 | + console.log(" Waiting for server..."); |
| 63 | + await waitReady(url); |
| 64 | + |
| 65 | + console.log(` Warmup ${WARMUP_MS / 1000}s...`); |
| 66 | + await loadTest(url, WARMUP_MS, CONCURRENCY); |
| 67 | + |
| 68 | + console.log(` Benchmarking ${DURATION_MS / 1000}s, concurrency=${CONCURRENCY}...`); |
| 69 | + const result = await loadTest(url, DURATION_MS, CONCURRENCY); |
| 70 | + |
| 71 | + console.log(` RPS: ${result.rps}`); |
| 72 | + console.log(` Avg latency: ${result.avgLatencyMs} ms`); |
| 73 | + console.log(` P99 latency: ${result.p99LatencyMs} ms`); |
| 74 | + console.log(` Total reqs: ${result.total}`); |
| 75 | + console.log(` Errors: ${result.errors}`); |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +const targets = [ |
| 80 | + { name: "Gerbil", url: "http://127.0.0.1:8080/" }, |
| 81 | + { name: "Hono (Deno)", url: "http://127.0.0.1:8000/" }, |
| 82 | + { name: "Python (stdlib)", url: "http://127.0.0.1:8001/" }, |
| 83 | +]; |
| 84 | + |
| 85 | +const results: Record<string, { rps: number; avgLatencyMs: number; p99LatencyMs: number }> = {}; |
| 86 | + |
| 87 | +const shuffled = [...targets].sort(() => Math.random() - 0.5); |
| 88 | +console.log(`\nBench order: ${shuffled.map((t) => t.name).join(" → ")}`); |
| 89 | + |
| 90 | +for (const t of shuffled) { |
| 91 | + const r = await bench(t.name, t.url); |
| 92 | + results[t.name] = r; |
| 93 | +} |
| 94 | + |
| 95 | +console.log("\n=== Summary ==="); |
| 96 | +const entries = Object.entries(results).sort((a, b) => b[1].rps - a[1].rps); |
| 97 | +const baseline = entries[entries.length - 1][1].rps; |
| 98 | +for (const [name, r] of entries) { |
| 99 | + const ratio = (r.rps / baseline).toFixed(2); |
| 100 | + console.log(`${name.padEnd(20)} RPS: ${String(r.rps).padStart(6)} avg: ${r.avgLatencyMs}ms p99: ${r.p99LatencyMs}ms (${ratio}x)`); |
| 101 | +} |
0 commit comments