2013-04-30 21:50:21 +00:00
|
|
|
// throughput benchmark
|
|
|
|
// creates a single hasher, then pushes a bunch of data through it
|
2016-02-20 01:03:16 +00:00
|
|
|
'use strict';
|
2013-04-30 21:50:21 +00:00
|
|
|
var common = require('../common.js');
|
|
|
|
var crypto = require('crypto');
|
|
|
|
|
|
|
|
var bench = common.createBenchmark(main, {
|
|
|
|
writes: [500],
|
2015-04-02 12:33:24 +00:00
|
|
|
algo: ['sha1', 'sha256', 'sha512'],
|
2013-04-30 21:50:21 +00:00
|
|
|
type: ['asc', 'utf', 'buf'],
|
|
|
|
len: [2, 1024, 102400, 1024 * 1024],
|
|
|
|
api: ['legacy', 'stream']
|
|
|
|
});
|
|
|
|
|
|
|
|
function main(conf) {
|
|
|
|
var api = conf.api;
|
2017-06-08 15:30:57 +00:00
|
|
|
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':
|
2017-04-02 21:32:50 +00:00
|
|
|
message = 'a'.repeat(conf.len);
|
2013-04-30 21:50:21 +00:00
|
|
|
encoding = 'ascii';
|
|
|
|
break;
|
|
|
|
case 'utf':
|
2017-04-02 21:32:50 +00:00
|
|
|
message = 'ü'.repeat(conf.len / 2);
|
2013-04-30 21:50:21 +00:00
|
|
|
encoding = 'utf8';
|
|
|
|
break;
|
|
|
|
case 'buf':
|
2016-01-25 23:00:06 +00:00
|
|
|
message = Buffer.alloc(conf.len, 'b');
|
2013-04-30 21:50:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-04-17 01:01:12 +00:00
|
|
|
throw new Error(`unknown message type: ${conf.type}`);
|
2013-04-30 21:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|