0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 15:30:56 +01:00
nodejs/benchmark/crypto/hash-stream-throughput.js
Rich Trott dcfda1007b tools,benchmark: increase lint compliance
In the hopes of soon having the benchmark code linted, this change
groups all the likely non-controversial lint-compliance changes such as
indentation, semi-colon usage, and single-vs.-double quotation marks.

Other lint rules may have subtle performance implications in the V8
currently shipped with Node.js. Those changes will require more careful
review and will be in a separate change.

PR-URL: https://github.com/nodejs/node/pull/5429
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Brian White <mscdex@mscdex.net>
2016-02-27 20:15:17 -08:00

76 lines
1.8 KiB
JavaScript

// throughput benchmark
// creates a single hasher, then pushes a bunch of data through it
'use strict';
var common = require('../common.js');
var crypto = require('crypto');
var bench = common.createBenchmark(main, {
writes: [500],
algo: ['sha1', 'sha256', 'sha512'],
type: ['asc', 'utf', 'buf'],
len: [2, 1024, 102400, 1024 * 1024],
api: ['legacy', 'stream']
});
function main(conf) {
var api = conf.api;
if (api === 'stream' && process.version.match(/^v0\.[0-8]\./)) {
console.error('Crypto streams not available until v0.10');
// use the legacy, just so that we can compare them.
api = 'legacy';
}
var message;
var encoding;
switch (conf.type) {
case 'asc':
message = new Array(conf.len + 1).join('a');
encoding = 'ascii';
break;
case 'utf':
message = new Array(conf.len / 2 + 1).join('ü');
encoding = 'utf8';
break;
case 'buf':
message = new Buffer(conf.len);
message.fill('b');
break;
default:
throw new Error('unknown message type: ' + conf.type);
}
var fn = api === 'stream' ? streamWrite : legacyWrite;
bench.start();
fn(conf.algo, message, encoding, conf.writes, conf.len);
}
function legacyWrite(algo, message, encoding, writes, len) {
var written = writes * len;
var bits = written * 8;
var gbits = bits / (1024 * 1024 * 1024);
var h = crypto.createHash(algo);
while (writes-- > 0)
h.update(message, encoding);
h.digest();
bench.end(gbits);
}
function streamWrite(algo, message, encoding, writes, len) {
var written = writes * len;
var bits = written * 8;
var gbits = bits / (1024 * 1024 * 1024);
var h = crypto.createHash(algo);
while (writes-- > 0)
h.write(message, encoding);
h.end();
h.read();
bench.end(gbits);
}