11cb0ef41Sopenharmony_ci// Throughput benchmark
21cb0ef41Sopenharmony_ci// creates a single hasher, then pushes a bunch of data through it
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ciconst common = require('../common.js');
51cb0ef41Sopenharmony_ciconst crypto = require('crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
81cb0ef41Sopenharmony_ci  writes: [500],
91cb0ef41Sopenharmony_ci  algo: ['sha1', 'sha256', 'sha512'],
101cb0ef41Sopenharmony_ci  type: ['asc', 'utf', 'buf'],
111cb0ef41Sopenharmony_ci  len: [2, 1024, 102400, 1024 * 1024],
121cb0ef41Sopenharmony_ci  api: ['legacy', 'stream'],
131cb0ef41Sopenharmony_ci});
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifunction main({ api, type, len, algo, writes }) {
161cb0ef41Sopenharmony_ci  if (api === 'stream' && /^v0\.[0-8]\./.test(process.version)) {
171cb0ef41Sopenharmony_ci    console.error('Crypto streams not available until v0.10');
181cb0ef41Sopenharmony_ci    // Use the legacy, just so that we can compare them.
191cb0ef41Sopenharmony_ci    api = 'legacy';
201cb0ef41Sopenharmony_ci  }
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  let message;
231cb0ef41Sopenharmony_ci  let encoding;
241cb0ef41Sopenharmony_ci  switch (type) {
251cb0ef41Sopenharmony_ci    case 'asc':
261cb0ef41Sopenharmony_ci      message = 'a'.repeat(len);
271cb0ef41Sopenharmony_ci      encoding = 'ascii';
281cb0ef41Sopenharmony_ci      break;
291cb0ef41Sopenharmony_ci    case 'utf':
301cb0ef41Sopenharmony_ci      message = 'ü'.repeat(len / 2);
311cb0ef41Sopenharmony_ci      encoding = 'utf8';
321cb0ef41Sopenharmony_ci      break;
331cb0ef41Sopenharmony_ci    case 'buf':
341cb0ef41Sopenharmony_ci      message = Buffer.alloc(len, 'b');
351cb0ef41Sopenharmony_ci      break;
361cb0ef41Sopenharmony_ci    default:
371cb0ef41Sopenharmony_ci      throw new Error(`unknown message type: ${type}`);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  const fn = api === 'stream' ? streamWrite : legacyWrite;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  bench.start();
431cb0ef41Sopenharmony_ci  fn(algo, message, encoding, writes, len);
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cifunction legacyWrite(algo, message, encoding, writes, len) {
471cb0ef41Sopenharmony_ci  const written = writes * len;
481cb0ef41Sopenharmony_ci  const bits = written * 8;
491cb0ef41Sopenharmony_ci  const gbits = bits / (1024 * 1024 * 1024);
501cb0ef41Sopenharmony_ci  const h = crypto.createHash(algo);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  while (writes-- > 0)
531cb0ef41Sopenharmony_ci    h.update(message, encoding);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  h.digest();
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  bench.end(gbits);
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_cifunction streamWrite(algo, message, encoding, writes, len) {
611cb0ef41Sopenharmony_ci  const written = writes * len;
621cb0ef41Sopenharmony_ci  const bits = written * 8;
631cb0ef41Sopenharmony_ci  const gbits = bits / (1024 * 1024 * 1024);
641cb0ef41Sopenharmony_ci  const h = crypto.createHash(algo);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  while (writes-- > 0)
671cb0ef41Sopenharmony_ci    h.write(message, encoding);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  h.end();
701cb0ef41Sopenharmony_ci  h.read();
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  bench.end(gbits);
731cb0ef41Sopenharmony_ci}
74