|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | +const crypto = require('crypto'); |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | +const fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/'); |
| 8 | +const keys = { |
| 9 | + publicKey: crypto.createPublicKey( |
| 10 | + fs.readFileSync(`${fixtures_keydir}/ed25519_public.pem`)), |
| 11 | + privateKey: crypto.createPrivateKey( |
| 12 | + fs.readFileSync(`${fixtures_keydir}/ed25519_private.pem`)), |
| 13 | +}; |
| 14 | + |
| 15 | +const data = crypto.randomBytes(256); |
| 16 | +const args = [null, data]; |
| 17 | + |
| 18 | +const bench = common.createBenchmark(main, { |
| 19 | + sync: [0, 1], |
| 20 | + n: [1e4], |
| 21 | +}); |
| 22 | + |
| 23 | +function measureSync(n) { |
| 24 | + bench.start(); |
| 25 | + for (let i = 0; i < n; ++i) { |
| 26 | + crypto.verify(...args, keys.publicKey, |
| 27 | + crypto.sign(...args, keys.privateKey)); |
| 28 | + } |
| 29 | + bench.end(n); |
| 30 | +} |
| 31 | + |
| 32 | +function measureAsync(n) { |
| 33 | + let remaining = n; |
| 34 | + function done() { |
| 35 | + if (--remaining === 0) |
| 36 | + bench.end(n); |
| 37 | + } |
| 38 | + bench.start(); |
| 39 | + for (let i = 0; i < n; ++i) { |
| 40 | + crypto.sign(...args, keys.privateKey, (err, signature) => { |
| 41 | + crypto.verify(...args, keys.publicKey, signature, done); |
| 42 | + }); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function main({ n, sync }) { |
| 47 | + if (sync) |
| 48 | + measureSync(n); |
| 49 | + else |
| 50 | + measureAsync(n); |
| 51 | +} |
0 commit comments