forked from serverless/examples
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.js
38 lines (32 loc) · 1.06 KB
/
bench.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
const http = require('http');
const url = 'http://localhost:3000/hello';
const nbRequests = 1000;
const bench = async () => {
console.info(nbRequests + ' "GET" requests to "' + url + '"');
console.time('Total');
const results = [];
for (let i = 1; i <= nbRequests; i++) {
await new Promise((resolve, reject) => {
var begin = Date.now();
http.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Calculate the time spent
resp.on('end', () => {
results.push(Date.now() - begin);
resolve();
});
}).on('error', (error) => {
reject(error);
});
});
}
console.timeEnd('Total');
console.info('Average: ', results.reduce((p, c) => {
return p + c;
}) / results.length + 'ms');
};
bench();