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