Skip to content

Commit 566a151

Browse files
committed
benchmark: reformat code for clarity
Some of the benchmark code can be a little dense. Not *very* hard to read but perhaps harder than it needs to be. These changes (many of them whitespace-only) hopefully improve readability. There are also a few cases of `assert.equal()` that are changed to `assert.strictEqual()`. PR-URL: #9790 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 31d1a3f commit 566a151

17 files changed

+204
-79
lines changed

benchmark/_http-benchmarkers.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ function AutocannonBenchmarker() {
1515
}
1616

1717
AutocannonBenchmarker.prototype.create = function(options) {
18-
const args = ['-d', options.duration, '-c', options.connections, '-j', '-n',
19-
`http://127.0.0.1:${options.port}${options.path}` ];
18+
const args = [
19+
'-d', options.duration,
20+
'-c', options.connections,
21+
'-j',
22+
'-n',
23+
`http://127.0.0.1:${options.port}${options.path}`
24+
];
2025
const child = child_process.spawn(this.autocannon_exe, args);
2126
return child;
2227
};
@@ -43,8 +48,12 @@ function WrkBenchmarker() {
4348
}
4449

4550
WrkBenchmarker.prototype.create = function(options) {
46-
const args = ['-d', options.duration, '-c', options.connections, '-t', 8,
47-
`http://127.0.0.1:${options.port}${options.path}` ];
51+
const args = [
52+
'-d', options.duration,
53+
'-c', options.connections,
54+
'-t', 8,
55+
`http://127.0.0.1:${options.port}${options.path}`
56+
];
4857
const child = child_process.spawn('wrk', args);
4958
return child;
5059
};
@@ -59,8 +68,7 @@ WrkBenchmarker.prototype.processResults = function(output) {
5968
}
6069
};
6170

62-
const http_benchmarkers = [ new WrkBenchmarker(),
63-
new AutocannonBenchmarker() ];
71+
const http_benchmarkers = [new WrkBenchmarker(), new AutocannonBenchmarker()];
6472

6573
const benchmarkers = {};
6674

benchmark/arrays/var-int.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
'use strict';
22
var common = require('../common.js');
3+
4+
var types = [
5+
'Array',
6+
'Buffer',
7+
'Int8Array',
8+
'Uint8Array',
9+
'Int16Array',
10+
'Uint16Array',
11+
'Int32Array',
12+
'Uint32Array',
13+
'Float32Array',
14+
'Float64Array'
15+
];
16+
317
var bench = common.createBenchmark(main, {
4-
type: ['Array', 'Buffer', 'Int8Array', 'Uint8Array', 'Int16Array',
5-
'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array',
6-
'Float64Array'],
18+
type: types,
719
n: [25]
820
});
921

benchmark/arrays/zero-float.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
'use strict';
22
var common = require('../common.js');
3+
4+
var types = [
5+
'Array',
6+
'Buffer',
7+
'Int8Array',
8+
'Uint8Array',
9+
'Int16Array',
10+
'Uint16Array',
11+
'Int32Array',
12+
'Uint32Array',
13+
'Float32Array',
14+
'Float64Array'
15+
];
16+
317
var bench = common.createBenchmark(main, {
4-
type: ['Array', 'Buffer', 'Int8Array', 'Uint8Array', 'Int16Array',
5-
'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array',
6-
'Float64Array'],
18+
type: types,
719
n: [25]
820
});
921

benchmark/arrays/zero-int.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
'use strict';
22
var common = require('../common.js');
3+
4+
var types = [
5+
'Array',
6+
'Buffer',
7+
'Int8Array',
8+
'Uint8Array',
9+
'Int16Array',
10+
'Uint16Array',
11+
'Int32Array',
12+
'Uint32Array',
13+
'Float32Array',
14+
'Float64Array'
15+
];
16+
317
var bench = common.createBenchmark(main, {
4-
type: ['Array', 'Buffer', 'Int8Array', 'Uint8Array', 'Int16Array',
5-
'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array',
6-
'Float64Array'],
18+
type: types,
719
n: [25]
820
});
921

