Browse Source

benchmark: refactor to eliminate redeclared vars

In order to comply with linting rules used in the rest of the code base,
eliminate redeclared variables. A conservative approach is used so as to
avoid unintentional performance issues (for example, as might be seen in
some situations when using `let` instead of `var`).

PR-URL: https://github.com/nodejs/node/pull/5773
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
v4.x
Rich Trott 9 years ago
committed by Myles Borins
parent
commit
4be2065dbc
  1. 7
      benchmark/buffers/buffer-base64-encode.js
  2. 5
      benchmark/buffers/buffer-tostring.js
  3. 21
      benchmark/compare.js
  4. 9
      benchmark/crypto/cipher-stream.js
  5. 7
      benchmark/events/ee-add-remove.js
  6. 5
      benchmark/fs-write-stream-throughput.js
  7. 11
      benchmark/http_simple.js
  8. 15
      benchmark/http_simple_auto.js
  9. 5
      benchmark/misc/string-decoder.js
  10. 16
      benchmark/querystring/querystring-parse.js

7
benchmark/buffers/buffer-base64-encode.js

@ -6,9 +6,10 @@ function main(conf) {
var N = 64 * 1024 * 1024; var N = 64 * 1024 * 1024;
var b = Buffer(N); var b = Buffer(N);
var s = ''; var s = '';
for (var i = 0; i < 256; ++i) s += String.fromCharCode(i); var i;
for (var i = 0; i < N; i += 256) b.write(s, i, 256, 'ascii'); for (i = 0; i < 256; ++i) s += String.fromCharCode(i);
for (i = 0; i < N; i += 256) b.write(s, i, 256, 'ascii');
bench.start(); bench.start();
for (var i = 0; i < 32; ++i) b.toString('base64'); for (i = 0; i < 32; ++i) b.toString('base64');
bench.end(64); bench.end(64);
} }

5
benchmark/buffers/buffer-tostring.js

@ -14,12 +14,13 @@ function main(conf) {
const n = conf.n | 0; const n = conf.n | 0;
const buf = Buffer(len).fill(42); const buf = Buffer(len).fill(42);
var i;
bench.start(); bench.start();
if (arg) { if (arg) {
for (var i = 0; i < n; i += 1) for (i = 0; i < n; i += 1)
buf.toString('utf8'); buf.toString('utf8');
} else { } else {
for (var i = 0; i < n; i += 1) for (i = 0; i < n; i += 1)
buf.toString(); buf.toString();
} }
bench.end(n); bench.end(n);

21
benchmark/compare.js

@ -36,18 +36,19 @@ for (var i = 2; i < process.argv.length; i++) {
} }
} }
var start, green, red, reset, end;
if (!html) { if (!html) {
var start = ''; start = '';
var green = '\033[1;32m'; green = '\u001b[1;32m';
var red = '\033[1;31m'; red = '\u001b[1;31m';
var reset = '\033[m'; reset = '\u001b[m';
var end = ''; end = '';
} else { } else {
var start = '<pre style="background-color:#333;color:#eee">'; start = '<pre style="background-color:#333;color:#eee">';
var green = '<span style="background-color:#0f0;color:#000">'; green = '<span style="background-color:#0f0;color:#000">';
var red = '<span style="background-color:#f00;color:#fff">'; red = '<span style="background-color:#f00;color:#fff">';
var reset = '</span>'; reset = '</span>';
var end = '</pre>'; end = '</pre>';
} }
var runBench = process.env.NODE_BENCH || 'bench'; var runBench = process.env.NODE_BENCH || 'bench';

9
benchmark/crypto/cipher-stream.js

