2017-03-29 17:27:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-10-25 04:23:25 +02:00
|
|
|
const myModule = process.argv[2];
|
|
|
|
if (!['http', 'http2'].includes(myModule)) {
|
|
|
|
throw new Error(`Invalid module for benchmark test double: ${myModule}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const http = require(myModule);
|
2017-03-29 17:27:51 +02:00
|
|
|
|
2020-02-12 19:33:33 +01:00
|
|
|
const duration = +process.env.duration;
|
2018-01-25 17:47:18 +01:00
|
|
|
const url = process.env.test_url;
|
|
|
|
|
|
|
|
const start = process.hrtime();
|
|
|
|
let throughput = 0;
|
|
|
|
|
2018-10-25 04:23:25 +02:00
|
|
|
function request(res, client) {
|
|
|
|
res.resume();
|
2018-01-25 17:47:18 +01:00
|
|
|
res.on('error', () => {});
|
|
|
|
res.on('end', () => {
|
|
|
|
throughput++;
|
2020-02-12 19:33:33 +01:00
|
|
|
const [sec, nanosec] = process.hrtime(start);
|
|
|
|
const ms = sec * 1000 + nanosec / 1e6;
|
|
|
|
if (ms < duration * 1000) {
|
2018-01-25 17:47:18 +01:00
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
console.log(JSON.stringify({ throughput }));
|
2018-10-25 04:23:25 +02:00
|
|
|
if (client) {
|
|
|
|
client.destroy();
|
2020-02-12 19:33:33 +01:00
|
|
|
process.exit(0);
|
2018-10-25 04:23:25 +02:00
|
|
|
}
|
2018-01-25 17:47:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
2018-10-25 04:23:25 +02:00
|
|
|
if (http.get) { // HTTP
|
|
|
|
http.get(url, request);
|
2020-02-12 19:33:33 +01:00
|
|
|
} else { // HTTP/2
|
2018-10-25 04:23:25 +02:00
|
|
|
const client = http.connect(url);
|
|
|
|
client.on('error', (e) => { throw e; });
|
|
|
|
request(client.request(), client);
|
|
|
|
}
|
2018-01-25 17:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
run();
|