0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 03:07:54 +01:00
nodejs/benchmark/_test-double-benchmarker.js
Rich Trott 1c4fa9a48a benchmark: fix http2 benchmarks
Fixes: https://github.com/nodejs/node/issues/36746

PR-URL: https://github.com/nodejs/node/pull/36871
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
2021-01-10 20:26:54 -08:00

55 lines
1.1 KiB
JavaScript

'use strict';
const myModule = process.argv[2];
if (!['http', 'https', 'http2'].includes(myModule)) {
throw new Error(`Invalid module for benchmark test double: ${myModule}`);
}
let options;
if (myModule === 'https') {
options = { rejectUnauthorized: false };
}
const http = require(myModule);
const duration = +process.env.duration;
const url = process.env.test_url;
const start = process.hrtime();
let throughput = 0;
function request(res, client) {
res.resume();
res.on('error', () => {});
res.on('end', () => {
throughput++;
const [sec, nanosec] = process.hrtime(start);
const ms = sec * 1000 + nanosec / 1e6;
if (ms < duration * 1000) {
run();
} else {
console.log(JSON.stringify({ throughput }));
if (client) {
client.destroy();
process.exit(0);
}
}
});
}
function run() {
if (http.get) { // HTTP or HTTPS
if (options) {
http.get(url, options, request);
} else {
http.get(url, request);
}
} else { // HTTP/2
const client = http.connect(url);
client.on('error', () => {});
request(client.request(), client);
}
}
run();