Skip to content

Commit

Permalink
benchmark: (crypto) refactor
Browse files Browse the repository at this point in the history
PR-URL: #18320
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and MylesBorins committed Feb 21, 2018
1 parent cb13c7c commit 0851822
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 40 deletions.
8 changes: 4 additions & 4 deletions benchmark/crypto/aes-gcm-throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const bench = common.createBenchmark(main, {
len: [1024, 4 * 1024, 16 * 1024, 64 * 1024, 256 * 1024, 1024 * 1024]
});

function main(conf) {
const message = Buffer.alloc(conf.len, 'b');
const key = crypto.randomBytes(keylen[conf.cipher]);
function main({ n, len, cipher }) {
const message = Buffer.alloc(len, 'b');
const key = crypto.randomBytes(keylen[cipher]);
const iv = crypto.randomBytes(12);
const associate_data = Buffer.alloc(16, 'z');
bench.start();
AEAD_Bench(conf.cipher, message, associate_data, key, iv, conf.n, conf.len);
AEAD_Bench(cipher, message, associate_data, key, iv, n, len);
}

function AEAD_Bench(cipher, message, associate_data, key, iv, n, len) {
Expand Down
19 changes: 9 additions & 10 deletions benchmark/crypto/cipher-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const bench = common.createBenchmark(main, {
api: ['legacy', 'stream']
});

function main(conf) {
var api = conf.api;
function main({ api, cipher, type, len, writes }) {
if (api === 'stream' && /^v0\.[0-8]\./.test(process.version)) {
console.error('Crypto streams not available until v0.10');
// use the legacy, just so that we can compare them.
Expand All @@ -33,33 +32,33 @@ function main(conf) {
// alice_secret and bob_secret should be the same
assert(alice_secret === bob_secret);

const alice_cipher = crypto.createCipher(conf.cipher, alice_secret);
const bob_cipher = crypto.createDecipher(conf.cipher, bob_secret);
const alice_cipher = crypto.createCipher(cipher, alice_secret);
const bob_cipher = crypto.createDecipher(cipher, bob_secret);

var message;
var encoding;
switch (conf.type) {
switch (type) {
case 'asc':
message = 'a'.repeat(conf.len);
message = 'a'.repeat(len);
encoding = 'ascii';
break;
case 'utf':
message = 'ü'.repeat(conf.len / 2);
message = 'ü'.repeat(len / 2);
encoding = 'utf8';
break;
case 'buf':
message = Buffer.alloc(conf.len, 'b');
message = Buffer.alloc(len, 'b');
break;
default:
throw new Error(`unknown message type: ${conf.type}`);
throw new Error(`unknown message type: ${type}`);
}

const fn = api === 'stream' ? streamWrite : legacyWrite;

// write data as fast as possible to alice, and have bob decrypt.
// use old API for comparison to v0.8
bench.start();
fn(alice_cipher, bob_cipher, message, encoding, conf.writes);
fn(alice_cipher, bob_cipher, message, encoding, writes);
}

function streamWrite(alice, bob, message, encoding, writes) {
Expand Down
6 changes: 2 additions & 4 deletions benchmark/crypto/get-ciphers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ const bench = common.createBenchmark(main, {
v: ['crypto', 'tls']
});

function main(conf) {
const n = +conf.n;
const v = conf.v;
function main({ n, v }) {
const method = require(v).getCiphers;
var i = 0;
// first call to getChipers will dominate the results
// First call to getChipers will dominate the results
if (n > 1) {
for (; i < n; i++)
method();
Expand Down
15 changes: 7 additions & 8 deletions benchmark/crypto/hash-stream-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const bench = common.createBenchmark(main, {
api: ['legacy', 'stream']
});

function main(conf) {
var api = conf.api;
function main({ api, type, len, out, writes, algo }) {
if (api === 'stream' && /^v0\.[0-8]\./.test(process.version)) {
console.error('Crypto streams not available until v0.10');
// use the legacy, just so that we can compare them.
Expand All @@ -23,26 +22,26 @@ function main(conf) {

var message;
var encoding;
switch (conf.type) {
switch (type) {
case 'asc':
message = 'a'.repeat(conf.len);
message = 'a'.repeat(len);
encoding = 'ascii';
break;
case 'utf':
message = 'ü'.repeat(conf.len / 2);
message = 'ü'.repeat(len / 2);
encoding = 'utf8';
break;
case 'buf':
message = Buffer.alloc(conf.len, 'b');
message = Buffer.alloc(len, 'b');
break;
default:
throw new Error(`unknown message type: ${conf.type}`);
throw new Error(`unknown message type: ${type}`);
}

const fn = api === 'stream' ? streamWrite : legacyWrite;

bench.start();
fn(conf.algo, message, encoding, conf.writes, conf.len, conf.out);
fn(algo, message, encoding, writes, len, out);
}

function legacyWrite(algo, message, encoding, writes, len, outEnc) {
Expand Down
15 changes: 7 additions & 8 deletions benchmark/crypto/hash-stream-throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const bench = common.createBenchmark(main, {
api: ['legacy', 'stream']
});

function main(conf) {
var api = conf.api;
function main({ api, type, len, algo, writes }) {
if (api === 'stream' && /^v0\.[0-8]\./.test(process.version)) {
console.error('Crypto streams not available until v0.10');
// use the legacy, just so that we can compare them.
Expand All @@ -22,26 +21,26 @@ function main(conf) {

var message;
var encoding;
switch (conf.type) {
switch (type) {
case 'asc':
message = 'a'.repeat(conf.len);
message = 'a'.repeat(len);
encoding = 'ascii';
break;
case 'utf':
message = 'ü'.repeat(conf.len / 2);
message = 'ü'.repeat(len / 2);
encoding = 'utf8';
break;
case 'buf':
message = Buffer.alloc(conf.len, 'b');
message = Buffer.alloc(len, 'b');
break;
default:
throw new Error(`unknown message type: ${conf.type}`);
throw new Error(`unknown message type: ${type}`);
}

const fn = api === 'stream' ? streamWrite : legacyWrite;

bench.start();
fn(conf.algo, message, encoding, conf.writes, conf.len);
fn(algo, message, encoding, writes, len);
}

function legacyWrite(algo, message, encoding, writes, len) {
Expand Down
6 changes: 3 additions & 3 deletions benchmark/crypto/rsa-encrypt-decrypt-throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const bench = common.createBenchmark(main, {
len: [16, 32, 64]
});

function main(conf) {
const message = Buffer.alloc(conf.len, 'b');
function main({ len, algo, keylen, n }) {
const message = Buffer.alloc(len, 'b');
bench.start();
StreamWrite(conf.algo, conf.keylen, message, conf.n, conf.len);
StreamWrite(algo, keylen, message, n, len);
}

function StreamWrite(algo, keylen, message, n, len) {
Expand Down
6 changes: 3 additions & 3 deletions benchmark/crypto/rsa-sign-verify-throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ const bench = common.createBenchmark(main, {
len: [1024, 102400, 2 * 102400, 3 * 102400, 1024 * 1024]
});

function main(conf) {
const message = Buffer.alloc(conf.len, 'b');
function main({ len, algo, keylen, writes }) {
const message = Buffer.alloc(len, 'b');
bench.start();
StreamWrite(conf.algo, conf.keylen, message, conf.writes, conf.len);
StreamWrite(algo, keylen, message, writes, len);
}

function StreamWrite(algo, keylen, message, writes, len) {
Expand Down

0 comments on commit 0851822

Please sign in to comment.