2016-08-05 09:34:50 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const child_process = require('child_process');
|
2017-03-29 15:27:51 +00:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2016-08-05 09:34:50 +00:00
|
|
|
|
2017-04-17 01:01:12 +00:00
|
|
|
const requirementsURL =
|
2023-04-03 23:52:21 +00:00
|
|
|
'https://github.com/nodejs/node/blob/HEAD/doc/contributing/writing-and-running-benchmarks.md#http-benchmark-requirements';
|
2017-04-17 01:01:12 +00:00
|
|
|
|
2016-08-05 09:34:50 +00:00
|
|
|
// The port used by servers and wrk
|
2018-10-17 21:27:54 +00:00
|
|
|
exports.PORT = Number(process.env.PORT) || 12346;
|
2016-08-05 09:34:50 +00:00
|
|
|
|
2017-03-29 15:27:51 +00:00
|
|
|
class AutocannonBenchmarker {
|
|
|
|
constructor() {
|
2024-03-15 04:59:51 +00:00
|
|
|
const shell = (process.platform === 'win32');
|
2017-03-29 15:27:51 +00:00
|
|
|
this.name = 'autocannon';
|
2024-03-15 04:59:51 +00:00
|
|
|
this.opts = { shell };
|
|
|
|
this.executable = shell ? 'autocannon.cmd' : 'autocannon';
|
|
|
|
const result = child_process.spawnSync(this.executable, ['-h'], this.opts);
|
|
|
|
if (shell) {
|
|
|
|
this.present = (result.status === 0);
|
|
|
|
} else {
|
|
|
|
this.present = !(result.error && result.error.code === 'ENOENT');
|
|
|
|
}
|
2017-03-29 15:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
create(options) {
|
|
|
|
const args = [
|
|
|
|
'-d', options.duration,
|
|
|
|
'-c', options.connections,
|
|
|
|
'-j',
|
|
|
|
'-n',
|
|
|
|
];
|
2019-02-11 05:30:15 +00:00
|
|
|
for (const field in options.headers) {
|
2024-03-15 04:59:51 +00:00
|
|
|
if (this.opts.shell) {
|
|
|
|
args.push('-H', `'${field}=${options.headers[field]}'`);
|
|
|
|
} else {
|
|
|
|
args.push('-H', `${field}=${options.headers[field]}`);
|
|
|
|
}
|
2019-02-11 05:30:15 +00:00
|
|
|
}
|
2020-12-23 13:52:44 +00:00
|
|
|
const scheme = options.scheme || 'http';
|
|
|
|
args.push(`${scheme}://127.0.0.1:${options.port}${options.path}`);
|
2024-03-15 04:59:51 +00:00
|
|
|
const child = child_process.spawn(this.executable, args, this.opts);
|
2017-03-29 15:27:51 +00:00
|
|
|
return child;
|
|
|
|
}
|
2016-08-05 09:34:50 +00:00
|
|
|
|
2017-03-29 15:27:51 +00:00
|
|
|
processResults(output) {
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(output);
|
2018-11-04 17:38:54 +00:00
|
|
|
} catch {
|
2017-03-29 15:27:51 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (!result || !result.requests || !result.requests.average) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-02-12 18:33:33 +00:00
|
|
|
return result.requests.average;
|
2017-03-29 15:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-05 09:34:50 +00:00
|
|
|
|
2017-03-29 15:27:51 +00:00
|
|
|
class WrkBenchmarker {
|
|
|
|
constructor() {
|
|
|
|
this.name = 'wrk';
|
|
|
|
this.executable = 'wrk';
|
|
|
|
const result = child_process.spawnSync(this.executable, ['-h']);
|
|
|
|
this.present = !(result.error && result.error.code === 'ENOENT');
|
2016-08-05 09:34:50 +00:00
|
|
|
}
|
2017-03-29 15:27:51 +00:00
|
|
|
|
|
|
|
create(options) {
|
2020-02-12 18:33:33 +00:00
|
|
|
const duration = typeof options.duration === 'number' ?
|
|
|
|
Math.max(options.duration, 1) :
|
|
|
|
options.duration;
|
2020-12-23 13:52:44 +00:00
|
|
|
const scheme = options.scheme || 'http';
|
2017-03-29 15:27:51 +00:00
|
|
|
const args = [
|
2020-02-12 18:33:33 +00:00
|
|
|
'-d', duration,
|
2017-03-29 15:27:51 +00:00
|
|
|
'-c', options.connections,
|
2023-01-02 04:15:37 +00:00
|
|
|
'-t', Math.min(options.connections, require('os').availableParallelism() || 8),
|
2020-12-23 13:52:44 +00:00
|
|
|
`${scheme}://127.0.0.1:${options.port}${options.path}`,
|
2017-03-29 15:27:51 +00:00
|
|
|
];
|
2020-01-14 18:03:19 +00:00
|
|
|
for (const field in options.headers) {
|
|
|
|
args.push('-H', `${field}: ${options.headers[field]}`);
|
|
|
|
}
|
2017-03-29 15:27:51 +00:00
|
|
|
const child = child_process.spawn(this.executable, args);
|
|
|
|
return child;
|
2016-08-05 09:34:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:27:51 +00:00
|
|
|
processResults(output) {
|
|
|
|
const throughputRe = /Requests\/sec:[ \t]+([0-9.]+)/;
|
|
|
|
const match = output.match(throughputRe);
|
|
|
|
const throughput = match && +match[1];
|
|
|
|
if (!isFinite(throughput)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-02-12 18:33:33 +00:00
|
|
|
return throughput;
|
2017-03-29 15:27:51 +00:00
|
|
|
}
|
2016-08-05 09:34:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:27:51 +00:00
|
|
|
/**
|
|
|
|
* Simple, single-threaded benchmarker for testing if the benchmark
|
|
|
|
* works
|
|
|
|
*/
|
|
|
|
class TestDoubleBenchmarker {
|
2018-10-25 02:23:25 +00:00
|
|
|
constructor(type) {
|
2020-12-23 13:52:44 +00:00
|
|
|
// `type` is the type of benchmarker. Possible values are 'http', 'https',
|
|
|
|
// and 'http2'.
|
2018-10-25 02:23:25 +00:00
|
|
|
this.name = `test-double-${type}`;
|
2017-03-29 15:27:51 +00:00
|
|
|
this.executable = path.resolve(__dirname, '_test-double-benchmarker.js');
|
|
|
|
this.present = fs.existsSync(this.executable);
|
2018-10-25 02:23:25 +00:00
|
|
|
this.type = type;
|
2017-03-29 15:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
create(options) {
|
2024-10-09 06:42:16 +00:00
|
|
|
process.env.duration ||= options.duration || 5;
|
2020-02-12 18:33:33 +00:00
|
|
|
|
2020-12-23 13:52:44 +00:00
|
|
|
const scheme = options.scheme || 'http';
|
2020-02-12 18:33:33 +00:00
|
|
|
const env = {
|
2020-12-23 13:52:44 +00:00
|
|
|
test_url: `${scheme}://127.0.0.1:${options.port}${options.path}`,
|
2023-01-29 18:13:35 +00:00
|
|
|
...process.env,
|
2020-02-12 18:33:33 +00:00
|
|
|
};
|
2018-01-25 16:47:18 +00:00
|
|
|
|
2018-10-25 02:23:25 +00:00
|
|
|
const child = child_process.fork(this.executable,
|
|
|
|
[this.type],
|
|
|
|
{ silent: true, env });
|
2017-03-29 15:27:51 +00:00
|
|
|
return child;
|
|
|
|
}
|
2016-08-05 09:34:50 +00:00
|
|
|
|
2017-03-29 15:27:51 +00:00
|
|
|
processResults(output) {
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(output);
|
2018-11-04 17:38:54 +00:00
|
|
|
} catch {
|
2017-03-29 15:27:51 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return result.throughput;
|
2016-08-05 09:34:50 +00:00
|
|
|
}
|
2017-03-29 15:27:51 +00:00
|
|
|
}
|
2016-08-05 09:34:50 +00:00
|
|
|
|
2017-07-17 17:29:42 +00:00
|
|
|
/**
|
|
|
|
* HTTP/2 Benchmarker
|
|
|
|
*/
|
|
|
|
class H2LoadBenchmarker {
|
|
|
|
constructor() {
|
|
|
|
this.name = 'h2load';
|
|
|
|
this.executable = 'h2load';
|
|
|
|
const result = child_process.spawnSync(this.executable, ['-h']);
|
|
|
|
this.present = !(result.error && result.error.code === 'ENOENT');
|
|
|
|
}
|
|
|
|
|
|
|
|
create(options) {
|
|
|
|
const args = [];
|
|
|
|
if (typeof options.requests === 'number')
|
|
|
|
args.push('-n', options.requests);
|
|
|
|
if (typeof options.clients === 'number')
|
|
|
|
args.push('-c', options.clients);
|
|
|
|
if (typeof options.threads === 'number')
|
|
|
|
args.push('-t', options.threads);
|
|
|
|
if (typeof options.maxConcurrentStreams === 'number')
|
|
|
|
args.push('-m', options.maxConcurrentStreams);
|
|
|
|
if (typeof options.initialWindowSize === 'number')
|
|
|
|
args.push('-w', options.initialWindowSize);
|
|
|
|
if (typeof options.sessionInitialWindowSize === 'number')
|
|
|
|
args.push('-W', options.sessionInitialWindowSize);
|
|
|
|
if (typeof options.rate === 'number')
|
|
|
|
args.push('-r', options.rate);
|
|
|
|
if (typeof options.ratePeriod === 'number')
|
|
|
|
args.push(`--rate-period=${options.ratePeriod}`);
|
|
|
|
if (typeof options.duration === 'number')
|
|
|
|
args.push('-T', options.duration);
|
|
|
|
if (typeof options.timeout === 'number')
|
|
|
|
args.push('-N', options.timeout);
|
|
|
|
if (typeof options.headerTableSize === 'number')
|
|
|
|
args.push(`--header-table-size=${options.headerTableSize}`);
|
|
|
|
if (typeof options.encoderHeaderTableSize === 'number') {
|
|
|
|
args.push(
|
|
|
|
`--encoder-header-table-size=${options.encoderHeaderTableSize}`);
|
|
|
|
}
|
|
|
|
const scheme = options.scheme || 'http';
|
|
|
|
const host = options.host || '127.0.0.1';
|
|
|
|
args.push(`${scheme}://${host}:${options.port}${options.path}`);
|
|
|
|
const child = child_process.spawn(this.executable, args);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
processResults(output) {
|
2021-04-11 23:57:59 +00:00
|
|
|
const rex = /(\d+\.\d+) req\/s/;
|
2017-07-17 17:29:42 +00:00
|
|
|
return rex.exec(output)[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 15:27:51 +00:00
|
|
|
const http_benchmarkers = [
|
|
|
|
new WrkBenchmarker(),
|
|
|
|
new AutocannonBenchmarker(),
|
2018-10-25 02:23:25 +00:00
|
|
|
new TestDoubleBenchmarker('http'),
|
2020-12-23 13:52:44 +00:00
|
|
|
new TestDoubleBenchmarker('https'),
|
2018-10-25 02:23:25 +00:00
|
|
|
new TestDoubleBenchmarker('http2'),
|
2019-01-29 22:41:33 +00:00
|
|
|
new H2LoadBenchmarker(),
|
2017-03-29 15:27:51 +00:00
|
|
|
];
|
2016-08-05 09:34:50 +00:00
|
|
|
|
|
|
|
const benchmarkers = {};
|
|
|
|
|
|
|
|
http_benchmarkers.forEach((benchmarker) => {
|
|
|
|
benchmarkers[benchmarker.name] = benchmarker;
|
|
|
|
if (!exports.default_http_benchmarker && benchmarker.present) {
|
|
|
|
exports.default_http_benchmarker = benchmarker.name;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.run = function(options, callback) {
|
2020-02-12 18:33:33 +00:00
|
|
|
options = {
|
2016-08-05 09:34:50 +00:00
|
|
|
port: exports.PORT,
|
|
|
|
path: '/',
|
|
|
|
connections: 100,
|
2018-01-25 16:09:55 +00:00
|
|
|
duration: 5,
|
2019-01-29 22:41:33 +00:00
|
|
|
benchmarker: exports.default_http_benchmarker,
|
2023-01-29 18:13:35 +00:00
|
|
|
...options,
|
2020-02-12 18:33:33 +00:00
|
|
|
};
|
2016-08-05 09:34:50 +00:00
|
|
|
if (!options.benchmarker) {
|
2017-06-24 03:52:07 +00:00
|
|
|
callback(new Error('Could not locate required http benchmarker. See ' +
|
|
|
|
`${requirementsURL} for further instructions.`));
|
2016-08-05 09:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const benchmarker = benchmarkers[options.benchmarker];
|
|
|
|
if (!benchmarker) {
|
2017-06-24 03:52:07 +00:00
|
|
|
callback(new Error(`Requested benchmarker '${options.benchmarker}' ` +
|
|
|
|
'is not supported'));
|
2016-08-05 09:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!benchmarker.present) {
|
2017-06-24 03:52:07 +00:00
|
|
|
callback(new Error(`Requested benchmarker '${options.benchmarker}' ` +
|
|
|
|
'is not installed'));
|
2016-08-05 09:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-20 09:06:16 +00:00
|
|
|
const benchmarker_start = process.hrtime.bigint();
|
2016-08-05 09:34:50 +00:00
|
|
|
|
|
|
|
const child = benchmarker.create(options);
|
|
|
|
|
|
|
|
child.stderr.pipe(process.stderr);
|
|
|
|
|
|
|
|
let stdout = '';
|
2020-02-12 18:33:33 +00:00
|
|
|
child.stdout.setEncoding('utf8');
|
|
|
|
child.stdout.on('data', (chunk) => stdout += chunk);
|
2016-08-05 09:34:50 +00:00
|
|
|
|
2019-01-29 22:41:33 +00:00
|
|
|
child.once('close', (code) => {
|
2021-05-20 09:06:16 +00:00
|
|
|
const benchmark_end = process.hrtime.bigint();
|
2016-08-05 09:34:50 +00:00
|
|
|
if (code) {
|
|
|
|
let error_message = `${options.benchmarker} failed with ${code}.`;
|
|
|
|
if (stdout !== '') {
|
|
|
|
error_message += ` Output: ${stdout}`;
|
|
|
|
}
|
|
|
|
callback(new Error(error_message), code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = benchmarker.processResults(stdout);
|
2016-12-31 21:30:05 +00:00
|
|
|
if (result === undefined) {
|
2017-04-17 01:01:12 +00:00
|
|
|
callback(new Error(
|
2017-04-17 01:11:54 +00:00
|
|
|
`${options.benchmarker} produced strange output: ${stdout}`), code);
|
2016-08-05 09:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-20 09:06:16 +00:00
|
|
|
const elapsed = benchmark_end - benchmarker_start;
|
2016-08-05 09:34:50 +00:00
|
|
|
callback(null, code, options.benchmarker, result, elapsed);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|