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: [ 'sha256', 'md5' ],
101cb0ef41Sopenharmony_ci  type: ['asc', 'utf', 'buf'],
111cb0ef41Sopenharmony_ci  out: ['hex', 'binary', 'buffer'],
121cb0ef41Sopenharmony_ci  len: [2, 1024, 102400, 1024 * 1024],
131cb0ef41Sopenharmony_ci  api: ['legacy', 'stream'],
141cb0ef41Sopenharmony_ci});
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cifunction main({ api, type, len, out, writes, algo }) {
171cb0ef41Sopenharmony_ci  if (api === 'stream' && /^v0\.[0-8]\./.test(process.version)) {
181cb0ef41Sopenharmony_ci    console.error('Crypto streams not available until v0.10');
191cb0ef41Sopenharmony_ci    // Use the legacy, just so that we can compare them.
201cb0ef41Sopenharmony_ci    api = 'legacy';
211cb0ef41Sopenharmony_ci  }
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  let message;
241cb0ef41Sopenharmony_ci  let encoding;
251cb0ef41Sopenharmony_ci  switch (type) {
261cb0ef41Sopenharmony_ci    case 'asc':
271cb0ef41Sopenharmony_ci      message = 'a'.repeat(len);
281cb0ef41Sopenharmony_ci      encoding = 'ascii';
291cb0ef41Sopenharmony_ci      break;
301cb0ef41Sopenharmony_ci    case 'utf':
311cb0ef41Sopenharmony_ci      message = 'ü'.repeat(len / 2);
321cb0ef41Sopenharmony_ci      encoding = 'utf8';
331cb0ef41Sopenharmony_ci      break;
341cb0ef41Sopenharmony_ci    case 'buf':
351cb0ef41Sopenharmony_ci      message = Buffer.alloc(len, 'b');
361cb0ef41Sopenharmony_ci      break;
371cb0ef41Sopenharmony_ci    default:
381cb0ef41Sopenharmony_ci      throw new Error(`unknown message type: ${type}`);
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const fn = api === 'stream' ? streamWrite : legacyWrite;
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  bench.start();
441cb0ef41Sopenharmony_ci  fn(algo, message, encoding, writes, len, out);
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cifunction legacyWrite(algo, message, encoding, writes, len, outEnc) {
481cb0ef41Sopenharmony_ci  const written = writes * len;
491cb0ef41Sopenharmony_ci  const bits = written * 8;
501cb0ef41Sopenharmony_ci  const gbits = bits / (1024 * 1024 * 1024);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  while (writes-- > 0) {
531cb0ef41Sopenharmony_ci    const h = crypto.createHash(algo);
541cb0ef41Sopenharmony_ci    h.update(message, encoding);
551cb0ef41Sopenharmony_ci    h.digest(outEnc);
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  bench.end(gbits);
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cifunction streamWrite(algo, message, encoding, writes, len, outEnc) {
621cb0ef41Sopenharmony_ci  const written = writes * len;
631cb0ef41Sopenharmony_ci  const bits = written * 8;
641cb0ef41Sopenharmony_ci  const gbits = bits / (1024 * 1024 * 1024);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  while (writes-- > 0) {
671cb0ef41Sopenharmony_ci    const h = crypto.createHash(algo);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    if (outEnc !== 'buffer')
701cb0ef41Sopenharmony_ci      h.setEncoding(outEnc);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    h.write(message, encoding);
731cb0ef41Sopenharmony_ci    h.end();
741cb0ef41Sopenharmony_ci    h.read();
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  bench.end(gbits);
781cb0ef41Sopenharmony_ci}
79