xref: /third_party/node/benchmark/zlib/pipe.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ciconst fs = require('fs');
41cb0ef41Sopenharmony_ciconst zlib = require('zlib');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
71cb0ef41Sopenharmony_ci  inputLen: [1024],
81cb0ef41Sopenharmony_ci  duration: [5],
91cb0ef41Sopenharmony_ci  type: ['string', 'buffer'],
101cb0ef41Sopenharmony_ci  algorithm: ['gzip', 'brotli'],
111cb0ef41Sopenharmony_ci}, {
121cb0ef41Sopenharmony_ci  test: {
131cb0ef41Sopenharmony_ci    inputLen: 1024,
141cb0ef41Sopenharmony_ci    duration: 0.2,
151cb0ef41Sopenharmony_ci  },
161cb0ef41Sopenharmony_ci});
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifunction main({ inputLen, duration, type, algorithm }) {
191cb0ef41Sopenharmony_ci  const buffer = Buffer.alloc(inputLen, fs.readFileSync(__filename));
201cb0ef41Sopenharmony_ci  const chunk = type === 'buffer' ? buffer : buffer.toString('utf8');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const input = algorithm === 'gzip' ?
231cb0ef41Sopenharmony_ci    zlib.createGzip() : zlib.createBrotliCompress();
241cb0ef41Sopenharmony_ci  const output = algorithm === 'gzip' ?
251cb0ef41Sopenharmony_ci    zlib.createGunzip() : zlib.createBrotliDecompress();
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  let readFromOutput = 0;
281cb0ef41Sopenharmony_ci  input.pipe(output);
291cb0ef41Sopenharmony_ci  if (type === 'string')
301cb0ef41Sopenharmony_ci    output.setEncoding('utf8');
311cb0ef41Sopenharmony_ci  output.on('data', (chunk) => readFromOutput += chunk.length);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  function write() {
341cb0ef41Sopenharmony_ci    input.write(chunk, write);
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  bench.start();
381cb0ef41Sopenharmony_ci  write();
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  setTimeout(() => {
411cb0ef41Sopenharmony_ci    // Give result in GBit/s, like the net benchmarks do
421cb0ef41Sopenharmony_ci    bench.end(readFromOutput * 8 / (1024 ** 3));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci    // Cut off writing the easy way.
451cb0ef41Sopenharmony_ci    input.write = () => {};
461cb0ef41Sopenharmony_ci  }, duration * 1000);
471cb0ef41Sopenharmony_ci}
48