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,74 +1,119 @@
'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 {
this.name = 'autocannon'; constructor() {
this.autocannon_exe = process.platform === 'win32' ? this.name = 'autocannon';
'autocannon.cmd' : this.executable = process.platform === 'win32' ?
'autocannon'; 'autocannon.cmd' :
const result = child_process.spawnSync(this.autocannon_exe, ['-h']); 'autocannon';
this.present = !(result.error && result.error.code === 'ENOENT'); 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;
}
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;
}
}
} }
AutocannonBenchmarker.prototype.create = function(options) { class WrkBenchmarker {
const args = [ constructor() {
'-d', options.duration, this.name = 'wrk';
'-c', options.connections, this.executable = 'wrk';
'-j', const result = child_process.spawnSync(this.executable, ['-h']);
'-n', this.present = !(result.error && result.error.code === 'ENOENT');
`http://127.0.0.1:${options.port}${options.path}`
];
const child = child_process.spawn(this.autocannon_exe, args);
return child;
};
AutocannonBenchmarker.prototype.processResults = function(output) {
let result;
try {
result = JSON.parse(output);
} catch (err) {
// Do nothing, let next line handle this
} }
if (!result || !result.requests || !result.requests.average) {
return undefined;
} else {
return result.requests.average;
}
};
function WrkBenchmarker() { create(options) {
this.name = 'wrk'; const args = [
this.regexp = /Requests\/sec:[ \t]+([0-9.]+)/; '-d', options.duration,
const result = child_process.spawnSync('wrk', ['-h']); '-c', options.connections,
this.present = !(result.error && result.error.code === 'ENOENT'); '-t', 8,
`http://127.0.0.1:${options.port}${options.path}`
];
const child = child_process.spawn(this.executable, args);
return child;
}
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;
}
}
} }
WrkBenchmarker.prototype.create = function(options) { /**
const args = [ * Simple, single-threaded benchmarker for testing if the benchmark
'-d', options.duration, * works
'-c', options.connections, */
'-t', 8, class TestDoubleBenchmarker {
`http://127.0.0.1:${options.port}${options.path}` constructor() {
]; this.name = 'test-double';
const child = child_process.spawn('wrk', args); this.executable = path.resolve(__dirname, '_test-double-benchmarker.js');
return child; this.present = fs.existsSync(this.executable);
};
WrkBenchmarker.prototype.processResults = function(output) {
const match = output.match(this.regexp);
const result = match && +match[1];
if (!isFinite(result)) {
return undefined;
} else {
return result;
} }
};
const http_benchmarkers = [new WrkBenchmarker(), new AutocannonBenchmarker()]; 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}));
});