|
| 1 | +/** |
| 2 | + * Leak / soak test — run at modest concurrency for a configurable window, |
| 3 | + * sample heap and FD count, report slope. |
| 4 | + * |
| 5 | + * Run: `npx tsx loadtest/h2-leak.ts [durationSeconds=600]` |
| 6 | + */ |
| 7 | +import http2 from 'node:http2'; |
| 8 | +import { execSync } from 'node:child_process'; |
| 9 | +import fs from 'node:fs'; |
| 10 | +import os from 'node:os'; |
| 11 | +import path from 'node:path'; |
| 12 | +import { createH2Fetch } from '../src/lib/h2-transport/index'; |
| 13 | + |
| 14 | +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
| 15 | + |
| 16 | +function makeCerts() { |
| 17 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'h2-leak-')); |
| 18 | + const key = path.join(tmp, 'key.pem'); |
| 19 | + const cert = path.join(tmp, 'cert.pem'); |
| 20 | + execSync( |
| 21 | + `openssl req -x509 -newkey rsa:2048 -keyout ${key} -out ${cert} -days 1 -nodes -subj "/CN=localhost" 2>/dev/null`, |
| 22 | + ); |
| 23 | + return { key: fs.readFileSync(key), cert: fs.readFileSync(cert), tmp }; |
| 24 | +} |
| 25 | + |
| 26 | +function fdCount(): number | null { |
| 27 | + try { |
| 28 | + return fs.readdirSync('/proc/self/fd').length; |
| 29 | + } catch { |
| 30 | + return null; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function startServer() { |
| 35 | + const { key, cert, tmp } = makeCerts(); |
| 36 | + let streamN = 0; |
| 37 | + const server = http2.createSecureServer({ key, cert }); |
| 38 | + server.on('stream', (stream) => { |
| 39 | + streamN++; |
| 40 | + const n = streamN; |
| 41 | + stream.on('error', () => {}); |
| 42 | + stream.respond({ ':status': 200 }); |
| 43 | + stream.end(JSON.stringify({ n })); |
| 44 | + if (n % 1000 === 0) { |
| 45 | + try { |
| 46 | + stream.session?.goaway(); |
| 47 | + } catch {} |
| 48 | + } |
| 49 | + }); |
| 50 | + return new Promise<{ port: number; close: () => void }>((resolve) => { |
| 51 | + server.listen(0, () => { |
| 52 | + resolve({ |
| 53 | + port: (server.address() as any).port, |
| 54 | + close: () => { |
| 55 | + server.close(); |
| 56 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 57 | + }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +async function main() { |
| 64 | + const durationSec = Number(process.argv[2] ?? 600); |
| 65 | + const server = await startServer(); |
| 66 | + const fetch = createH2Fetch({ |
| 67 | + minConnections: 4, |
| 68 | + maxConnections: 20, |
| 69 | + tlsOptions: { rejectUnauthorized: false }, |
| 70 | + }); |
| 71 | + |
| 72 | + const startMs = Date.now(); |
| 73 | + const end = startMs + durationSec * 1000; |
| 74 | + let completed = 0; |
| 75 | + let failed = 0; |
| 76 | + const samples: Array<{ t: number; heap: number; fds: number | null }> = []; |
| 77 | + |
| 78 | + const sampler = setInterval(() => { |
| 79 | + if (global.gc) global.gc(); |
| 80 | + samples.push({ |
| 81 | + t: Math.round((Date.now() - startMs) / 1000), |
| 82 | + heap: process.memoryUsage().heapUsed, |
| 83 | + fds: fdCount(), |
| 84 | + }); |
| 85 | + }, 30_000); |
| 86 | + |
| 87 | + const RATE = 50; |
| 88 | + const tickMs = 20; |
| 89 | + const perTick = Math.max(1, Math.round((RATE * tickMs) / 1000)); |
| 90 | + |
| 91 | + while (Date.now() < end) { |
| 92 | + await Promise.all( |
| 93 | + Array.from({ length: perTick }, async () => { |
| 94 | + try { |
| 95 | + const r = (await fetch(`https://localhost:${server.port}/x`, { method: 'GET' } as any)) as any; |
| 96 | + await r.text(); |
| 97 | + completed++; |
| 98 | + } catch { |
| 99 | + failed++; |
| 100 | + } |
| 101 | + }), |
| 102 | + ); |
| 103 | + await new Promise((r) => setTimeout(r, tickMs)); |
| 104 | + } |
| 105 | + |
| 106 | + clearInterval(sampler); |
| 107 | + await fetch.close(); |
| 108 | + server.close(); |
| 109 | + |
| 110 | + const warmup = samples[Math.min(2, samples.length - 1)]?.heap ?? 0; |
| 111 | + const final = samples[samples.length - 1]?.heap ?? 0; |
| 112 | + const heapGrowthMB = (final - warmup) / (1024 * 1024); |
| 113 | + |
| 114 | + console.log(JSON.stringify({ |
| 115 | + durationSec, |
| 116 | + completed, |
| 117 | + failed, |
| 118 | + rps: Math.round(completed / durationSec), |
| 119 | + heapWarmupMB: Math.round(warmup / (1024 * 1024)), |
| 120 | + heapFinalMB: Math.round(final / (1024 * 1024)), |
| 121 | + heapGrowthMB: Math.round(heapGrowthMB), |
| 122 | + samples, |
| 123 | + }, null, 2)); |
| 124 | + |
| 125 | + if (heapGrowthMB > 50) { |
| 126 | + console.error(`HEAP GREW BY ${heapGrowthMB.toFixed(1)}MB — possible leak`); |
| 127 | + process.exit(1); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +main().catch((err) => { |
| 132 | + console.error(err); |
| 133 | + process.exit(1); |
| 134 | +}); |
0 commit comments