Skip to content

Commit

Permalink
benchmark: add test and all options and improve errors"
Browse files Browse the repository at this point in the history
This reverts commit 4671d55 and
contains a fix to the issue raised for the revert.

PR-URL: #31755
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR authored and MylesBorins committed Mar 9, 2020
1 parent e11f38c commit 5d92cec
Show file tree
Hide file tree
Showing 38 changed files with 305 additions and 200 deletions.
22 changes: 16 additions & 6 deletions benchmark/_cli.js
Expand Up @@ -6,22 +6,24 @@ const path = require('path');
// Create an object of all benchmark scripts
const benchmarks = {};
fs.readdirSync(__dirname)
.filter((name) => fs.statSync(path.resolve(__dirname, name)).isDirectory())
.filter((name) => {
return name !== 'fixtures' &&
fs.statSync(path.resolve(__dirname, name)).isDirectory();
})
.forEach((category) => {
benchmarks[category] = fs.readdirSync(path.resolve(__dirname, category))
.filter((filename) => filename[0] !== '.' && filename[0] !== '_');
});

function CLI(usage, settings) {
if (!(this instanceof CLI)) return new CLI(usage, settings);

if (process.argv.length < 3) {
this.abort(usage); // Abort will exit the process
}

this.usage = usage;
this.optional = {};
this.items = [];
this.test = false;

for (const argName of settings.arrayArgs) {
this.optional[argName] = [];
Expand All @@ -34,7 +36,7 @@ function CLI(usage, settings) {
if (arg === '--') {
// Only items can follow --
mode = 'item';
} else if ('both' === mode && arg[0] === '-') {
} else if (mode === 'both' && arg[0] === '-') {
// Optional arguments declaration

if (arg[1] === '-') {
Expand All @@ -61,6 +63,8 @@ function CLI(usage, settings) {

// The next value can be either an option or an item
mode = 'both';
} else if (arg === 'test') {
this.test = true;
} else if (['both', 'item'].includes(mode)) {
// item arguments
this.items.push(arg);
Expand All @@ -83,9 +87,15 @@ CLI.prototype.abort = function(msg) {
CLI.prototype.benchmarks = function() {
const paths = [];

if (this.items.includes('all')) {
this.items = Object.keys(benchmarks);
}

for (const category of this.items) {
if (benchmarks[category] === undefined)
continue;
if (benchmarks[category] === undefined) {
console.error(`The "${category}" category does not exist.`);
process.exit(1);
}
for (const scripts of benchmarks[category]) {
if (this.shouldSkip(scripts)) continue;

Expand Down
32 changes: 19 additions & 13 deletions benchmark/_http-benchmarkers.js
Expand Up @@ -43,9 +43,8 @@ class AutocannonBenchmarker {
}
if (!result || !result.requests || !result.requests.average) {
return undefined;
} else {
return result.requests.average;
}
return result.requests.average;
}
}

Expand All @@ -58,10 +57,13 @@ class WrkBenchmarker {
}

create(options) {
const duration = typeof options.duration === 'number' ?
Math.max(options.duration, 1) :
options.duration;
const args = [
'-d', options.duration,
'-d', duration,
'-c', options.connections,
'-t', 8,
'-t', Math.min(options.connections, require('os').cpus().length || 8),
`http://127.0.0.1:${options.port}${options.path}`,
];
for (const field in options.headers) {
Expand All @@ -77,9 +79,8 @@ class WrkBenchmarker {
const throughput = match && +match[1];
if (!isFinite(throughput)) {
return undefined;
} else {
return throughput;
}
return throughput;
}
}

Expand All @@ -89,18 +90,21 @@ class WrkBenchmarker {
*/
class TestDoubleBenchmarker {
constructor(type) {
// `type` is the type ofbenchmarker. Possible values are 'http' and 'http2'.
// `type` is the type of benchmarker. Possible values are 'http' and
// 'http2'.
this.name = `test-double-${type}`;
this.executable = path.resolve(__dirname, '_test-double-benchmarker.js');
this.present = fs.existsSync(this.executable);
this.type = type;
}

create(options) {
const env = Object.assign({
duration: options.duration,
process.env.duration = process.env.duration || options.duration || 5;

const env = {
test_url: `http://127.0.0.1:${options.port}${options.path}`,
}, process.env);
...process.env
};

const child = child_process.fork(this.executable,
[this.type],
Expand Down Expand Up @@ -189,13 +193,14 @@ http_benchmarkers.forEach((benchmarker) => {
});

exports.run = function(options, callback) {
options = Object.assign({
options = {
port: exports.PORT,
path: '/',
connections: 100,
duration: 5,
benchmarker: exports.default_http_benchmarker,
}, options);
...options
};
if (!options.benchmarker) {
callback(new Error('Could not locate required http benchmarker. See ' +
`${requirementsURL} for further instructions.`));
Expand All @@ -220,7 +225,8 @@ exports.run = function(options, callback) {
child.stderr.pipe(process.stderr);

let stdout = '';
child.stdout.on('data', (chunk) => stdout += chunk.toString());
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => stdout += chunk);

child.once('close', (code) => {
const elapsed = process.hrtime(benchmarker_start);
Expand Down
10 changes: 6 additions & 4 deletions benchmark/_test-double-benchmarker.js
Expand Up @@ -7,7 +7,7 @@ if (!['http', 'http2'].includes(myModule)) {

const http = require(myModule);

const duration = process.env.duration || 0;
const duration = +process.env.duration;
const url = process.env.test_url;

const start = process.hrtime();
Expand All @@ -18,13 +18,15 @@ function request(res, client) {
res.on('error', () => {});
res.on('end', () => {
throughput++;
const diff = process.hrtime(start);
if (duration > 0 && diff[0] < duration) {
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);
}
}
});
Expand All @@ -33,7 +35,7 @@ function request(res, client) {
function run() {
if (http.get) { // HTTP
http.get(url, request);
} else { // HTTP/2
} else { // HTTP/2
const client = http.connect(url);
client.on('error', (e) => { throw e; });
request(client.request(), client);
Expand Down
13 changes: 6 additions & 7 deletions benchmark/async_hooks/async-resource-vs-destroy.js
Expand Up @@ -13,14 +13,12 @@ const {
} = require('async_hooks');
const { createServer } = require('http');

// Configuration for the http server
// there is no need for parameters in this test
const connections = 500;
const path = '/';

const bench = common.createBenchmark(main, {
type: ['async-resource', 'destroy', 'async-local-storage'],
asyncMethod: ['callbacks', 'async'],
path: '/',
connections: 500,
duration: 5,
n: [1e6]
});

Expand Down Expand Up @@ -165,7 +163,7 @@ const asyncMethods = {
'async': getServeAwait
};

function main({ type, asyncMethod }) {
function main({ type, asyncMethod, connections, duration, path }) {
const { server, close } = types[type](asyncMethods[asyncMethod]);

server
Expand All @@ -174,7 +172,8 @@ function main({ type, asyncMethod }) {

bench.http({
path,
connections
connections,
duration
}, () => {
close();
});
Expand Down
6 changes: 4 additions & 2 deletions benchmark/async_hooks/http-server.js
Expand Up @@ -3,10 +3,11 @@ const common = require('../common.js');

const bench = common.createBenchmark(main, {
asyncHooks: ['init', 'before', 'after', 'all', 'disabled', 'none'],
connections: [50, 500]
connections: [50, 500],
duration: 5
});

function main({ asyncHooks, connections }) {
function main({ asyncHooks, connections, duration }) {
if (asyncHooks !== 'none') {
let hooks = {
init() {},
Expand All @@ -33,6 +34,7 @@ function main({ asyncHooks, connections }) {
bench.http({
connections,
path,
duration
}, () => {
server.close();
});
Expand Down
2 changes: 2 additions & 0 deletions benchmark/buffers/buffer-base64-encode.js
Expand Up @@ -25,6 +25,8 @@ const common = require('../common.js');
const bench = common.createBenchmark(main, {
len: [64 * 1024 * 1024],
n: [32]
}, {
test: { len: 256 }
});

function main({ n, len }) {
Expand Down
2 changes: 2 additions & 0 deletions benchmark/buffers/buffer-swap.js
Expand Up @@ -7,6 +7,8 @@ const bench = common.createBenchmark(main, {
method: ['swap16', 'swap32', 'swap64'/* , 'htons', 'htonl', 'htonll' */],
len: [64, 256, 768, 1024, 2056, 8192],
n: [1e6]
}, {
test: { len: 16 }
});

// The htons and htonl methods below are used to benchmark the
Expand Down

0 comments on commit 5d92cec

Please sign in to comment.