benchmark/buffers/buffer-base64-decode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const bench = common.createBenchmark(main, {});
77
function main(conf) {
88
const s = 'abcd'.repeat(8 << 20);
99
s.match(/./); // Flatten string.
10-
assert.equal(s.length % 4, 0);
10+
assert.strictEqual(s.length % 4, 0);
1111
const b = Buffer.allocUnsafe(s.length / 4 * 3);
1212
b.write(s, 0, s.length, 'base64');
1313
bench.start();

benchmark/buffers/buffer-indexof.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@ var common = require('../common.js');
33
var fs = require('fs');
44
const path = require('path');
55

6+
const searchStrings = [
7+
'@',
8+
'SQ',
9+
'10x',
10+
'--l',
11+
'Alice',
12+
'Gryphon',
13+
'Panther',
14+
'Ou est ma chatte?',
15+
'found it very',
16+
'among mad people',
17+
'neighbouring pool',
18+
'Soo--oop',
19+
'aaaaaaaaaaaaaaaaa',
20+
'venture to go near the house till she had brought herself down to',
21+
'</i> to the Caterpillar'
22+
];
23+
624
var bench = common.createBenchmark(main, {
7-
search: ['@', 'SQ', '10x', '--l', 'Alice', 'Gryphon', 'Panther',
8-
'Ou est ma chatte?', 'found it very', 'among mad people',
9-
'neighbouring pool', 'Soo--oop', 'aaaaaaaaaaaaaaaaa',
10-
'venture to go near the house till she had brought herself down to',
11-
'</i> to the Caterpillar'],
25+
search: searchStrings,
1226
encoding: ['undefined', 'utf8', 'ucs2', 'binary'],
1327
type: ['buffer', 'string'],
1428
iter: [1]

benchmark/buffers/buffer-read.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
'use strict';
22
var common = require('../common.js');
33

4+
var types = [
5+
'UInt8',
6+
'UInt16LE',
7+
'UInt16BE',
8+
'UInt32LE',
9+
'UInt32BE',
10+
'Int8',
11+
'Int16LE',
12+
'Int16BE',
13+
'Int32LE',
14+
'Int32BE',
15+
'FloatLE',
16+
'FloatBE',
17+
'DoubleLE',
18+
'DoubleBE'
19+
];
20+
421
var bench = common.createBenchmark(main, {
522
noAssert: ['false', 'true'],
623
buffer: ['fast', 'slow'],
7-
type: ['UInt8', 'UInt16LE', 'UInt16BE',
8-
'UInt32LE', 'UInt32BE',
9-
'Int8', 'Int16LE', 'Int16BE',
10-
'Int32LE', 'Int32BE',
11-
'FloatLE', 'FloatBE',
12-
'DoubleLE', 'DoubleBE'],
24+
type: types,
1325
millions: [1]
1426
});
1527

benchmark/buffers/buffer-write.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
'use strict';
22
var common = require('../common.js');
3+
4+
var types = [
5+
'UInt8',
6+
'UInt16LE',
7+
'UInt16BE',
8+
'UInt32LE',
9+
'UInt32BE',
10+
'Int8',
11+
'Int16LE',
12+
'Int16BE',
13+
'Int32LE',
14+
'Int32BE',
15+
'FloatLE',
16+
'FloatBE',
17+
'DoubleLE',
18+
'DoubleBE'
19+
];
20+
321
var bench = common.createBenchmark(main, {
422
noAssert: ['false', 'true'],
523
buffer: ['fast', 'slow'],
6-
type: ['UInt8', 'UInt16LE', 'UInt16BE',
7-
'UInt32LE', 'UInt32BE',
8-
'Int8', 'Int16LE', 'Int16BE',
9-
'Int32LE', 'Int32BE',
10-
'FloatLE', 'FloatBE',
11-
'DoubleLE', 'DoubleBE'],
24+
type: types,
1225
millions: [1]
1326
});
1427

benchmark/buffers/dataview-set.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
'use strict';
22
var common = require('../common.js');
3+
4+
var types = [
5+
'Uint8',
6+
'Uint16LE',
7+
'Uint16BE',
8+
'Uint32LE',
9+
'Uint32BE',
10+
'Int8',
11+
'Int16LE',
12+
'Int16BE',
13+
'Int32LE',
14+
'Int32BE',
15+
'Float32LE',
16+
'Float32BE',
17+
'Float64LE',
18+
'Float64BE'
19+
];
20+
321
var bench = common.createBenchmark(main, {
4-
type: ['Uint8', 'Uint16LE', 'Uint16BE',
5-
'Uint32LE', 'Uint32BE',
6-
'Int8', 'Int16LE', 'Int16BE',
7-
'Int32LE', 'Int32BE',
8-
'Float32LE', 'Float32BE',
9-
'Float64LE', 'Float64BE'],
22+
type: types,
1023
millions: [1]
1124
});
1225

benchmark/es/map-bench.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function runObject(n) {
1515
for (; i < n; i++) {
1616
m['i' + i] = i;
1717
m['s' + i] = String(i);
18-
assert.equal(m['i' + i], m['s' + i]);
18+
assert.strictEqual(String(m['i' + i]), m['s' + i]);
1919
m['i' + i] = undefined;
2020
m['s' + i] = undefined;
2121
}
@@ -29,7 +29,7 @@ function runNullProtoObject(n) {
2929
for (; i < n; i++) {
3030
m['i' + i] = i;
3131
m['s' + i] = String(i);
32-
assert.equal(m['i' + i], m['s' + i]);
32+
assert.strictEqual(String(m['i' + i]), m['s' + i]);
3333
m['i' + i] = undefined;
3434
m['s' + i] = undefined;
3535
}
@@ -53,7 +53,7 @@ function runFakeMap(n) {
5353
for (; i < n; i++) {
5454
m.set('i' + i, i);
5555
m.set('s' + i, String(i));
56-
assert.equal(m.get('i' + i), m.get('s' + i));
56+
assert.strictEqual(String(m.get('i' + i)), m.get('s' + i));
5757
m.set('i' + i, undefined);
5858
m.set('s' + i, undefined);
5959
}
@@ -67,7 +67,7 @@ function runMap(n) {
6767
for (; i < n; i++) {
6868
m.set('i' + i, i);
6969
m.set('s' + i, String(i));
70-
assert.equal(m.get('i' + i), m.get('s' + i));
70+
assert.strictEqual(String(m.get('i' + i)), m.get('s' + i));
7171
m.set('i' + i, undefined);
7272
m.set('s' + i, undefined);
7373
}

benchmark/http/_http_simple.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ var server = module.exports = http.createServer(function(req, res) {
7575
body = fixed;
7676

7777
} else if (command === 'echo') {
78-
res.writeHead(200, { 'Content-Type': 'text/plain',
79-
'Transfer-Encoding': 'chunked' });
78+
const headers = {
79+
'Content-Type': 'text/plain',
80+
'Transfer-Encoding': 'chunked'
81+
};
82+
res.writeHead(200, headers);
8083
req.pipe(res);
8184
return;
8285

@@ -88,8 +91,11 @@ var server = module.exports = http.createServer(function(req, res) {
8891
// example: http://localhost:port/bytes/512/4
8992
// sends a 512 byte body in 4 chunks of 128 bytes
9093
if (n_chunks > 0) {
91-
res.writeHead(status, { 'Content-Type': 'text/plain',
92-
'Transfer-Encoding': 'chunked' });
94+
const headers = {
95+
'Content-Type': 'text/plain',
96+
'Transfer-Encoding': 'chunked'
97+
};
98+
res.writeHead(status, headers);
9399
// send body in chunks
94100
var len = body.length;
95101
var step = Math.floor(len / n_chunks) || 1;
@@ -99,10 +105,12 @@ var server = module.exports = http.createServer(function(req, res) {
99105
}
100106
res.end(body.slice((n_chunks - 1) * step));
101107
} else {
102-
var content_length = body.length.toString();
108+
const headers = {
109+
'Content-Type': 'text/plain',
110+
'Content-Length': body.length.toString()
111+
};
103112

104-
res.writeHead(status, { 'Content-Type': 'text/plain',
105-
'Content-Length': content_length });
113+
res.writeHead(status, headers);
106114
res.end(body);
107115
}
108116
});

benchmark/http/http_server_for_chunky_client.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ try {
2121
} catch (e) { /* ignore */ }
2222

2323
server = http.createServer(function(req, res) {
24-
res.writeHead(200, { 'content-type': 'text/plain',
25-
'content-length': '2' });
24+
var headers = {
25+
'content-type': 'text/plain',
26+
'content-length': '2'
27+
};
28+
res.writeHead(200, headers);
2629
res.end('ok');
2730
});
2831

benchmark/misc/console.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ const v8 = require('v8');
88

99
v8.setFlagsFromString('--allow_natives_syntax');
1010

11+
const methods = [
12+
'restAndSpread',
13+
'argumentsAndApply',
14+
'restAndApply',
15+
'restAndConcat'
16+
];
17+
1118
var bench = common.createBenchmark(main, {
12-
method: ['restAndSpread',
13-
'argumentsAndApply',
14-
'restAndApply',
15-
'restAndConcat'],
19+
method: methods,
1620
concat: [1, 0],
1721
n: [1000000]
1822
});

benchmark/querystring/querystring-parse.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ var common = require('../common.js');
33
var querystring = require('querystring');
44
var v8 = require('v8');
55

6+
var types = [
7+
'noencode',
8+
'multicharsep',
9+
'encodemany',
10+
'encodelast',
11+
'multivalue',
12+
'multivaluemany',
13+
'manypairs'
14+
];
15+
616
var bench = common.createBenchmark(main, {
7-
type: ['noencode',
8-
'multicharsep',
9-
'encodemany',
10-
'encodelast',
11-
'multivalue',
12-
'multivaluemany',
13-
'manypairs'],
17+
type: types,
1418
n: [1e6],
1519
});
1620

0 commit comments

Comments
 (0)