1'use strict'; 2 3const common = require('../common.js'); 4const assert = require('assert'); 5const { 6 generateKeyPair, 7 generateKeyPairSync, 8} = require('crypto'); 9 10const bench = common.createBenchmark(main, { 11 method: ['rsaSync', 'rsaAsync', 'dsaSync', 'dsaAsync'], 12 n: [1e2], 13}); 14 15const methods = { 16 rsaSync(n) { 17 bench.start(); 18 for (let i = 0; i < n; ++i) { 19 generateKeyPairSync('rsa', { 20 modulusLength: 1024, 21 publicExponent: 0x10001, 22 }); 23 } 24 bench.end(n); 25 }, 26 27 rsaAsync(n) { 28 let remaining = n; 29 function done(err) { 30 assert.ifError(err); 31 if (--remaining === 0) 32 bench.end(n); 33 } 34 bench.start(); 35 for (let i = 0; i < n; ++i) 36 generateKeyPair('rsa', { 37 modulusLength: 512, 38 publicExponent: 0x10001, 39 }, done); 40 }, 41 42 dsaSync(n) { 43 bench.start(); 44 for (let i = 0; i < n; ++i) { 45 generateKeyPairSync('dsa', { 46 modulusLength: 1024, 47 divisorLength: 160, 48 }); 49 } 50 bench.end(n); 51 }, 52 53 dsaAsync(n) { 54 let remaining = n; 55 function done(err) { 56 assert.ifError(err); 57 if (--remaining === 0) 58 bench.end(n); 59 } 60 bench.start(); 61 for (let i = 0; i < n; ++i) 62 generateKeyPair('dsa', { 63 modulusLength: 1024, 64 divisorLength: 160, 65 }, done); 66 }, 67}; 68 69function main({ n, method }) { 70 methods[method](n); 71} 72