@ -85,13 +85,14 @@ function streamWrite(alice, bob, message, encoding, writes) {
function legacyWrite(alice, bob, message, encoding, writes) { function legacyWrite(alice, bob, message, encoding, writes) {
var written = 0; var written = 0;
var enc, dec;
for (var i = 0; i < writes; i++) { for (var i = 0; i < writes; i++) {
var enc = alice.update(message, encoding); enc = alice.update(message, encoding);
var dec = bob.update(enc); dec = bob.update(enc);
written += dec.length; written += dec.length;
} }
var enc = alice.final(); enc = alice.final();
var dec = bob.update(enc); dec = bob.update(enc);
written += dec.length; written += dec.length;
dec = bob.final(); dec = bob.final();
written += dec.length; written += dec.length;

7
benchmark/events/ee-add-remove.js

@ -9,14 +9,15 @@ function main(conf) {
var ee = new events.EventEmitter(); var ee = new events.EventEmitter();
var listeners = []; var listeners = [];
for (var k = 0; k < 10; k += 1) var k;
for (k = 0; k < 10; k += 1)
listeners.push(function() {}); listeners.push(function() {});
bench.start(); bench.start();
for (var i = 0; i < n; i += 1) { for (var i = 0; i < n; i += 1) {
for (var k = listeners.length; --k >= 0; /* empty */) for (k = listeners.length; --k >= 0; /* empty */)
ee.on('dummy', listeners[k]); ee.on('dummy', listeners[k]);
for (var k = listeners.length; --k >= 0; /* empty */) for (k = listeners.length; --k >= 0; /* empty */)
ee.removeListener('dummy', listeners[k]); ee.removeListener('dummy', listeners[k]);
} }
bench.end(n); bench.end(n);

5
benchmark/fs-write-stream-throughput.js

@ -39,12 +39,13 @@ function parent() {
function runTest(dur, size, type) { function runTest(dur, size, type) {
if (type !== 'string') if (type !== 'string')
type = 'buffer'; type = 'buffer';
var chunk;
switch (type) { switch (type) {
case 'string': case 'string':
var chunk = new Array(size + 1).join('a'); chunk = new Array(size + 1).join('a');
break; break;
case 'buffer': case 'buffer':
var chunk = new Buffer(size); chunk = new Buffer(size);
chunk.fill('a'); chunk.fill('a');
break; break;
} }

11
benchmark/http_simple.js

@ -36,8 +36,9 @@ var server = module.exports = http.createServer(function(req, res) {
var n_chunks = parseInt(commands[3], 10); var n_chunks = parseInt(commands[3], 10);
var status = 200; var status = 200;
var n, i;
if (command == 'bytes') { if (command == 'bytes') {
var n = ~~arg; n = ~~arg;
if (n <= 0) if (n <= 0)
throw new Error('bytes called with n <= 0'); throw new Error('bytes called with n <= 0');
if (storedBytes[n] === undefined) { if (storedBytes[n] === undefined) {
@ -46,19 +47,19 @@ var server = module.exports = http.createServer(function(req, res) {
body = storedBytes[n]; body = storedBytes[n];
} else if (command == 'buffer') { } else if (command == 'buffer') {
var n = ~~arg; n = ~~arg;
if (n <= 0) if (n <= 0)
throw new Error('buffer called with n <= 0'); throw new Error('buffer called with n <= 0');
if (storedBuffer[n] === undefined) { if (storedBuffer[n] === undefined) {
storedBuffer[n] = new Buffer(n); storedBuffer[n] = new Buffer(n);
for (var i = 0; i < n; i++) { for (i = 0; i < n; i++) {
storedBuffer[n][i] = 'C'.charCodeAt(0); storedBuffer[n][i] = 'C'.charCodeAt(0);
} }
} }
body = storedBuffer[n]; body = storedBuffer[n];
} else if (command == 'unicode') { } else if (command == 'unicode') {
var n = ~~arg; n = ~~arg;
if (n <= 0) if (n <= 0)
throw new Error('unicode called with n <= 0'); throw new Error('unicode called with n <= 0');
if (storedUnicode[n] === undefined) { if (storedUnicode[n] === undefined) {
@ -93,7 +94,7 @@ var server = module.exports = http.createServer(function(req, res) {
var len = body.length; var len = body.length;
var step = Math.floor(len / n_chunks) || 1; var step = Math.floor(len / n_chunks) || 1;
for (var i = 0, n = (n_chunks - 1); i < n; ++i) { for (i = 0, n = (n_chunks - 1); i < n; ++i) {
res.write(body.slice(i * step, i * step + step)); res.write(body.slice(i * step, i * step + step));
} }
res.end(body.slice((n_chunks - 1) * step)); res.end(body.slice((n_chunks - 1) * step));

15
benchmark/http_simple_auto.js

@ -13,7 +13,8 @@ var spawn = require('child_process').spawn;
var port = parseInt(process.env.PORT || 8000); var port = parseInt(process.env.PORT || 8000);
var fixed = ''; var fixed = '';
for (var i = 0; i < 20 * 1024; i++) { var i;
for (i = 0; i < 20 * 1024; i++) {
fixed += 'C'; fixed += 'C';
} }
@ -27,25 +28,25 @@ var server = http.createServer(function(req, res) {
var arg = commands[2]; var arg = commands[2];
var n_chunks = parseInt(commands[3], 10); var n_chunks = parseInt(commands[3], 10);
var status = 200; var status = 200;
var n;
if (command == 'bytes') { if (command == 'bytes') {
var n = parseInt(arg, 10); n = parseInt(arg, 10);
if (n <= 0) if (n <= 0)
throw new Error('bytes called with n <= 0'); throw new Error('bytes called with n <= 0');
if (stored[n] === undefined) { if (stored[n] === undefined) {
stored[n] = ''; stored[n] = '';
for (var i = 0; i < n; i++) { for (i = 0; i < n; i++) {
stored[n] += 'C'; stored[n] += 'C';
} }
} }
body = stored[n]; body = stored[n];
} else if (command == 'buffer') { } else if (command == 'buffer') {
var n = parseInt(arg, 10); n = parseInt(arg, 10);
if (n <= 0) throw new Error('bytes called with n <= 0'); if (n <= 0) throw new Error('bytes called with n <= 0');
if (storedBuffer[n] === undefined) { if (storedBuffer[n] === undefined) {
storedBuffer[n] = new Buffer(n); storedBuffer[n] = new Buffer(n);
for (var i = 0; i < n; i++) { for (i = 0; i < n; i++) {
storedBuffer[n][i] = 'C'.charCodeAt(0); storedBuffer[n][i] = 'C'.charCodeAt(0);
} }
} }
@ -78,7 +79,7 @@ var server = http.createServer(function(req, res) {
var len = body.length; var len = body.length;
var step = Math.floor(len / n_chunks) || 1; var step = Math.floor(len / n_chunks) || 1;
for (var i = 0, n = (n_chunks - 1); i < n; ++i) { for (i = 0, n = (n_chunks - 1); i < n; ++i) {
res.write(body.slice(i * step, i * step + step)); res.write(body.slice(i * step, i * step + step));
} }
res.end(body.slice((n_chunks - 1) * step)); res.end(body.slice((n_chunks - 1) * step));

5
benchmark/misc/string-decoder.js

@ -21,6 +21,7 @@ function main(conf) {
var chunks = []; var chunks = [];
var str = ''; var str = '';
var isBase64 = (encoding === 'base64-ascii' || encoding === 'base64-utf8'); var isBase64 = (encoding === 'base64-ascii' || encoding === 'base64-utf8');
var i;
if (encoding === 'ascii' || encoding === 'base64-ascii') if (encoding === 'ascii' || encoding === 'base64-ascii')
alpha = ASC_ALPHA; alpha = ASC_ALPHA;
@ -31,7 +32,7 @@ function main(conf) {
var sd = new StringDecoder(isBase64 ? 'base64' : encoding); var sd = new StringDecoder(isBase64 ? 'base64' : encoding);
for (var i = 0; i < inLen; ++i) { for (i = 0; i < inLen; ++i) {
if (i > 0 && (i % chunkLen) === 0 && !isBase64) { if (i > 0 && (i % chunkLen) === 0 && !isBase64) {
chunks.push(new Buffer(str, encoding)); chunks.push(new Buffer(str, encoding));
str = ''; str = '';
@ -52,7 +53,7 @@ function main(conf) {
var nChunks = chunks.length; var nChunks = chunks.length;
bench.start(); bench.start();
for (var i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
for (var j = 0; j < nChunks; ++j) for (var j = 0; j < nChunks; ++j)
sd.write(chunks[j]); sd.write(chunks[j]);
} }

16
benchmark/querystring/querystring-parse.js

@ -27,8 +27,16 @@ function main(conf) {
v8.setFlagsFromString('--allow_natives_syntax'); v8.setFlagsFromString('--allow_natives_syntax');
eval('%OptimizeFunctionOnNextCall(querystring.parse)'); eval('%OptimizeFunctionOnNextCall(querystring.parse)');
bench.start(); var i;
for (var i = 0; i < n; i += 1) if (type !== 'multicharsep') {
querystring.parse(input); bench.start();
bench.end(n); for (i = 0; i < n; i += 1)
querystring.parse(input);
bench.end(n);
} else {
bench.start();
for (i = 0; i < n; i += 1)
querystring.parse(input, '&&&&&&&&&&');
bench.end(n);
}
} }

Loading…
Cancel
Save