|
| 1 | +import autocannon from 'autocannon'; |
| 2 | + |
| 3 | +interface TestConfig { |
| 4 | + name: string; |
| 5 | + url: string; |
| 6 | + method?: 'GET' | 'POST'; |
| 7 | + duration?: number; |
| 8 | + connections?: number; |
| 9 | + pipelining?: number; |
| 10 | + headers?: Record<string, string>; |
| 11 | + body?: string; |
| 12 | + expectedMinRps?: number; |
| 13 | + expectedMaxP95Ms?: number; |
| 14 | +} |
| 15 | + |
| 16 | +const BASE_URL = process.env.API_URL || 'http://localhost:3001'; |
| 17 | +const DURATION = parseInt(process.env.LOAD_TEST_DURATION || '10', 10); |
| 18 | + |
| 19 | +const scenarios: TestConfig[] = [ |
| 20 | + { |
| 21 | + name: 'GET /health Baseline Load Test', |
| 22 | + url: `${BASE_URL}/health`, |
| 23 | + method: 'GET', |
| 24 | + duration: DURATION, |
| 25 | + connections: 50, |
| 26 | + pipelining: 1, |
| 27 | + expectedMinRps: 100, |
| 28 | + expectedMaxP95Ms: 200, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: 'GET /feed Query Load Test', |
| 32 | + url: `${BASE_URL}/feed`, |
| 33 | + method: 'GET', |
| 34 | + duration: DURATION, |
| 35 | + connections: 20, |
| 36 | + pipelining: 1, |
| 37 | + expectedMinRps: 10, |
| 38 | + expectedMaxP95Ms: 500, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'GET /articles Listing Load Test', |
| 42 | + url: `${BASE_URL}/articles`, |
| 43 | + method: 'GET', |
| 44 | + duration: DURATION, |
| 45 | + connections: 20, |
| 46 | + pipelining: 1, |
| 47 | + expectedMinRps: 10, |
| 48 | + expectedMaxP95Ms: 500, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: 'Throttler / Rate Limit Gate Test', |
| 52 | + url: `${BASE_URL}/health`, |
| 53 | + method: 'GET', |
| 54 | + duration: 5, |
| 55 | + connections: 100, |
| 56 | + pipelining: 2, |
| 57 | + }, |
| 58 | +]; |
| 59 | + |
| 60 | +async function runScenario(scenario: TestConfig): Promise<boolean> { |
| 61 | + console.log(`\n==================================================`); |
| 62 | + console.log(`Running Scenario: ${scenario.name}`); |
| 63 | + console.log(`URL: ${scenario.url} (${scenario.method || 'GET'})`); |
| 64 | + console.log(`Connections: ${scenario.connections}, Duration: ${scenario.duration}s`); |
| 65 | + console.log(`==================================================`); |
| 66 | + |
| 67 | + return new Promise((resolve) => { |
| 68 | + const instance = autocannon({ |
| 69 | + url: scenario.url, |
| 70 | + method: scenario.method || 'GET', |
| 71 | + connections: scenario.connections || 10, |
| 72 | + duration: scenario.duration || 10, |
| 73 | + pipelining: scenario.pipelining || 1, |
| 74 | + headers: scenario.headers, |
| 75 | + body: scenario.body, |
| 76 | + }); |
| 77 | + |
| 78 | + autocannon.track(instance, { renderProgressBar: false }); |
| 79 | + |
| 80 | + instance.on('done', (result) => { |
| 81 | + console.log(`\n📊 Results for ${scenario.name}:`); |
| 82 | + console.log(` Requests/sec (RPS): ${result.requests.average.toFixed(2)}`); |
| 83 | + console.log(` Latency Average: ${result.latency.average.toFixed(2)} ms`); |
| 84 | + console.log(` Latency P50: ${result.latency.p50} ms`); |
| 85 | + console.log(` Latency P95: ${result.latency.p95} ms`); |
| 86 | + console.log(` Latency P99: ${result.latency.p99} ms`); |
| 87 | + console.log(` Total Requests: ${result.requests.total}`); |
| 88 | + console.log(` 2xx Successes: ${result['2xx']}`); |
| 89 | + console.log(` 4xx Rate Limits: ${result['4xx']}`); |
| 90 | + console.log(` 5xx Errors: ${result['5xx']}`); |
| 91 | + console.log(` Non-2xx Responses: ${result.non2xx}`); |
| 92 | + |
| 93 | + let passed = true; |
| 94 | + if (result['5xx'] > 0) { |
| 95 | + console.error(`❌ FAILED: Received ${result['5xx']} 5xx server errors!`); |
| 96 | + passed = false; |
| 97 | + } |
| 98 | + |
| 99 | + if (scenario.expectedMinRps && result.requests.average < scenario.expectedMinRps) { |
| 100 | + console.warn( |
| 101 | + `⚠️ WARNING: RPS (${result.requests.average.toFixed(2)}) below target threshold of ${scenario.expectedMinRps}`, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + if (scenario.expectedMaxP95Ms && result.latency.p95 > scenario.expectedMaxP95Ms) { |
| 106 | + console.warn( |
| 107 | + `⚠️ WARNING: P95 Latency (${result.latency.p95}ms) exceeded target threshold of ${scenario.expectedMaxP95Ms}ms`, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + resolve(passed); |
| 112 | + }); |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +async function main() { |
| 117 | + console.log(`🚀 Starting Inferr API Load Test Suite`); |
| 118 | + let allPassed = true; |
| 119 | + |
| 120 | + for (const scenario of scenarios) { |
| 121 | + const passed = await runScenario(scenario); |
| 122 | + if (!passed) { |
| 123 | + allPassed = false; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + console.log(`\n==================================================`); |
| 128 | + if (allPassed) { |
| 129 | + console.log(`✅ All load test scenarios completed cleanly with 0 server errors.`); |
| 130 | + process.exit(0); |
| 131 | + } else { |
| 132 | + console.error(`❌ Load test suite completed with failures/errors.`); |
| 133 | + process.exit(1); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +main().catch((err) => { |
| 138 | + console.error('Fatal load test error:', err); |
| 139 | + process.exit(1); |
| 140 | +}); |
0 commit comments