mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
1760c23f75
This reverts commit 4671d551cf
and
contains a fix to the issue raised for the revert.
PR-URL: https://github.com/nodejs/node/pull/31755
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
46 lines
977 B
JavaScript
46 lines
977 B
JavaScript
'use strict';
|
|
|
|
const myModule = process.argv[2];
|
|
if (!['http', 'http2'].includes(myModule)) {
|
|
throw new Error(`Invalid module for benchmark test double: ${myModule}`);
|
|
}
|
|
|
|
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
|
|
http.get(url, request);
|
|
} else { // HTTP/2
|
|
const client = http.connect(url);
|
|
client.on('error', (e) => { throw e; });
|
|
request(client.request(), client);
|
|
}
|
|
}
|
|
|
|
run();
|