Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: reland test refactoring #31755

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions benchmark/_cli.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
4 changes: 1 addition & 3 deletions benchmark/assert/deepequal-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ const bench = common.createBenchmark(main, {
n: [2e4],
len: [1e2, 1e3],
strict: [0, 1],
method: [ 'deepEqual', 'notDeepEqual' ],
method: ['deepEqual', 'notDeepEqual'],
});

function main({ len, n, method, strict }) {
if (!method)
method = 'deepEqual';
const data = Buffer.allocUnsafe(len + 1);
const actual = Buffer.alloc(len);
const expected = Buffer.alloc(len);
Expand Down
2 changes: 0 additions & 2 deletions benchmark/assert/deepequal-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ function main({ n, len, method, strict }) {
const array = Array(len).fill(1);

switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'deepEqual_primitiveOnly': {
const values = array.map((_, i) => [`str_${i}`, 123]);
benchmark(strict ? deepStrictEqual : deepEqual, n, values);
Expand Down
5 changes: 1 addition & 4 deletions benchmark/assert/deepequal-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const bench = common.createBenchmark(main, {
n: [5e3],
size: [1e2, 1e3, 5e4],
strict: [0, 1],
method: [ 'deepEqual', 'notDeepEqual' ],
method: ['deepEqual', 'notDeepEqual'],
});

function createObj(source, add = '') {
Expand All @@ -27,9 +27,6 @@ function main({ size, n, method, strict }) {
// TODO: Fix this "hack". `n` should not be manipulated.
n = Math.min(Math.ceil(n / size), 20);

if (!method)
method = 'deepEqual';

const source = Array.apply(null, Array(size));
const actual = createObj(source);
const expected = createObj(source);
Expand Down
2 changes: 0 additions & 2 deletions benchmark/assert/deepequal-prims-and-objs-big-array-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ function main({ n, len, primitive, method, strict }) {
const expectedWrongSet = new Set(expectedWrong);

switch (method) {
// Empty string falls through to next line as default, mostly for tests.
case '':
case 'deepEqual_Array':
run(strict ? deepStrictEqual : deepEqual, n, actual, expected);
break;
Expand Down
4 changes: 1 addition & 3 deletions benchmark/assert/deepequal-prims-and-objs-big-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ const bench = common.createBenchmark(main, {
primitive: Object.keys(primValues),
n: [2e4],
strict: [0, 1],
method: [ 'deepEqual', 'notDeepEqual' ],
method: ['deepEqual', 'notDeepEqual'],
});

function main({ n, primitive, method, strict }) {
if (!method)
method = 'deepEqual';
const prim = primValues[primitive];
const actual = prim;
const expected = prim;
Expand Down
2 changes: 0 additions & 2 deletions benchmark/assert/deepequal-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ function main({ n, len, method, strict }) {
const array = Array(len).fill(1);

switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'deepEqual_primitiveOnly': {
const values = array.map((_, i) => `str_${i}`);
benchmark(strict ? deepStrictEqual : deepEqual, n, values);
Expand Down
2 changes: 0 additions & 2 deletions benchmark/assert/deepequal-typedarrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const bench = common.createBenchmark(main, {
});

function main({ type, n, len, method, strict }) {
if (!method)
method = 'deepEqual';
const clazz = global[type];
const actual = new clazz(len);
const expected = new clazz(len);
Expand Down
2 changes: 0 additions & 2 deletions benchmark/assert/throws.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ function main({ n, method }) {
const message = 'failure';

switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'doesNotThrow':
bench.start();
for (let i = 0; i < n; ++i) {
Expand Down
13 changes: 6 additions & 7 deletions benchmark/async_hooks/async-resource-vs-destroy.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions benchmark/buffers/buffer-bytelength.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const chars = [

function main({ n, len, encoding }) {
let strings = [];
let results = [ len * 16 ];
let results = [len * 16];
if (encoding === 'buffer') {
strings = [ Buffer.alloc(len * 16, 'a') ];
strings = [Buffer.alloc(len * 16, 'a')];
} else {
for (const string of chars) {
// Strings must be built differently, depending on encoding
Expand Down
1 change: 0 additions & 1 deletion benchmark/buffers/buffer-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const bench = common.createBenchmark(main, {
function main({ len, n, type }) {
let fn, i;
switch (type) {
case '':
case 'fast-alloc':
fn = Buffer.alloc;
break;
Expand Down
2 changes: 1 addition & 1 deletion benchmark/buffers/buffer-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function main({ n, type, size }) {
const buffer = Buffer.allocUnsafe(size);
const testFunction = new Function('b', `
for (var i = 0; i < ${n}; i++) {
b.${type || 'fill(0)'};
b.${type};
}
`);
bench.start();
Expand Down
2 changes: 1 addition & 1 deletion benchmark/buffers/buffer-iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function main({ size, type, method, n }) {
Buffer.alloc(size) :
SlowBuffer(size).fill(0);

const fn = methods[method || 'for'];
const fn = methods[method];

bench.start();
fn(buffer, n);
Expand Down
1 change: 0 additions & 1 deletion benchmark/buffers/buffer-read-float.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const bench = common.createBenchmark(main, {
});

function main({ n, type, endian, value }) {
type = type || 'Double';
const buff = Buffer.alloc(8);
const fn = `read${type}${endian}`;
const values = {
Expand Down
Loading