Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
panva committed Mar 20, 2021
1 parent e82cbd1 commit 9655b0f
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions benchmark/crypto/oneshot-sign-verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const common = require('../common.js');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/');
const keys = {
publicKey: crypto.createPublicKey(
fs.readFileSync(`${fixtures_keydir}/ed25519_public.pem`)),
privateKey: crypto.createPrivateKey(
fs.readFileSync(`${fixtures_keydir}/ed25519_private.pem`)),
};

const data = crypto.randomBytes(256);
const args = [null, data];

const bench = common.createBenchmark(main, {
sync: [0, 1],
n: [1e4],
});

function measureSync(n) {
bench.start();
for (let i = 0; i < n; ++i) {
crypto.verify(...args, keys.publicKey,
crypto.sign(...args, keys.privateKey));
}
bench.end(n);
}

function measureAsync(n) {
let remaining = n;
function done() {
if (--remaining === 0)
bench.end(n);
}
bench.start();
for (let i = 0; i < n; ++i) {
crypto.sign(...args, keys.privateKey, (err, signature) => {
crypto.verify(...args, keys.publicKey, signature, done);
});
}
}

function main({ n, sync }) {
if (sync)
measureSync(n);
else
measureAsync(n);
}

0 comments on commit 9655b0f

Please sign in to comment.