1'use strict';
2
3const common = require('../common.js');
4const assert = require('assert');
5const {
6  hkdf,
7  hkdfSync,
8} = require('crypto');
9
10const bench = common.createBenchmark(main, {
11  sync: [0, 1],
12  size: [10, 64, 1024],
13  key: ['a', 'secret', 'this-is-a-much-longer-secret'],
14  salt: ['', 'salt'],
15  info: ['', 'info'],
16  hash: ['sha256', 'sha512'],
17  n: [1e3],
18});
19
20function measureSync(n, size, salt, info, hash, key) {
21  bench.start();
22  for (let i = 0; i < n; ++i)
23    hkdfSync(hash, key, salt, info, size);
24  bench.end(n);
25}
26
27function measureAsync(n, size, salt, info, hash, key) {
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    hkdf(hash, key, salt, info, size, done);
37}
38
39function main({ n, sync, size, salt, info, hash, key }) {
40  if (sync)
41    measureSync(n, size, salt, info, hash, key);
42  else
43    measureAsync(n, size, salt, info, hash, key);
44}
45