1'use strict'; 2const common = require('../common.js'); 3const fs = require('fs'); 4const zlib = require('zlib'); 5 6const bench = common.createBenchmark(main, { 7 inputLen: [1024], 8 duration: [5], 9 type: ['string', 'buffer'], 10 algorithm: ['gzip', 'brotli'] 11}, { 12 test: { 13 inputLen: 1024, 14 duration: 0.2 15 } 16}); 17 18function main({ inputLen, duration, type, algorithm }) { 19 const buffer = Buffer.alloc(inputLen, fs.readFileSync(__filename)); 20 const chunk = type === 'buffer' ? buffer : buffer.toString('utf8'); 21 22 const input = algorithm === 'gzip' ? 23 zlib.createGzip() : zlib.createBrotliCompress(); 24 const output = algorithm === 'gzip' ? 25 zlib.createGunzip() : zlib.createBrotliDecompress(); 26 27 let readFromOutput = 0; 28 input.pipe(output); 29 if (type === 'string') 30 output.setEncoding('utf8'); 31 output.on('data', (chunk) => readFromOutput += chunk.length); 32 33 function write() { 34 input.write(chunk, write); 35 } 36 37 bench.start(); 38 write(); 39 40 setTimeout(() => { 41 // Give result in GBit/s, like the net benchmarks do 42 bench.end(readFromOutput * 8 / (1024 ** 3)); 43 44 // Cut off writing the easy way. 45 input.write = () => {}; 46 }, duration * 1000); 47} 48