roast-api 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/api/bn/roasts-bn-1.json +602 -2
- package/api/en/roasts-en-1.json +602 -2
- package/index.html +47 -0
- package/load_test.js +72 -0
- package/package.json +1 -1
package/load_test.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const RaaS = require('./api/client.js');
|
|
2
|
+
|
|
3
|
+
async function runLoadTest(totalRequests = 1000, concurrency = 50) {
|
|
4
|
+
console.log(`🚀 Starting Load Test: ${totalRequests} total requests, Concurrency: ${concurrency}\n`);
|
|
5
|
+
|
|
6
|
+
const results = {
|
|
7
|
+
latencies: [],
|
|
8
|
+
errors: 0,
|
|
9
|
+
networkRequests: 0,
|
|
10
|
+
startTime: Date.now()
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// Instrument RaaS fetch to count network hits
|
|
14
|
+
const originalFetch = global.fetch;
|
|
15
|
+
global.fetch = async (...args) => {
|
|
16
|
+
results.networkRequests++;
|
|
17
|
+
return originalFetch(...args);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const languages = ['en', 'bn'];
|
|
21
|
+
let count = totalRequests;
|
|
22
|
+
|
|
23
|
+
async function worker() {
|
|
24
|
+
while (count > 0) {
|
|
25
|
+
count--;
|
|
26
|
+
const start = Date.now();
|
|
27
|
+
try {
|
|
28
|
+
const lang = languages[Math.floor(Math.random() * languages.length)];
|
|
29
|
+
await RaaS.getRandomRoast({ lang });
|
|
30
|
+
results.latencies.push(Date.now() - start);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
results.errors++;
|
|
33
|
+
console.error(`❌ Request failed: ${err.message}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Spawn workers
|
|
39
|
+
const workers = Array(concurrency).fill(0).map(() => worker());
|
|
40
|
+
await Promise.all(workers);
|
|
41
|
+
|
|
42
|
+
// Restore fetch
|
|
43
|
+
global.fetch = originalFetch;
|
|
44
|
+
|
|
45
|
+
// Results Calculation
|
|
46
|
+
const totalTime = (Date.now() - results.startTime) / 1000;
|
|
47
|
+
const avgLatency = results.latencies.reduce((a, b) => a + b, 0) / results.latencies.length;
|
|
48
|
+
results.latencies.sort((a, b) => a - b);
|
|
49
|
+
const p95 = results.latencies[Math.floor(results.latencies.length * 0.95)] || 0;
|
|
50
|
+
const p99 = results.latencies[Math.floor(results.latencies.length * 0.99)] || 0;
|
|
51
|
+
|
|
52
|
+
console.log('-------------------------------------------');
|
|
53
|
+
console.log('📊 LOAD TEST RESULTS (INSTRUMENTED)');
|
|
54
|
+
console.log('-------------------------------------------');
|
|
55
|
+
console.log(`Total Time: ${totalTime.toFixed(2)}s`);
|
|
56
|
+
console.log(`Requests/sec: ${(totalRequests / totalTime).toFixed(2)}`);
|
|
57
|
+
console.log(`Success Rate: ${((totalRequests - results.errors) / totalRequests * 100).toFixed(2)}%`);
|
|
58
|
+
console.log(`Avg Latency: ${avgLatency.toFixed(2)}ms`);
|
|
59
|
+
console.log(`P95 Latency: ${p95}ms`);
|
|
60
|
+
console.log(`P99 Latency: ${p99}ms`);
|
|
61
|
+
console.log(`Network Requests: ${results.networkRequests}`);
|
|
62
|
+
console.log(`Cache Hits: ${totalRequests - results.networkRequests}`);
|
|
63
|
+
console.log(`Cache Efficiency: ${((1 - results.networkRequests / totalRequests) * 100).toFixed(2)}%`);
|
|
64
|
+
console.log(`Total Errors: ${results.errors}`);
|
|
65
|
+
console.log('-------------------------------------------');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Get args or defaults
|
|
69
|
+
const requests = parseInt(process.argv[2]) || 1000;
|
|
70
|
+
const concurrency = parseInt(process.argv[3]) || 50;
|
|
71
|
+
|
|
72
|
+
runLoadTest(requests, concurrency);
|