|
| 1 | +import http2 from 'node:http2'; |
| 2 | + |
| 3 | +const REQUEST_COUNT = parseInt(process.env.REQUEST_COUNT ?? '10000', 10); |
| 4 | +const NUM_CONNECTIONS = parseInt(process.env.NUM_CONNECTIONS ?? '10', 10); |
| 5 | +const BASE_URL = process.env.RUNLOOP_BASE_URL ?? 'https://api.runloop.pro'; |
| 6 | +const API_KEY = process.env.RUNLOOP_API_KEY!; |
| 7 | + |
| 8 | +const body = JSON.stringify({ |
| 9 | + blueprint_id: 'bp_nonexistent_loadtest_00000', |
| 10 | + name: 'loadtest-h2-0', |
| 11 | + environment_variables: { TEST_VAR_1: 'value_one', TEST_VAR_2: 'value_two' }, |
| 12 | + metadata: { test_run: 'h2', index: '0' }, |
| 13 | + launch_parameters: { resource_size_request: 'SMALL', keep_alive_time_seconds: 300 }, |
| 14 | +}); |
| 15 | + |
| 16 | +function percentile(sorted: number[], p: number): number { |
| 17 | + const idx = Math.ceil((p / 100) * sorted.length) - 1; |
| 18 | + return sorted[Math.max(0, idx)]; |
| 19 | +} |
| 20 | + |
| 21 | +function connectH2(origin: string): Promise<http2.ClientHttp2Session> { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + const client = http2.connect(origin); |
| 24 | + client.on('connect', () => resolve(client)); |
| 25 | + client.on('error', reject); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +function sendRequest(client: http2.ClientHttp2Session): Promise<{ latencyMs: number; status: number }> { |
| 30 | + return new Promise((resolve, reject) => { |
| 31 | + const start = performance.now(); |
| 32 | + const req = client.request({ |
| 33 | + ':method': 'POST', |
| 34 | + ':path': '/v1/devboxes', |
| 35 | + 'content-type': 'application/json', |
| 36 | + authorization: `Bearer ${API_KEY}`, |
| 37 | + }); |
| 38 | + req.on('response', (headers) => { |
| 39 | + const status = headers[':status'] as number; |
| 40 | + req.on('data', () => {}); |
| 41 | + req.on('end', () => resolve({ latencyMs: performance.now() - start, status })); |
| 42 | + }); |
| 43 | + req.on('error', reject); |
| 44 | + req.end(body); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +async function main() { |
| 49 | + if (!Number.isInteger(NUM_CONNECTIONS) || NUM_CONNECTIONS < 1) { |
| 50 | + console.error(`NUM_CONNECTIONS must be a positive integer (got "${process.env.NUM_CONNECTIONS}")`); |
| 51 | + process.exit(1); |
| 52 | + } |
| 53 | + |
| 54 | + console.log(`HTTP/2 test: ${REQUEST_COUNT} requests, ${NUM_CONNECTIONS} connections to ${BASE_URL}`); |
| 55 | + |
| 56 | + const url = new URL(BASE_URL); |
| 57 | + const clients = await Promise.all(Array.from({ length: NUM_CONNECTIONS }, () => connectH2(url.origin))); |
| 58 | + |
| 59 | + const maxStreams = clients[0].remoteSettings?.maxConcurrentStreams; |
| 60 | + console.log(`Server MAX_CONCURRENT_STREAMS: ${maxStreams ?? 'unknown'}`); |
| 61 | + console.log(`${NUM_CONNECTIONS} connections established\n`); |
| 62 | + |
| 63 | + let completed = 0; |
| 64 | + const progressTimer = setInterval(() => { |
| 65 | + console.log( |
| 66 | + ` progress: ${completed}/${REQUEST_COUNT} (${((completed / REQUEST_COUNT) * 100).toFixed(1)}%)`, |
| 67 | + ); |
| 68 | + }, 2000); |
| 69 | + |
| 70 | + const wallStart = performance.now(); |
| 71 | + |
| 72 | + const promises = Array.from({ length: REQUEST_COUNT }, (_, i) => { |
| 73 | + const client = clients[i % NUM_CONNECTIONS]; |
| 74 | + return sendRequest(client).then((r) => { |
| 75 | + completed++; |
| 76 | + return r; |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + const results = await Promise.all(promises); |
| 81 | + const wallMs = performance.now() - wallStart; |
| 82 | + clearInterval(progressTimer); |
| 83 | + |
| 84 | + for (const c of clients) c.close(); |
| 85 | + |
| 86 | + const latencies = results.map((r) => r.latencyMs).sort((a, b) => a - b); |
| 87 | + const statusCounts = new Map<number, number>(); |
| 88 | + for (const r of results) { |
| 89 | + statusCounts.set(r.status, (statusCounts.get(r.status) ?? 0) + 1); |
| 90 | + } |
| 91 | + |
| 92 | + console.log(`\n=== HTTP/2 Results ===`); |
| 93 | + console.log(`Requests: ${REQUEST_COUNT}`); |
| 94 | + console.log(`Connections: ${NUM_CONNECTIONS}`); |
| 95 | + console.log(`Wall clock: ${(wallMs / 1000).toFixed(2)}s`); |
| 96 | + console.log(`Throughput: ${(REQUEST_COUNT / (wallMs / 1000)).toFixed(1)} req/s`); |
| 97 | + if (latencies.length > 0) { |
| 98 | + console.log(`\nLatency (ms):`); |
| 99 | + console.log(` min: ${latencies[0].toFixed(1)}`); |
| 100 | + console.log(` p50: ${percentile(latencies, 50).toFixed(1)}`); |
| 101 | + console.log(` p90: ${percentile(latencies, 90).toFixed(1)}`); |
| 102 | + console.log(` p95: ${percentile(latencies, 95).toFixed(1)}`); |
| 103 | + console.log(` p99: ${percentile(latencies, 99).toFixed(1)}`); |
| 104 | + console.log(` max: ${latencies[latencies.length - 1].toFixed(1)}`); |
| 105 | + } |
| 106 | + console.log(`\nStatus codes:`); |
| 107 | + for (const [s, c] of [...statusCounts.entries()].sort()) console.log(` ${s}: ${c}`); |
| 108 | +} |
| 109 | + |
| 110 | +main().catch((e) => { |
| 111 | + console.error(e); |
| 112 | + process.exit(1); |
| 113 | +}); |
0 commit comments