Skip to content

Commit 9c12582

Browse files
BridgeARMylesBorins
authored andcommitted
benchmark: refactor
PR-URL: #18320 Reviewed-By: James M Snell <[email protected]>
1 parent f018670 commit 9c12582

17 files changed

+39
-82
lines changed

benchmark/async_hooks/gc-tracking.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@ function endAfterGC(n) {
2222
}
2323

2424
function main({ n, method }) {
25+
var i;
2526
switch (method) {
2627
case 'trackingEnabled':
2728
bench.start();
28-
for (let i = 0; i < n; i++) {
29+
for (i = 0; i < n; i++) {
2930
new AsyncResource('foobar');
3031
}
3132
endAfterGC(n);
3233
break;
3334
case 'trackingDisabled':
3435
bench.start();
35-
for (let i = 0; i < n; i++) {
36+
for (i = 0; i < n; i++) {
3637
new AsyncResource('foobar', { requireManualDestroy: true });
3738
}
3839
endAfterGC(n);
3940
break;
4041
default:
41-
throw new Error('Unsupported method');
42+
throw new Error(`Unsupported method "${method}"`);
4243
}
4344
}

benchmark/dgram/bind-params.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ const noop = () => {};
1515
function main({ n, port, address }) {
1616
port = port === 'true' ? 0 : undefined;
1717
address = address === 'true' ? '0.0.0.0' : undefined;
18+
var i;
1819

1920
if (port !== undefined && address !== undefined) {
2021
bench.start();
21-
for (let i = 0; i < n; i++) {
22+
for (i = 0; i < n; i++) {
2223
dgram.createSocket('udp4').bind(port, address)
2324
.on('error', noop)
2425
.unref();
2526
}
2627
bench.end(n);
2728
} else if (port !== undefined) {
2829
bench.start();
29-
for (let i = 0; i < n; i++) {
30+
for (i = 0; i < n; i++) {
3031
dgram.createSocket('udp4')
3132
.bind(port)
3233
.on('error', noop)
@@ -35,7 +36,7 @@ function main({ n, port, address }) {
3536
bench.end(n);
3637
} else if (port === undefined && address === undefined) {
3738
bench.start();
38-
for (let i = 0; i < n; i++) {
39+
for (i = 0; i < n; i++) {
3940
dgram.createSocket('udp4')
4041
.bind()
4142
.on('error', noop)

benchmark/domain/domain-fn-args.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ function main({ n, args }) {
2828
bench.end(n);
2929
}
3030

31-
function fn(a, b, c) {
32-
if (!a)
33-
a = 1;
34-
35-
if (!b)
36-
b = 2;
37-
38-
if (!c)
39-
c = 3;
40-
31+
function fn(a = 1, b = 2, c = 3) {
4132
return a + b + c;
4233
}

benchmark/fs/bench-realpath.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ function main({ n, pathType }) {
1616
bench.start();
1717
if (pathType === 'relative')
1818
relativePath(n);
19-
else if (pathType === 'resolved')
20-
resolvedPath(n);
2119
else
22-
throw new Error(`unknown "pathType": ${pathType}`);
20+
resolvedPath(n);
2321
}
2422

2523
function relativePath(n) {

benchmark/fs/bench-realpathSync.js

+3-17
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,10 @@ const bench = common.createBenchmark(main, {
1515

1616

1717
function main({ n, pathType }) {
18+
const path = pathType === 'relative' ? relative_path : resolved_path;
1819
bench.start();
19-
if (pathType === 'relative')
20-
relativePath(n);
21-
else if (pathType === 'resolved')
22-
resolvedPath(n);
23-
else
24-
throw new Error(`unknown "pathType": ${pathType}`);
25-
bench.end(n);
26-
}
27-
28-
function relativePath(n) {
29-
for (var i = 0; i < n; i++) {
30-
fs.realpathSync(relative_path);
31-
}
32-
}
33-
34-
function resolvedPath(n) {
3520
for (var i = 0; i < n; i++) {
36-
fs.realpathSync(resolved_path);
21+
fs.realpathSync(path);
3722
}
23+
bench.end(n);
3824
}

benchmark/fs/write-stream-throughput.js

-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function main({ dur, encodingType, size }) {
3636
try { fs.unlinkSync(filename); } catch (e) {}
3737

3838
var started = false;
39-
var ending = false;
4039
var ended = false;
4140

4241
var f = fs.createWriteStream(filename);
@@ -52,15 +51,9 @@ function main({ dur, encodingType, size }) {
5251

5352

5453
function write() {
55-
// don't try to write after we end, even if a 'drain' event comes.
56-
// v0.8 streams are so sloppy!
57-
if (ending)
58-
return;
59-
6054
if (!started) {
6155
started = true;
6256
setTimeout(function() {
63-
ending = true;
6457
f.end();
6558
}, dur * 1000);
6659
bench.start();

benchmark/misc/freelist.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function main({ n }) {
1212
const FreeList = require('internal/freelist');
1313
const poolSize = 1000;
1414
const list = new FreeList('test', poolSize, Object);
15-
var i;
1615
var j;
1716
const used = [];
1817

@@ -23,7 +22,7 @@ function main({ n }) {
2322

2423
bench.start();
2524

26-
for (i = 0; i < n; i++) {
25+
for (var i = 0; i < n; i++) {
2726
// Return all the items to the pool
2827
for (j = 0; j < poolSize; j++) {
2928
list.free(used[j]);

benchmark/misc/function_call/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ const bench = common.createBenchmark(main, {
3232
});
3333

3434
function main({ millions, type }) {
35-
const n = millions * 1e6;
36-
3735
const fn = type === 'cxx' ? cxx : js;
3836
bench.start();
39-
for (var i = 0; i < n; i++) {
37+
for (var i = 0; i < millions * 1e6; i++) {
4038
fn();
4139
}
4240
bench.end(millions);

benchmark/misc/object-property-bench.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ function main({ millions, method }) {
7878
runSymbol(n);
7979
break;
8080
default:
81-
throw new Error('Unexpected method');
81+
throw new Error(`Unexpected method "${method}"`);
8282
}
8383
}

benchmark/misc/punycode.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ function runPunycode(n, val) {
5555
}
5656

5757
function runICU(n, val) {
58-
var i = 0;
5958
bench.start();
60-
for (; i < n; i++)
59+
for (var i = 0; i < n; i++)
6160
usingICU(val);
6261
bench.end(n);
6362
}
@@ -76,6 +75,6 @@ function main({ n, val, method }) {
7675
}
7776
// fallthrough
7877
default:
79-
throw new Error('Unexpected method');
78+
throw new Error(`Unexpected method "${method}"`);
8079
}
8180
}

benchmark/module/module-loader.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ const bench = common.createBenchmark(main, {
1313
});
1414

1515
function main({ thousands, fullPath, useCache }) {
16-
const n = thousands * 1e3;
17-
1816
tmpdir.refresh();
1917
try { fs.mkdirSync(benchmarkDirectory); } catch (e) {}
2018

21-
for (var i = 0; i <= n; i++) {
19+
for (var i = 0; i <= thousands * 1e3; i++) {
2220
fs.mkdirSync(`${benchmarkDirectory}${i}`);
2321
fs.writeFileSync(
2422
`${benchmarkDirectory}${i}/package.json`,
@@ -31,37 +29,37 @@ function main({ thousands, fullPath, useCache }) {
3129
}
3230

3331
if (fullPath === 'true')
34-
measureFull(n, useCache === 'true');
32+
measureFull(thousands, useCache === 'true');
3533
else
36-
measureDir(n, useCache === 'true');
34+
measureDir(thousands, useCache === 'true');
3735

3836
tmpdir.refresh();
3937
}
4038

41-
function measureFull(n, useCache) {
39+
function measureFull(thousands, useCache) {
4240
var i;
4341
if (useCache) {
44-
for (i = 0; i <= n; i++) {
42+
for (i = 0; i <= thousands * 1e3; i++) {
4543
require(`${benchmarkDirectory}${i}/index.js`);
4644
}
4745
}
4846
bench.start();
49-
for (i = 0; i <= n; i++) {
47+
for (i = 0; i <= thousands * 1e3; i++) {
5048
require(`${benchmarkDirectory}${i}/index.js`);
5149
}
52-
bench.end(n / 1e3);
50+
bench.end(thousands);
5351
}
5452

55-
function measureDir(n, useCache) {
53+
function measureDir(thousands, useCache) {
5654
var i;
5755
if (useCache) {
58-
for (i = 0; i <= n; i++) {
56+
for (i = 0; i <= thousands * 1e3; i++) {
5957
require(`${benchmarkDirectory}${i}`);
6058
}
6159
}
6260
bench.start();
63-
for (i = 0; i <= n; i++) {
61+
for (i = 0; i <= thousands * 1e3; i++) {
6462
require(`${benchmarkDirectory}${i}`);
6563
}
66-
bench.end(n / 1e3);
64+
bench.end(thousands);
6765
}

benchmark/tls/convertprotocols.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ const bench = common.createBenchmark(main, {
88
});
99

1010
function main({ n }) {
11-
var i = 0;
11+
const input = ['ABC', 'XYZ123', 'FOO'];
1212
var m = {};
1313
// First call dominates results
1414
if (n > 1) {
15-
tls.convertNPNProtocols(['ABC', 'XYZ123', 'FOO'], m);
15+
tls.convertNPNProtocols(input, m);
1616
m = {};
1717
}
1818
bench.start();
19-
for (; i < n; i++) tls.convertNPNProtocols(['ABC', 'XYZ123', 'FOO'], m);
19+
for (var i = 0; i < n; i++)
20+
tls.convertNPNProtocols(input, m);
2021
bench.end(n);
2122
}

benchmark/util/format.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ const bench = common.createBenchmark(main, {
2222

2323
function main({ n, type }) {
2424
// For testing, if supplied with an empty type, default to string.
25-
type = type || 'string';
26-
27-
const [first, second] = inputs[type];
25+
const [first, second] = inputs[type || 'string'];
2826

2927
bench.start();
3028
for (var i = 0; i < n; i++) {

benchmark/util/inspect-array.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ function main({ n, len, type }) {
1818
var arr = Array(len);
1919
var i, opts;
2020

21-
// For testing, if supplied with an empty type, default to denseArray.
22-
type = type || 'denseArray';
23-
2421
switch (type) {
2522
case 'denseArray_showHidden':
2623
opts = { showHidden: true };
2724
arr = arr.fill('denseArray');
2825
break;
26+
// For testing, if supplied with an empty type, default to denseArray.
27+
case '':
2928
case 'denseArray':
3029
arr = arr.fill('denseArray');
3130
break;

benchmark/v8/get-stats.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ const bench = common.createBenchmark(main, {
1212
});
1313

1414
function main({ method, n }) {
15-
var i = 0;
1615
bench.start();
17-
for (; i < n; i++)
16+
for (var i = 0; i < n; i++)
1817
v8[method]();
1918
bench.end(n);
2019
}

benchmark/vm/run-in-context.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ function main({ n, breakOnSigint, withSigintListener }) {
1717
if (withSigintListener)
1818
process.on('SIGINT', () => {});
1919

20-
var i = 0;
21-
2220
const contextifiedSandbox = vm.createContext();
2321

2422
bench.start();
25-
for (; i < n; i++)
23+
for (var i = 0; i < n; i++)
2624
vm.runInContext('0', contextifiedSandbox, options);
2725
bench.end(n);
2826
}

benchmark/vm/run-in-this-context.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ function main({ n, breakOnSigint, withSigintListener }) {
1717
if (withSigintListener)
1818
process.on('SIGINT', () => {});
1919

20-
var i = 0;
21-
2220
bench.start();
23-
for (; i < n; i++)
21+
for (var i = 0; i < n; i++)
2422
vm.runInThisContext('0', options);
2523
bench.end(n);
2624
}

0 commit comments

Comments
 (0)