Skip to content

Commit b0e6f10

Browse files
addaleaxMylesBorins
authored andcommitted
benchmark: add bench for zlib gzip + gunzip cycle
Originally wrote this for some work that is going to take a while longer before it’s ready to be PR’ed, so it seems fine to start with this on its own. PR-URL: #20034 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 31812ed commit b0e6f10

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎benchmark/zlib/pipe.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
constfs=require('fs');
4+
constzlib=require('zlib');
5+
6+
constbench=common.createBenchmark(main,{
7+
inputLen: [1024],
8+
duration: [5],
9+
type: ['string','buffer']
10+
});
11+
12+
functionmain({ inputLen, duration, type }){
13+
constbuffer=Buffer.alloc(inputLen,fs.readFileSync(__filename));
14+
constchunk=type==='buffer' ? buffer : buffer.toString('utf8');
15+
16+
constinput=zlib.createGzip();
17+
constoutput=zlib.createGunzip();
18+
19+
letreadFromOutput=0;
20+
input.pipe(output);
21+
if(type==='string')
22+
output.setEncoding('utf8');
23+
output.on('data',(chunk)=>readFromOutput+=chunk.length);
24+
25+
functionwrite(){
26+
input.write(chunk,write);
27+
}
28+
29+
bench.start();
30+
write();
31+
32+
setTimeout(()=>{
33+
// Give result in GBit/s, like the net benchmarks do
34+
bench.end(readFromOutput*8/(1024**3));
35+
36+
// Cut off writing the easy way.
37+
input.write=()=>{};
38+
},duration*1000);
39+
}

‎test/parallel/test-benchmark-zlib.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@ runBenchmark('zlib',
99
'method=deflate',
1010
'n=1',
1111
'options=true',
12-
'type=Deflate'
13-
]);
12+
'type=Deflate',
13+
'inputLen=1024',
14+
'duration=0.001'
15+
],
16+
{
17+
'NODEJS_BENCHMARK_ZERO_ALLOWED': 1
18+
});

0 commit comments

Comments
 (0)