Skip to content

Commit

Permalink
benchmark: refactor
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18320
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and ryzokuken committed Jun 6, 2018
1 parent b186741 commit ab4365e
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 95 deletions.
12 changes: 9 additions & 3 deletions benchmark/async_hooks/gc-tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,31 @@ function endAfterGC(n) {
});
}

<<<<<<< HEAD
function main(conf) {
const n = +conf.n;

switch (conf.method) {
=======
function main({ n, method }) {
var i;
switch (method) {
>>>>>>> 1b6cb94761... benchmark: refactor
case 'trackingEnabled':
bench.start();
for (let i = 0; i < n; i++) {
for (i = 0; i < n; i++) {
new AsyncResource('foobar');
}
endAfterGC(n);
break;
case 'trackingDisabled':
bench.start();
for (let i = 0; i < n; i++) {
for (i = 0; i < n; i++) {
new AsyncResource('foobar', { requireManualDestroy: true });
}
endAfterGC(n);
break;
default:
throw new Error('Unsupported method');
throw new Error(`Unsupported method "${method}"`);
}
}
14 changes: 7 additions & 7 deletions benchmark/dgram/bind-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ const configs = {
const bench = common.createBenchmark(main, configs);
const noop = () => {};

function main(conf) {
const n = +conf.n;
const port = conf.port === 'true' ? 0 : undefined;
const address = conf.address === 'true' ? '0.0.0.0' : undefined;
function main({ n, port, address }) {
port = port === 'true' ? 0 : undefined;
address = address === 'true' ? '0.0.0.0' : undefined;
var i;

if (port !== undefined && address !== undefined) {
bench.start();
for (let i = 0; i < n; i++) {
for (i = 0; i < n; i++) {
dgram.createSocket('udp4').bind(port, address)
.on('error', noop)
.unref();
}
bench.end(n);
} else if (port !== undefined) {
bench.start();
for (let i = 0; i < n; i++) {
for (i = 0; i < n; i++) {
dgram.createSocket('udp4')
.bind(port)
.on('error', noop)
Expand All @@ -36,7 +36,7 @@ function main(conf) {
bench.end(n);
} else if (port === undefined && address === undefined) {
bench.start();
for (let i = 0; i < n; i++) {
for (i = 0; i < n; i++) {
dgram.createSocket('udp4')
.bind()
.on('error', noop)
Expand Down
11 changes: 1 addition & 10 deletions benchmark/domain/domain-fn-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ function main(conf) {
bench.end(n);
}

function fn(a, b, c) {
if (!a)
a = 1;

if (!b)
b = 2;

if (!c)
c = 3;

function fn(a = 1, b = 2, c = 3) {
return a + b + c;
}
4 changes: 1 addition & 3 deletions benchmark/fs/bench-realpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ function main(conf) {
bench.start();
if (pathType === 'relative')
relativePath(n);
else if (pathType === 'resolved')
resolvedPath(n);
else
throw new Error(`unknown "pathType": ${pathType}`);
resolvedPath(n);
}

function relativePath(n) {
Expand Down
25 changes: 4 additions & 21 deletions benchmark/fs/bench-realpathSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,11 @@ const bench = common.createBenchmark(main, {
});


function main(conf) {
const n = conf.n >>> 0;
const pathType = conf.pathType;

function main({ n, pathType }) {
const path = pathType === 'relative' ? relative_path : resolved_path;
bench.start();
if (pathType === 'relative')
relativePath(n);
else if (pathType === 'resolved')
resolvedPath(n);
else
throw new Error(`unknown "pathType": ${pathType}`);
bench.end(n);
}

function relativePath(n) {
for (var i = 0; i < n; i++) {
fs.realpathSync(relative_path);
}
}

function resolvedPath(n) {
for (var i = 0; i < n; i++) {
fs.realpathSync(resolved_path);
fs.realpathSync(path);
}
bench.end(n);
}
7 changes: 0 additions & 7 deletions benchmark/fs/write-stream-throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ function main(conf) {
try { fs.unlinkSync(filename); } catch (e) {}

var started = false;
var ending = false;
var ended = false;

var f = fs.createWriteStream(filename);
Expand All @@ -55,15 +54,9 @@ function main(conf) {


function write() {
// don't try to write after we end, even if a 'drain' event comes.
// v0.8 streams are so sloppy!
if (ending)
return;

if (!started) {
started = true;
setTimeout(function() {
ending = true;
f.end();
}, dur * 1000);
bench.start();
Expand Down
3 changes: 1 addition & 2 deletions benchmark/misc/freelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function main(conf) {
const n = conf.n;
const poolSize = 1000;
const list = new FreeList('test', poolSize, Object);
var i;
var j;
const used = [];

Expand All @@ -24,7 +23,7 @@ function main(conf) {

bench.start();

for (i = 0; i < n; i++) {
for (var i = 0; i < n; i++) {
// Return all the items to the pool
for (j = 0; j < poolSize; j++) {
list.free(used[j]);
Expand Down
8 changes: 3 additions & 5 deletions benchmark/misc/function_call/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ const bench = common.createBenchmark(main, {
millions: [1, 10, 50]
});

function main(conf) {
const n = +conf.millions * 1e6;

const fn = conf.type === 'cxx' ? cxx : js;
function main({ millions, type }) {
const fn = type === 'cxx' ? cxx : js;
bench.start();
for (var i = 0; i < n; i++) {
for (var i = 0; i < millions * 1e6; i++) {
fn();
}
bench.end(+conf.millions);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/misc/object-property-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ function main(conf) {
runSymbol(n);
break;
default:
throw new Error('Unexpected method');
throw new Error(`Unexpected method "${method}"`);
}
}
5 changes: 2 additions & 3 deletions benchmark/misc/punycode.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ function runPunycode(n, val) {
}

function runICU(n, val) {
var i = 0;
bench.start();
for (; i < n; i++)
for (var i = 0; i < n; i++)
usingICU(val);
bench.end(n);
}
Expand All @@ -78,6 +77,6 @@ function main(conf) {
}
// fallthrough
default:
throw new Error('Unexpected method');
throw new Error(`Unexpected method "${method}"`);
}
}
28 changes: 13 additions & 15 deletions benchmark/module/module-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ const bench = common.createBenchmark(main, {
useCache: ['true', 'false']
});

function main(conf) {
const n = +conf.thousands * 1e3;

function main({ thousands, fullPath, useCache }) {
tmpdir.refresh();
try { fs.mkdirSync(benchmarkDirectory); } catch (e) {}

for (var i = 0; i <= n; i++) {
for (var i = 0; i <= thousands * 1e3; i++) {
fs.mkdirSync(`${benchmarkDirectory}${i}`);
fs.writeFileSync(
`${benchmarkDirectory}${i}/package.json`,
Expand All @@ -30,38 +28,38 @@ function main(conf) {
);
}

if (conf.fullPath === 'true')
measureFull(n, conf.useCache === 'true');
if (fullPath === 'true')
measureFull(thousands, useCache === 'true');
else
measureDir(n, conf.useCache === 'true');
measureDir(thousands, useCache === 'true');

tmpdir.refresh();
}

function measureFull(n, useCache) {
function measureFull(thousands, useCache) {
var i;
if (useCache) {
for (i = 0; i <= n; i++) {
for (i = 0; i <= thousands * 1e3; i++) {
require(`${benchmarkDirectory}${i}/index.js`);
}
}
bench.start();
for (i = 0; i <= n; i++) {
for (i = 0; i <= thousands * 1e3; i++) {
require(`${benchmarkDirectory}${i}/index.js`);
}
bench.end(n / 1e3);
bench.end(thousands);
}

function measureDir(n, useCache) {
function measureDir(thousands, useCache) {
var i;
if (useCache) {
for (i = 0; i <= n; i++) {
for (i = 0; i <= thousands * 1e3; i++) {
require(`${benchmarkDirectory}${i}`);
}
}
bench.start();
for (i = 0; i <= n; i++) {
for (i = 0; i <= thousands * 1e3; i++) {
require(`${benchmarkDirectory}${i}`);
}
bench.end(n / 1e3);
bench.end(thousands);
}
11 changes: 5 additions & 6 deletions benchmark/tls/convertprotocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ const bench = common.createBenchmark(main, {
n: [1, 50000]
});

function main(conf) {
const n = +conf.n;

var i = 0;
function main({ n }) {
const input = ['ABC', 'XYZ123', 'FOO'];
var m = {};
// First call dominates results
if (n > 1) {
tls.convertNPNProtocols(['ABC', 'XYZ123', 'FOO'], m);
tls.convertNPNProtocols(input, m);
m = {};
}
bench.start();
for (; i < n; i++) tls.convertNPNProtocols(['ABC', 'XYZ123', 'FOO'], m);
for (var i = 0; i < n; i++)
tls.convertNPNProtocols(input, m);
bench.end(n);
}
3 changes: 2 additions & 1 deletion benchmark/util/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const bench = common.createBenchmark(main, {
});

function main({ n, type }) {
const [first, second] = inputs[type];
// For testing, if supplied with an empty type, default to string.
const [first, second] = inputs[type || 'string'];

bench.start();
for (var i = 0; i < n; i++) {
Expand Down
2 changes: 2 additions & 0 deletions benchmark/util/inspect-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function main({ n, len, type }) {
opts = { showHidden: true };
arr = arr.fill('denseArray');
break;
// For testing, if supplied with an empty type, default to denseArray.
case '':
case 'denseArray':
arr = arr.fill('denseArray');
break;
Expand Down
7 changes: 2 additions & 5 deletions benchmark/v8/get-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ const bench = common.createBenchmark(main, {
n: [1e6]
});

function main(conf) {
const n = +conf.n;
const method = conf.method;
var i = 0;
function main({ method, n }) {
bench.start();
for (; i < n; i++)
for (var i = 0; i < n; i++)
v8[method]();
bench.end(n);
}
4 changes: 1 addition & 3 deletions benchmark/vm/run-in-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ function main(conf) {
if (withSigintListener)
process.on('SIGINT', () => {});

var i = 0;

const contextifiedSandbox = vm.createContext();

bench.start();
for (; i < n; i++)
for (var i = 0; i < n; i++)
vm.runInContext('0', contextifiedSandbox, options);
bench.end(n);
}
4 changes: 1 addition & 3 deletions benchmark/vm/run-in-this-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ function main(conf) {
if (withSigintListener)
process.on('SIGINT', () => {});

var i = 0;

bench.start();
for (; i < n; i++)
for (var i = 0; i < n; i++)
vm.runInThisContext('0', options);
bench.end(n);
}

0 comments on commit ab4365e

Please sign in to comment.