Browse Source

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: https://github.com/nodejs/node/pull/9790
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
v6
Rich Trott 8 years ago
parent
commit
566a1513d1
  1. 20
      benchmark/_http-benchmarkers.js
  2. 18
      benchmark/arrays/var-int.js
  3. 18
      benchmark/arrays/zero-float.js
  4. 18
      benchmark/arrays/zero-int.js
  5. 2
      benchmark/buffers/buffer-base64-decode.js
  6. 24
      benchmark/buffers/buffer-indexof.js
  7. 24
      benchmark/buffers/buffer-read.js
  8. 25
      benchmark/buffers/buffer-write.js
  9. 25
      benchmark/buffers/dataview-set.js
  10. 8
      benchmark/es/map-bench.js
  11. 22
      benchmark/http/_http_simple.js
  12. 7
      benchmark/http/http_server_for_chunky_client.js
  13. 10
      benchmark/misc/console.js
  14. 10
      benchmark/querystring/querystring-parse.js
  15. 6
      benchmark/tls/throughput.js
  16. 15
      benchmark/tls/tls-connect.js
  17. 11
      benchmark/util/format.js

20
benchmark/_http-benchmarkers.js

@ -15,8 +15,13 @@ function AutocannonBenchmarker() {
} }
AutocannonBenchmarker.prototype.create = function(options) { AutocannonBenchmarker.prototype.create = function(options) {
const args = ['-d', options.duration, '-c', options.connections, '-j', '-n', const args = [
`http://127.0.0.1:${options.port}${options.path}` ]; '-d', options.duration,
'-c', options.connections,
'-j',
'-n',
`http://127.0.0.1:${options.port}${options.path}`
];
const child = child_process.spawn(this.autocannon_exe, args); const child = child_process.spawn(this.autocannon_exe, args);
return child; return child;
}; };
@ -43,8 +48,12 @@ function WrkBenchmarker() {
} }
WrkBenchmarker.prototype.create = function(options) { WrkBenchmarker.prototype.create = function(options) {
const args = ['-d', options.duration, '-c', options.connections, '-t', 8, const args = [
`http://127.0.0.1:${options.port}${options.path}` ]; '-d', options.duration,
'-c', options.connections,
'-t', 8,
`http://127.0.0.1:${options.port}${options.path}`
];
const child = child_process.spawn('wrk', args); const child = child_process.spawn('wrk', args);
return child; return child;
}; };
@ -59,8 +68,7 @@ WrkBenchmarker.prototype.processResults = function(output) {
} }
}; };
const http_benchmarkers = [ new WrkBenchmarker(), const http_benchmarkers = [new WrkBenchmarker(), new AutocannonBenchmarker()];
new AutocannonBenchmarker() ];
const benchmarkers = {}; const benchmarkers = {};

18
benchmark/arrays/var-int.js

@ -1,9 +1,21 @@
'use strict'; 'use strict';
var common = require('../common.js'); var common = require('../common.js');
var types = [
'Array',
'Buffer',
'Int8Array',
'Uint8Array',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array'
];
var bench = common.createBenchmark(main, { var bench = common.createBenchmark(main, {
type: ['Array', 'Buffer', 'Int8Array', 'Uint8Array', 'Int16Array', type: types,
'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array',
'Float64Array'],
n: [25] n: [25]
}); });

18
benchmark/arrays/zero-float.js

@ -1,9 +1,21 @@
'use strict'; 'use strict';
var common = require('../common.js'); var common = require('../common.js');
var types = [
'Array',
'Buffer',
'Int8Array',
'Uint8Array',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array'
];
var bench = common.createBenchmark(main, { var bench = common.createBenchmark(main, {
type: ['Array', 'Buffer', 'Int8Array', 'Uint8Array', 'Int16Array', type: types,
'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array',
'Float64Array'],
n: [25] n: [25]
}); });

18
benchmark/arrays/zero-int.js

@ -1,9 +1,21 @@
'use strict'; 'use strict';
var common = require('../common.js'); var common = require('../common.js');
var types = [
'Array',
'Buffer',
'Int8Array',
'Uint8Array',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array'
];
var bench = common.createBenchmark(main, { var bench = common.createBenchmark(main, {
type: ['Array', 'Buffer', 'Int8Array', 'Uint8Array', 'Int16Array', type: types,
'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array',
'Float64Array'],
n: [25] n: [25]
}); });

2
benchmark/buffers/buffer-base64-decode.js

