2019-03-22 02:44:26 +00:00
|
|
|
// Throughput benchmark
|
2013-04-30 21:50:21 +00:00
|
|
|
// creates a single hasher, then pushes a bunch of data through it
|
2016-02-20 01:03:16 +00:00
|
|
|
'use strict';
|
2017-09-14 01:48:53 +00:00
|
|
|
const common = require('../common.js');
|
|
|
|
const crypto = require('crypto');
|
2013-04-30 21:50:21 +00:00
|
|
|
|
2017-09-14 01:48:53 +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],
|
2023-02-10 00:54:53 +00:00
|
|
|
api: ['legacy', 'stream'],
|
2013-04-30 21:50:21 +00:00
|
|
|
});
|
|
|
|
|
2018-01-23 12:17:56 +00:00
|
|
|
function main({ api, type, len, out, writes, algo }) {
|
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');
|
2019-01-21 00:22:27 +00:00
|
|
|
// Use the legacy, just so that we can compare them.
|
2013-04-30 21:50:21 +00:00
|
|
|
api = 'legacy';
|
|
|
|
}
|
|
|
|
|
2019-12-30 11:20:07 +00:00
|
|
|
let message;
|
|
|
|
let encoding;
|
2018-01-23 12:17:56 +00:00
|
|
|
switch (type) {
|
2013-04-30 21:50:21 +00:00
|
|
|
case 'asc':
|
2018-01-23 12:17:56 +00:00
|
|
|
message = 'a'.repeat(len);
|
2013-04-30 21:50:21 +00:00
|
|
|
encoding = 'ascii';
|
|
|
|
break;
|
|
|
|
case 'utf':
|
2018-01-23 12:17:56 +00:00
|
|
|
message = 'ü'.repeat(len / 2);
|
2013-04-30 21:50:21 +00:00
|
|
|
encoding = 'utf8';
|
|
|
|
break;
|
|
|
|
case 'buf':
|
2018-01-23 12:17:56 +00:00
|
|
|
message = Buffer.alloc(len, 'b');
|
2013-04-30 21:50:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-23 12:17:56 +00:00
|
|
|
throw new Error(`unknown message type: ${type}`);
|
2013-04-30 21:50:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 01:48:53 +00:00
|
|
|
const fn = api === 'stream' ? streamWrite : legacyWrite;
|
2013-04-30 21:50:21 +00:00
|
|
|
|
|
|
|
bench.start();
|
2018-01-23 12:17:56 +00:00
|
|
|
fn(algo, message, encoding, writes, len, out);
|
2013-04-30 21:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function legacyWrite(algo, message, encoding, writes, len, outEnc) {
|
2017-09-14 01:48:53 +00:00
|
|
|
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) {
|
2017-09-14 01:48:53 +00:00
|
|
|
const h = crypto.createHash(algo);
|
2013-04-30 21:50:21 +00:00
|
|
|
h.update(message, encoding);
|
2022-01-17 05:31:21 +00:00
|
|
|
h.digest(outEnc);
|
2013-04-30 21:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bench.end(gbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
function streamWrite(algo, message, encoding, writes, len, outEnc) {
|
2017-09-14 01:48:53 +00:00
|
|
|
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) {
|
2017-09-14 01:48:53 +00:00
|
|
|
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);
|
|
|
|
}
|