benchmark: add test double HTTP benchmarker

Refactor benchmark/_http-benchmarkers.js and add a test double
HTTP benchmarker for testing.

PR-URL: https://github.com/nodejs/node/pull/12121
Refs: https://github.com/nodejs/node/issues/12068
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Joyee Cheung 2017-03-29 23:27:51 +08:00
parent c0953945a8
commit a3e71a8901
2 changed files with 108 additions and 56 deletions

View File

@ -1,20 +1,23 @@
'use strict'; 'use strict';
const child_process = require('child_process'); const child_process = require('child_process');
const path = require('path');
const fs = require('fs');
// The port used by servers and wrk // The port used by servers and wrk
exports.PORT = process.env.PORT || 12346; exports.PORT = process.env.PORT || 12346;
function AutocannonBenchmarker() { class AutocannonBenchmarker {
constructor() {
this.name = 'autocannon'; this.name = 'autocannon';
this.autocannon_exe = process.platform === 'win32' ? this.executable = process.platform === 'win32' ?
'autocannon.cmd' : 'autocannon.cmd' :
'autocannon'; 'autocannon';
const result = child_process.spawnSync(this.autocannon_exe, ['-h']); const result = child_process.spawnSync(this.executable, ['-h']);
this.present = !(result.error && result.error.code === 'ENOENT'); this.present = !(result.error && result.error.code === 'ENOENT');
} }
AutocannonBenchmarker.prototype.create = function(options) { create(options) {
const args = [ const args = [
'-d', options.duration, '-d', options.duration,
'-c', options.connections, '-c', options.connections,
@ -22,53 +25,95 @@ AutocannonBenchmarker.prototype.create = function(options) {
'-n', '-n',
`http://127.0.0.1:${options.port}${options.path}` `http://127.0.0.1:${options.port}${options.path}`
]; ];
const child = child_process.spawn(this.autocannon_exe, args); const child = child_process.spawn(this.executable, args);
return child; return child;
}; }
AutocannonBenchmarker.prototype.processResults = function(output) { processResults(output) {
let result; let result;
try { try {
result = JSON.parse(output); result = JSON.parse(output);
} catch (err) { } catch (err) {
// Do nothing, let next line handle this return undefined;
} }
if (!result || !result.requests || !result.requests.average) { if (!result || !result.requests || !result.requests.average) {
return undefined; return undefined;
} else { } else {
return result.requests.average; return result.requests.average;
} }
}; }
}
function WrkBenchmarker() { class WrkBenchmarker {
constructor() {
this.name = 'wrk'; this.name = 'wrk';
this.regexp = /Requests\/sec:[ \t]+([0-9.]+)/; this.executable = 'wrk';
const result = child_process.spawnSync('wrk', ['-h']); const result = child_process.spawnSync(this.executable, ['-h']);
this.present = !(result.error && result.error.code === 'ENOENT'); this.present = !(result.error && result.error.code === 'ENOENT');
} }
WrkBenchmarker.prototype.create = function(options) { create(options) {
const args = [ const args = [
'-d', options.duration, '-d', options.duration,
'-c', options.connections, '-c', options.connections,
'-t', 8, '-t', 8,
`http://127.0.0.1:${options.port}${options.path}` `http://127.0.0.1:${options.port}${options.path}`
]; ];
const child = child_process.spawn('wrk', args); const child = child_process.spawn(this.executable, args);
return child; return child;
}; }
WrkBenchmarker.prototype.processResults = function(output) { processResults(output) {
const match = output.match(this.regexp); const throughputRe = /Requests\/sec:[ \t]+([0-9.]+)/;
const result = match && +match[1]; const match = output.match(throughputRe);
if (!isFinite(result)) { const throughput = match && +match[1];
if (!isFinite(throughput)) {
return undefined; return undefined;
} else { } else {
return result; return throughput;
}
}
} }
};
const http_benchmarkers = [new WrkBenchmarker(), new AutocannonBenchmarker()]; /**
* Simple, single-threaded benchmarker for testing if the benchmark
* works
*/
class TestDoubleBenchmarker {
constructor() {
this.name = 'test-double';
this.executable = path.resolve(__dirname, '_test-double-benchmarker.js');
this.present = fs.existsSync(this.executable);
}
create(options) {
const child = child_process.fork(this.executable, {
silent: true,
env: {
duration: options.duration,
connections: options.connections,
path: `http://127.0.0.1:${options.port}${options.path}`
}
});
return child;
}
processResults(output) {
let result;
try {
result = JSON.parse(output);
} catch (err) {
return undefined;
}
return result.throughput;
}
}
const http_benchmarkers = [
new WrkBenchmarker(),
new AutocannonBenchmarker(),
new TestDoubleBenchmarker()
];
const benchmarkers = {}; const benchmarkers = {};

View File

@ -0,0 +1,7 @@
'use strict';
const http = require('http');
http.get(process.env.path, function() {
console.log(JSON.stringify({throughput: 1}));
});