11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ciconst zlib = require('zlib');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
61cb0ef41Sopenharmony_ci  method: ['createDeflate', 'deflate', 'deflateSync'],
71cb0ef41Sopenharmony_ci  inputLen: [1024],
81cb0ef41Sopenharmony_ci  n: [4e5],
91cb0ef41Sopenharmony_ci});
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction main({ n, method, inputLen }) {
121cb0ef41Sopenharmony_ci  // Default method value for testing.
131cb0ef41Sopenharmony_ci  method = method || 'deflate';
141cb0ef41Sopenharmony_ci  const chunk = Buffer.alloc(inputLen, 'a');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  switch (method) {
171cb0ef41Sopenharmony_ci    // Performs `n` writes for a single deflate stream
181cb0ef41Sopenharmony_ci    case 'createDeflate': {
191cb0ef41Sopenharmony_ci      let i = 0;
201cb0ef41Sopenharmony_ci      const deflater = zlib.createDeflate();
211cb0ef41Sopenharmony_ci      deflater.resume();
221cb0ef41Sopenharmony_ci      deflater.on('finish', () => {
231cb0ef41Sopenharmony_ci        bench.end(n);
241cb0ef41Sopenharmony_ci      });
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci      bench.start();
271cb0ef41Sopenharmony_ci      (function next() {
281cb0ef41Sopenharmony_ci        if (i++ === n)
291cb0ef41Sopenharmony_ci          return deflater.end();
301cb0ef41Sopenharmony_ci        deflater.write(chunk, next);
311cb0ef41Sopenharmony_ci      })();
321cb0ef41Sopenharmony_ci      break;
331cb0ef41Sopenharmony_ci    }
341cb0ef41Sopenharmony_ci    // Performs `n` single deflate operations
351cb0ef41Sopenharmony_ci    case 'deflate': {
361cb0ef41Sopenharmony_ci      let i = 0;
371cb0ef41Sopenharmony_ci      const deflate = zlib.deflate;
381cb0ef41Sopenharmony_ci      bench.start();
391cb0ef41Sopenharmony_ci      (function next(err, result) {
401cb0ef41Sopenharmony_ci        if (i++ === n)
411cb0ef41Sopenharmony_ci          return bench.end(n);
421cb0ef41Sopenharmony_ci        deflate(chunk, next);
431cb0ef41Sopenharmony_ci      })();
441cb0ef41Sopenharmony_ci      break;
451cb0ef41Sopenharmony_ci    }
461cb0ef41Sopenharmony_ci    // Performs `n` single deflateSync operations
471cb0ef41Sopenharmony_ci    case 'deflateSync': {
481cb0ef41Sopenharmony_ci      const deflateSync = zlib.deflateSync;
491cb0ef41Sopenharmony_ci      bench.start();
501cb0ef41Sopenharmony_ci      for (let i = 0; i < n; ++i)
511cb0ef41Sopenharmony_ci        deflateSync(chunk);
521cb0ef41Sopenharmony_ci      bench.end(n);
531cb0ef41Sopenharmony_ci      break;
541cb0ef41Sopenharmony_ci    }
551cb0ef41Sopenharmony_ci    default:
561cb0ef41Sopenharmony_ci      throw new Error('Unsupported deflate method');
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci}
59