@ -7,7 +7,7 @@ const bench = common.createBenchmark(main, {});
function main(conf) { function main(conf) {
const s = 'abcd'.repeat(8 << 20); const s = 'abcd'.repeat(8 << 20);
s.match(/./); // Flatten string. s.match(/./); // Flatten string.
assert.equal(s.length % 4, 0); assert.strictEqual(s.length % 4, 0);
const b = Buffer.allocUnsafe(s.length / 4 * 3); const b = Buffer.allocUnsafe(s.length / 4 * 3);
b.write(s, 0, s.length, 'base64'); b.write(s, 0, s.length, 'base64');
bench.start(); bench.start();

24
benchmark/buffers/buffer-indexof.js

@ -3,12 +3,26 @@ var common = require('../common.js');
var fs = require('fs'); var fs = require('fs');
const path = require('path'); const path = require('path');
var bench = common.createBenchmark(main, { const searchStrings = [
search: ['@', 'SQ', '10x', '--l', 'Alice', 'Gryphon', 'Panther', '@',
'Ou est ma chatte?', 'found it very', 'among mad people', 'SQ',
'neighbouring pool', 'Soo--oop', 'aaaaaaaaaaaaaaaaa', '10x',
'--l',
'Alice',
'Gryphon',
'Panther',
'Ou est ma chatte?',
'found it very',
'among mad people',
'neighbouring pool',
'Soo--oop',
'aaaaaaaaaaaaaaaaa',
'venture to go near the house till she had brought herself down to', 'venture to go near the house till she had brought herself down to',
'</i> to the Caterpillar'], '</i> to the Caterpillar'
];
var bench = common.createBenchmark(main, {
search: searchStrings,
encoding: ['undefined', 'utf8', 'ucs2', 'binary'], encoding: ['undefined', 'utf8', 'ucs2', 'binary'],
type: ['buffer', 'string'], type: ['buffer', 'string'],
iter: [1] iter: [1]

24
benchmark/buffers/buffer-read.js

@ -1,15 +1,27 @@
'use strict'; 'use strict';
var common = require('../common.js'); var common = require('../common.js');
var types = [
'UInt8',
'UInt16LE',
'UInt16BE',
'UInt32LE',
'UInt32BE',
'Int8',
'Int16LE',
'Int16BE',
'Int32LE',
'Int32BE',
'FloatLE',
'FloatBE',
'DoubleLE',
'DoubleBE'
];
var bench = common.createBenchmark(main, { var bench = common.createBenchmark(main, {
noAssert: ['false', 'true'], noAssert: ['false', 'true'],
buffer: ['fast', 'slow'], buffer: ['fast', 'slow'],
type: ['UInt8', 'UInt16LE', 'UInt16BE', type: types,
'UInt32LE', 'UInt32BE',
'Int8', 'Int16LE', 'Int16BE',
'Int32LE', 'Int32BE',
'FloatLE', 'FloatBE',
'DoubleLE', 'DoubleBE'],
millions: [1] millions: [1]
}); });

25
benchmark/buffers/buffer-write.js

@ -1,14 +1,27 @@
'use strict'; 'use strict';
var common = require('../common.js'); var common = require('../common.js');
var types = [
'UInt8',
'UInt16LE',
'UInt16BE',
'UInt32LE',
'UInt32BE',
'Int8',
'Int16LE',
'Int16BE',
'Int32LE',
'Int32BE',
'FloatLE',
'FloatBE',
'DoubleLE',
'DoubleBE'
];
var bench = common.createBenchmark(main, { var bench = common.createBenchmark(main, {
noAssert: ['false', 'true'], noAssert: ['false', 'true'],
buffer: ['fast', 'slow'], buffer: ['fast', 'slow'],
type: ['UInt8', 'UInt16LE', 'UInt16BE', type: types,
'UInt32LE', 'UInt32BE',
'Int8', 'Int16LE', 'Int16BE',
'Int32LE', 'Int32BE',
'FloatLE', 'FloatBE',
'DoubleLE', 'DoubleBE'],
millions: [1] millions: [1]
}); });

25
benchmark/buffers/dataview-set.js

@ -1,12 +1,25 @@
'use strict'; 'use strict';
var common = require('../common.js'); var common = require('../common.js');
var types = [
'Uint8',
'Uint16LE',
'Uint16BE',
'Uint32LE',
'Uint32BE',
'Int8',
'Int16LE',
'Int16BE',
'Int32LE',
'Int32BE',
'Float32LE',
'Float32BE',
'Float64LE',
'Float64BE'
];
var bench = common.createBenchmark(main, { var bench = common.createBenchmark(main, {
type: ['Uint8', 'Uint16LE', 'Uint16BE', type: types,
'Uint32LE', 'Uint32BE',
'Int8', 'Int16LE', 'Int16BE',
'Int32LE', 'Int32BE',
'Float32LE', 'Float32BE',
'Float64LE', 'Float64BE'],
millions: [1] millions: [1]
}); });

8
benchmark/es/map-bench.js

@ -15,7 +15,7 @@ function runObject(n) {
for (; i < n; i++) { for (; i < n; i++) {
m['i' + i] = i; m['i' + i] = i;
m['s' + i] = String(i); m['s' + i] = String(i);
assert.equal(m['i' + i], m['s' + i]); assert.strictEqual(String(m['i' + i]), m['s' + i]);
m['i' + i] = undefined; m['i' + i] = undefined;
m['s' + i] = undefined; m['s' + i] = undefined;
} }
@ -29,7 +29,7 @@ function runNullProtoObject(n) {
for (; i < n; i++) { for (; i < n; i++) {
m['i' + i] = i; m['i' + i] = i;
m['s' + i] = String(i); m['s' + i] = String(i);
assert.equal(m['i' + i], m['s' + i]); assert.strictEqual(String(m['i' + i]), m['s' + i]);
m['i' + i] = undefined; m['i' + i] = undefined;
m['s' + i] = undefined; m['s' + i] = undefined;
} }
@ -53,7 +53,7 @@ function runFakeMap(n) {
for (; i < n; i++) { for (; i < n; i++) {
m.set('i' + i, i); m.set('i' + i, i);
m.set('s' + i, String(i)); m.set('s' + i, String(i));
assert.equal(m.get('i' + i), m.get('s' + i)); assert.strictEqual(String(m.get('i' + i)), m.get('s' + i));
m.set('i' + i, undefined); m.set('i' + i, undefined);
m.set('s' + i, undefined); m.set('s' + i, undefined);
} }
@ -67,7 +67,7 @@ function runMap(n) {
for (; i < n; i++) { for (; i < n; i++) {
m.set('i' + i, i); m.set('i' + i, i);
m.set('s' + i, String(i)); m.set('s' + i, String(i));
assert.equal(m.get('i' + i), m.get('s' + i)); assert.strictEqual(String(m.get('i' + i)), m.get('s' + i));
m.set('i' + i, undefined); m.set('i' + i, undefined);
m.set('s' + i, undefined); m.set('s' + i, undefined);
} }

22
benchmark/http/_http_simple.js

@ -75,8 +75,11 @@ var server = module.exports = http.createServer(function(req, res) {
body = fixed; body = fixed;
} else if (command === 'echo') { } else if (command === 'echo') {
res.writeHead(200, { 'Content-Type': 'text/plain', const headers = {
'Transfer-Encoding': 'chunked' }); 'Content-Type': 'text/plain',
'Transfer-Encoding': 'chunked'
};
res.writeHead(200, headers);
req.pipe(res); req.pipe(res);
return; return;
@ -88,8 +91,11 @@ var server = module.exports = http.createServer(function(req, res) {
// example: http://localhost:port/bytes/512/4 // example: http://localhost:port/bytes/512/4
// sends a 512 byte body in 4 chunks of 128 bytes // sends a 512 byte body in 4 chunks of 128 bytes
if (n_chunks > 0) { if (n_chunks > 0) {
res.writeHead(status, { 'Content-Type': 'text/plain', const headers = {
'Transfer-Encoding': 'chunked' }); 'Content-Type': 'text/plain',
'Transfer-Encoding': 'chunked'
};
res.writeHead(status, headers);
// send body in chunks // send body in chunks
var len = body.length; var len = body.length;
var step = Math.floor(len / n_chunks) || 1; var step = Math.floor(len / n_chunks) || 1;
@ -99,10 +105,12 @@ var server = module.exports = http.createServer(function(req, res) {
} }
res.end(body.slice((n_chunks - 1) * step)); res.end(body.slice((n_chunks - 1) * step));
} else { } else {
var content_length = body.length.toString(); const headers = {
'Content-Type': 'text/plain',
'Content-Length': body.length.toString()
};
res.writeHead(status, { 'Content-Type': 'text/plain', res.writeHead(status, headers);
'Content-Length': content_length });
res.end(body); res.end(body);
} }
}); });

7
benchmark/http/http_server_for_chunky_client.js

@ -21,8 +21,11 @@ try {
} catch (e) { /* ignore */ } } catch (e) { /* ignore */ }
server = http.createServer(function(req, res) { server = http.createServer(function(req, res) {
res.writeHead(200, { 'content-type': 'text/plain', var headers = {
'content-length': '2' }); 'content-type': 'text/plain',
'content-length': '2'
};
res.writeHead(200, headers);
res.end('ok'); res.end('ok');
}); });

10
benchmark/misc/console.js

@ -8,11 +8,15 @@ const v8 = require('v8');
v8.setFlagsFromString('--allow_natives_syntax'); v8.setFlagsFromString('--allow_natives_syntax');
var bench = common.createBenchmark(main, { const methods = [
method: ['restAndSpread', 'restAndSpread',
'argumentsAndApply', 'argumentsAndApply',
'restAndApply', 'restAndApply',
'restAndConcat'], 'restAndConcat'
];
var bench = common.createBenchmark(main, {
method: methods,
concat: [1, 0], concat: [1, 0],
n: [1000000] n: [1000000]
}); });

10
benchmark/querystring/querystring-parse.js

@ -3,14 +3,18 @@ var common = require('../common.js');
var querystring = require('querystring'); var querystring = require('querystring');
var v8 = require('v8'); var v8 = require('v8');
var bench = common.createBenchmark(main, { var types = [
type: ['noencode', 'noencode',
'multicharsep', 'multicharsep',
'encodemany', 'encodemany',
'encodelast', 'encodelast',
'multivalue', 'multivalue',
'multivaluemany', 'multivaluemany',
'manypairs'], 'manypairs'
];
var bench = common.createBenchmark(main, {
type: types,
n: [1e6], n: [1e6],
}); });

6
benchmark/tls/throughput.js

@ -37,10 +37,12 @@ function main(conf) {
throw new Error('invalid type'); throw new Error('invalid type');
} }
options = { key: fs.readFileSync(cert_dir + '/test_key.pem'), options = {
key: fs.readFileSync(cert_dir + '/test_key.pem'),
cert: fs.readFileSync(cert_dir + '/test_cert.pem'), cert: fs.readFileSync(cert_dir + '/test_cert.pem'),
ca: [ fs.readFileSync(cert_dir + '/test_ca.pem') ], ca: [ fs.readFileSync(cert_dir + '/test_ca.pem') ],
ciphers: 'AES256-GCM-SHA384' }; ciphers: 'AES256-GCM-SHA384'
};
server = tls.createServer(options, onConnection); server = tls.createServer(options, onConnection);
setTimeout(done, dur * 1000); setTimeout(done, dur * 1000);

15
benchmark/tls/tls-connect.js

@ -20,11 +20,13 @@ function main(conf) {
dur = +conf.dur; dur = +conf.dur;
concurrency = +conf.concurrency; concurrency = +conf.concurrency;
var cert_dir = path.resolve(__dirname, '../../test/fixtures'), var cert_dir = path.resolve(__dirname, '../../test/fixtures');
options = { key: fs.readFileSync(cert_dir + '/test_key.pem'), var options = {
key: fs.readFileSync(cert_dir + '/test_key.pem'),
cert: fs.readFileSync(cert_dir + '/test_cert.pem'), cert: fs.readFileSync(cert_dir + '/test_cert.pem'),
ca: [ fs.readFileSync(cert_dir + '/test_ca.pem') ], ca: [ fs.readFileSync(cert_dir + '/test_ca.pem') ],
ciphers: 'AES256-GCM-SHA384' }; ciphers: 'AES256-GCM-SHA384'
};
server = tls.createServer(options, onConnection); server = tls.createServer(options, onConnection);
server.listen(common.PORT, onListening); server.listen(common.PORT, onListening);
@ -42,8 +44,11 @@ function onConnection(conn) {
} }
function makeConnection() { function makeConnection() {
var conn = tls.connect({ port: common.PORT, var options = {
rejectUnauthorized: false }, function() { port: common.PORT,
rejectUnauthorized: false
};
var conn = tls.connect(options, function() {
clientConn++; clientConn++;
conn.on('error', function(er) { conn.on('error', function(er) {
console.error('client error', er); console.error('client error', er);

11
benchmark/util/format.js

@ -3,13 +3,16 @@
const util = require('util'); const util = require('util');
const common = require('../common'); const common = require('../common');
const v8 = require('v8'); const v8 = require('v8');
const bench = common.createBenchmark(main, { const types = [
n: [1e6], 'string',
type: ['string',
'number', 'number',
'object', 'object',
'unknown', 'unknown',
'no-replace'] 'no-replace'
];
const bench = common.createBenchmark(main, {
n: [1e6],
type: types
}); });
const inputs = { const inputs = {

Loading…
Cancel
Save