2016-08-05 11:34:50 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const child_process = require('child_process');
|
2017-03-29 17:27:51 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2016-08-05 11:34:50 +02:00
|
|
|
|
2017-04-17 03:01:12 +02:00
|
|
|
const requirementsURL =
|
2017-04-17 03:09:50 +02:00
|
|
|
'https://github.com/nodejs/node/blob/master/doc/guides/writing-and-running-benchmarks.md#http-benchmark-requirements';
|
2017-04-17 03:01:12 +02:00
|
|
|
|
2016-08-05 11:34:50 +02:00
|
|
|
// The port used by servers and wrk
|
2018-10-17 23:27:54 +02:00
|
|
|
exports.PORT = Number(process.env.PORT) || 12346;
|
2016-08-05 11:34:50 +02:00
|
|
|
|
2017-03-29 17:27:51 +02:00
|
|
|
class AutocannonBenchmarker {
|
|
|
|
constructor() {
|
|
|
|
this.name = 'autocannon';
|
2017-06-24 05:52:07 +02:00
|
|
|
this.executable =
|
|
|
|
process.platform === 'win32' ? 'autocannon.cmd' : 'autocannon';
|
2017-03-29 17:27:51 +02:00
|
|
|
const result = child_process.spawnSync(this.executable, ['-h']);
|
|
|
|
this.present = !(result.error && result.error.code === 'ENOENT');
|
|
|
|
}
|
|
|
|
|
|
|
|
create(options) {
|
|
|
|
const args = [
|
|
|
|
'-d', options.duration,
|
|
|
|
'-c', options.connections,
|
|
|
|
'-j',
|
|
|
|
'-n',
|
|
|
|
`http://127.0.0.1:${options.port}${options.path}`
|
|
|
|
];
|
|
|
|
const child = child_process.spawn(this.executable, args);
|
|
|
|
return child;
|
|
|
|
}
|
2016-08-05 11:34:50 +02:00
|
|
|
|
2017-03-29 17:27:51 +02:00
|
|
|
processResults(output) {
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(output);
|
|
|
|
} catch (err) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (!result || !result.requests || !result.requests.average) {
|
|
|
|
return undefined;
|
|
|
|
} else {
|
|
|
|
return result.requests.average;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 11:34:50 +02:00
|
|
|
|
2017-03-29 17:27:51 +02: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 11:34:50 +02:00
|
|
|
}
|
2017-03-29 17:27:51 +02:00
|
|
|
|
|
|
|
create(options) {
|
|
|
|
const args = [
|
|
|
|
'-d', options.duration,
|
|
|
|
'-c', options.connections,
|
|
|
|
'-t', 8,
|
|
|
|
`http://127.0.0.1:${options.port}${options.path}`
|
|
|
|
];
|
|
|
|
const child = child_process.spawn(this.executable, args);
|
|
|
|
return child;
|
2016-08-05 11:34:50 +02:00
|
|
|
}
|
|
|
|
|
2017-03-29 17:27:51 +02: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;
|
|
|
|
} else {
|
|
|
|
return throughput;
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 11:34:50 +02:00
|
|
|
}
|
|
|
|
|
2017-03-29 17:27:51 +02:00
|
|
|
/**
|
|
|
|
* Simple, single-threaded benchmarker for testing if the benchmark
|
|
|
|
* works
|
|
|
|
*/
|
|
|
|
class TestDoubleBenchmarker {
|
2018-10-25 04:23:25 +02:00
|
|
|
constructor(type) {
|
|
|
|
// `type` is the type ofbenchmarker. Possible values are 'http' and 'http2'.
|
|
|
|
this.name = `test-double-${type}`;
|
2017-03-29 17:27:51 +02:00
|
|
|
this.executable = path.resolve(__dirname, '_test-double-benchmarker.js');
|
|
|
|
this.present = fs.existsSync(this.executable);
|
2018-10-25 04:23:25 +02:00
|
|
|
this.type = type;
|
2017-03-29 17:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
create(options) {
|
2018-01-25 17:47:18 +01:00
|
|
|
const env = Object.assign({
|
|
|
|
duration: options.duration,
|
|
|
|
test_url: `http://127.0.0.1:${options.port}${options.path}`,
|
|
|
|
}, process.env);
|
|
|
|
|
2018-10-25 04:23:25 +02:00
|
|
|
const child = child_process.fork(this.executable,
|
|
|
|
[this.type],
|
|
|
|
{ silent: true, env });
|
2017-03-29 17:27:51 +02:00
|
|
|
return child;
|
|
|
|
}
|
2016-08-05 11:34:50 +02:00
|
|
|
|
2017-03-29 17:27:51 +02:00
|
|
|
processResults(output) {
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(output);
|
|
|
|
} catch (err) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return result.throughput;
|
2016-08-05 11:34:50 +02:00
|
|
|
}
|
2017-03-29 17:27:51 +02:00
|
|
|
}
|
2016-08-05 11:34:50 +02:00
|
|
|
|
2017-07-17 19:29:42 +02: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) {
|
|
|
|
const rex = /(\d+(?:\.\d+)) req\/s/;
|
|
|
|
return rex.exec(output)[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 17:27:51 +02:00
|
|
|
const http_benchmarkers = [
|
|
|
|
new WrkBenchmarker(),
|
|
|
|
new AutocannonBenchmarker(),
|
2018-10-25 04:23:25 +02:00
|
|
|
new TestDoubleBenchmarker('http'),
|
|
|
|
new TestDoubleBenchmarker('http2'),
|
2017-07-17 19:29:42 +02:00
|
|
|
new H2LoadBenchmarker()
|
2017-03-29 17:27:51 +02:00
|
|
|
];
|
2016-08-05 11:34:50 +02: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) {
|
|
|
|
options = Object.assign({
|
|
|
|
port: exports.PORT,
|
|
|
|
path: '/',
|
|
|
|
connections: 100,
|
2018-01-25 17:09:55 +01:00
|
|
|
duration: 5,
|
2016-08-05 11:34:50 +02:00
|
|
|
benchmarker: exports.default_http_benchmarker
|
|
|
|
}, options);
|
|
|
|
if (!options.benchmarker) {
|
2017-06-24 05:52:07 +02:00
|
|
|
callback(new Error('Could not locate required http benchmarker. See ' +
|
|
|
|
`${requirementsURL} for further instructions.`));
|
2016-08-05 11:34:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const benchmarker = benchmarkers[options.benchmarker];
|
|
|
|
if (!benchmarker) {
|
2017-06-24 05:52:07 +02:00
|
|
|
callback(new Error(`Requested benchmarker '${options.benchmarker}' ` +
|
|
|
|
'is not supported'));
|
2016-08-05 11:34:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!benchmarker.present) {
|
2017-06-24 05:52:07 +02:00
|
|
|
callback(new Error(`Requested benchmarker '${options.benchmarker}' ` +
|
|
|
|
'is not installed'));
|
2016-08-05 11:34:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const benchmarker_start = process.hrtime();
|
|
|
|
|
|
|
|
const child = benchmarker.create(options);
|
|
|
|
|
|
|
|
child.stderr.pipe(process.stderr);
|
|
|
|
|
|
|
|
let stdout = '';
|
|
|
|
child.stdout.on('data', (chunk) => stdout += chunk.toString());
|
|
|
|
|
|
|
|
child.once('close', function(code) {
|
|
|
|
const elapsed = process.hrtime(benchmarker_start);
|
|
|
|
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 22:30:05 +01:00
|
|
|
if (result === undefined) {
|
2017-04-17 03:01:12 +02:00
|
|
|
callback(new Error(
|
2017-04-17 03:11:54 +02:00
|
|
|
`${options.benchmarker} produced strange output: ${stdout}`), code);
|
2016-08-05 11:34:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, code, options.benchmarker, result, elapsed);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|