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

revert: benchmark: test refactoring #31722

Closed
wants to merge 4 commits into from
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: 6 additions & 16 deletions benchmark/_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@ const path = require('path');
// Create an object of all benchmark scripts
const benchmarks = {};
fs.readdirSync(__dirname)
.filter((name) => {
return name !== 'fixtures' &&
fs.statSync(path.resolve(__dirname, name)).isDirectory();
})
.filter((name) => 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 @@ -36,7 +34,7 @@ function CLI(usage, settings) {
if (arg === '--') {
// Only items can follow --
mode = 'item';
} else if (mode === 'both' && arg[0] === '-') {
} else if ('both' === mode && arg[0] === '-') {
// Optional arguments declaration

if (arg[1] === '-') {
Expand All @@ -63,8 +61,6 @@ 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 @@ -87,15 +83,9 @@ 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) {
console.error(`The "${category}" category does not exist.`);
process.exit(1);
}
if (benchmarks[category] === undefined)
continue;
for (const scripts of benchmarks[category]) {
if (this.shouldSkip(scripts)) continue;

Expand Down
23 changes: 10 additions & 13 deletions benchmark/_http-benchmarkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class AutocannonBenchmarker {
}
if (!result || !result.requests || !result.requests.average) {
return undefined;
} else {
return result.requests.average;
}
return result.requests.average;
}
}

Expand Down Expand Up @@ -76,8 +77,9 @@ class WrkBenchmarker {
const throughput = match && +match[1];
if (!isFinite(throughput)) {
return undefined;
} else {
return throughput;
}
return throughput;
}
}

Expand All @@ -87,20 +89,18 @@ class WrkBenchmarker {
*/
class TestDoubleBenchmarker {
constructor(type) {
// `type` is the type of benchmarker. Possible values are 'http' and
// 'http2'.
// `type` is the type ofbenchmarker. 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 = {
const env = Object.assign({
duration: options.duration,
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,14 +189,13 @@ http_benchmarkers.forEach((benchmarker) => {
});

exports.run = function(options, callback) {
options = {
options = Object.assign({
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 @@ -213,7 +212,6 @@ exports.run = function(options, callback) {
'is not installed'));
return;
}
process.env.duration = process.env.duration || options.duration || 5;

const benchmarker_start = process.hrtime();

Expand All @@ -222,8 +220,7 @@ exports.run = function(options, callback) {
child.stderr.pipe(process.stderr);

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

child.once('close', (code) => {
const elapsed = process.hrtime(benchmarker_start);
Expand Down
10 changes: 4 additions & 6 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;
const duration = process.env.duration || 0;
const url = process.env.test_url;

const start = process.hrtime();
Expand All @@ -18,15 +18,13 @@ function request(res, client) {
res.on('error', () => {});
res.on('end', () => {
throughput++;
const [sec, nanosec] = process.hrtime(start);
const ms = sec * 1000 + nanosec / 1e6;
if (ms < duration * 1000) {
const diff = process.hrtime(start);
if (duration > 0 && diff[0] < duration) {
run();
} else {
console.log(JSON.stringify({ throughput }));
if (client) {
client.destroy();
process.exit(0);
}
}
});
Expand All @@ -35,7 +33,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: 3 additions & 1 deletion benchmark/assert/deepequal-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ 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: 2 additions & 0 deletions benchmark/assert/deepequal-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ 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: 4 additions & 1 deletion 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,6 +27,9 @@ 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: 2 additions & 0 deletions benchmark/assert/deepequal-prims-and-objs-big-array-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ 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: 3 additions & 1 deletion benchmark/assert/deepequal-prims-and-objs-big-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ 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: 2 additions & 0 deletions benchmark/assert/deepequal-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ 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: 2 additions & 0 deletions benchmark/assert/deepequal-typedarrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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: 2 additions & 0 deletions benchmark/assert/throws.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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
6 changes: 2 additions & 4 deletions benchmark/async_hooks/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ const common = require('../common.js');

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

function main({ asyncHooks, connections, duration }) {
function main({ asyncHooks, connections }) {
if (asyncHooks !== 'none') {
let hooks = {
init() {},
Expand All @@ -34,7 +33,6 @@ function main({ asyncHooks, connections, duration }) {
bench.http({
connections,
path,
duration
}, () => {
server.close();
});
Expand Down
2 changes: 0 additions & 2 deletions benchmark/buffers/buffer-base64-encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ 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: 1 addition & 0 deletions benchmark/buffers/buffer-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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};
b.${type || 'fill(0)'};
}
`);
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];
const fn = methods[method || 'for'];

bench.start();
fn(buffer, n);
Expand Down
1 change: 1 addition & 0 deletions benchmark/buffers/buffer-read-float.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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
2 changes: 1 addition & 1 deletion benchmark/buffers/buffer-read-with-byteLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function main({ n, buf, type, byteLength }) {
const buff = buf === 'fast' ?
Buffer.alloc(8) :
require('buffer').SlowBuffer(8);
const fn = `read${type}`;
const fn = `read${type || 'IntBE'}`;

buff.writeDoubleLE(0, 0);
bench.start();
Expand Down
2 changes: 1 addition & 1 deletion benchmark/buffers/buffer-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function main({ n, buf, type }) {
const buff = buf === 'fast' ?
Buffer.alloc(8) :
require('buffer').SlowBuffer(8);
const fn = `read${type}`;
const fn = `read${type || 'UInt8'}`;

buff.writeDoubleLE(0, 0);
bench.start();
Expand Down
4 changes: 1 addition & 3 deletions benchmark/buffers/buffer-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ 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 Expand Up @@ -76,7 +74,7 @@ function genMethod(method) {

function main({ method, len, n, aligned = 'true' }) {
const buf = createBuffer(len, aligned === 'true');
const bufferSwap = genMethod(method);
const bufferSwap = genMethod(method || 'swap16');

bufferSwap(n, buf);
bench.start();
Expand Down
2 changes: 1 addition & 1 deletion benchmark/buffers/buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function main({ n, buf, type }) {
const buff = buf === 'fast' ?
Buffer.alloc(8) :
require('buffer').SlowBuffer(8);
const fn = `write${type}`;
const fn = `write${type || 'UInt8'}`;

if (!/\d/.test(fn))
benchSpecialInt(buff, fn, n);
Expand Down
Loading