-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
48 lines (42 loc) · 1.35 KB
/
test.js
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
const autocannon = require('autocannon');
const url = 'http://localhost:3000';
const requestsPerSecond = 50;
const durationSeconds = 2;
// Function to run the test.
function runTest() {
console.log(`Running a test against ${url}...`);
const instance = autocannon(
{
url,
connections: 10, // 10 concurrent connections.
pipelining: 1, // No pipelining.
duration: durationSeconds, // Test duration in seconds.
requests: [
{
method: 'GET',
path: '/', // Test the root route.
},
{
method: 'GET',
path: '/api', // Test the /api route.
},
],
},
(err, results) => {
if (err) {
console.error('Error running the test:', err);
return;
}
console.log('Test complete. Results:', results);
}
);
// Listen to events for real-time feedback.
autocannon.track(instance, { renderProgressBar: true });
instance.on('response', (client, statusCode, resBytes, responseTime) => {
console.log(
`Response received - Status: ${statusCode}, Response Time: ${responseTime}ms`
);
});
}
// Run the test.
runTest();