node/benchmark/crypto/hash-stream-creation.js

84 lines
2.0 KiB
JavaScript
Raw Normal View History

2013-04-30 21:50:21 +00:00
// throughput benchmark
// creates a single hasher, then pushes a bunch of data through it
'use strict';
const common = require('../common.js');
const crypto = require('crypto');
2013-04-30 21:50:21 +00:00
const bench = common.createBenchmark(main, {
2013-04-30 21:50:21 +00:00
writes: [500],
algo: [ 'sha256', 'md5' ],
type: ['asc', 'utf', 'buf'],
out: ['hex', 'binary', 'buffer'],
len: [2, 1024, 102400, 1024 * 1024],
api: ['legacy', 'stream']
});
function main(conf) {
var api = conf.api;
if (api === 'stream' && /^v0\.[0-8]\./.test(process.version)) {
2013-04-30 21:50:21 +00:00
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 = 'a'.repeat(conf.len);
2013-04-30 21:50:21 +00:00
encoding = 'ascii';
break;
case 'utf':
message = 'ü'.repeat(conf.len / 2);
2013-04-30 21:50:21 +00:00
encoding = 'utf8';
break;
case 'buf':
message = Buffer.alloc(conf.len, 'b');
2013-04-30 21:50:21 +00:00
break;
default:
throw new Error(`unknown message type: ${conf.type}`);
2013-04-30 21:50:21 +00:00
}
const fn = api === 'stream' ? streamWrite : legacyWrite;
2013-04-30 21:50:21 +00:00
bench.start();
fn(conf.algo, message, encoding, conf.writes, conf.len, conf.out);
}
function legacyWrite(algo, message, encoding, writes, len, outEnc) {
const written = writes * len;
const bits = written * 8;
const gbits = bits / (1024 * 1024 * 1024);
2013-04-30 21:50:21 +00:00
while (writes-- > 0) {
const h = crypto.createHash(algo);
2013-04-30 21:50:21 +00:00
h.update(message, encoding);
var res = h.digest(outEnc);
// include buffer creation costs for older versions
if (outEnc === 'buffer' && typeof res === 'string')
res = Buffer.from(res, 'binary');
2013-04-30 21:50:21 +00:00
}
bench.end(gbits);
}
function streamWrite(algo, message, encoding, writes, len, outEnc) {
const written = writes * len;
const bits = written * 8;
const gbits = bits / (1024 * 1024 * 1024);
2013-04-30 21:50:21 +00:00
while (writes-- > 0) {
const h = crypto.createHash(algo);
2013-04-30 21:50:21 +00:00
if (outEnc !== 'buffer')
h.setEncoding(outEnc);
h.write(message, encoding);
h.end();
h.read();
}
bench.end(gbits);
}