Repository navigation
Expand file tree
/
Copy pathload-test.mjs
More file actions
63 lines (51 loc) · 1.6 KB
/
Copy pathload-test.mjs
File metadata and controls
63 lines (51 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Native Node.js Load Testing Script (Zero Dependencies)
// Usage: node scripts/load-test.mjs
const TARGET_URL = process.env.TARGET_URL || 'http://localhost:3000/api/streak?user=souravjhahind';
const CONCURRENCY = 50;
const DURATION_MS = 10000; // 10 seconds
console.info(`Starting native load test against ${TARGET_URL}...`);
console.info(`Concurrency: ${CONCURRENCY}, Duration: ${DURATION_MS / 1000}s\n`);
let totalRequests = 0;
let errors = 0;
let isRunning = true;
const startTime = Date.now();
// Stop the test after DURATION_MS
setTimeout(() => {
isRunning = false;
}, DURATION_MS);
async function worker() {
while (isRunning) {
try {
const res = await fetch(TARGET_URL);
// Consume the response body to avoid memory leaks
await res.arrayBuffer();
if (!res.ok) {
errors++;
}
totalRequests++;
} catch {
errors++;
totalRequests++;
}
}
}
async function run() {
const workers = [];
for (let i = 0; i < CONCURRENCY; i++) {
workers.push(worker());
}
await Promise.all(workers);
const durationSec = (Date.now() - startTime) / 1000;
const requestsPerSec = (totalRequests / durationSec).toFixed(2);
console.info('--- Load Test Results ---');
console.info(`Total Requests: ${totalRequests}`);
console.info(`Requests/sec: ${requestsPerSec}`);
console.info(`Errors: ${errors}`);
if (errors > 0) {
console.error('\nTest resulted in errors! The endpoint may be failing under load.');
process.exit(1);
} else {
console.info('\nSuccess! Endpoint handled the massive scaling test without errors.');
}
}
run();