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 <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 31812ed commit b0e6f10

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

benchmark/zlib/pipe.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const fs = require('fs');
4+
const zlib = require('zlib');
5+
6+
const bench = common.createBenchmark(main, {
7+
inputLen: [1024],
8+
duration: [5],
9+
type: ['string', 'buffer']
10+
});
11+
12+
function main({ inputLen, duration, type }) {
13+
const buffer = Buffer.alloc(inputLen, fs.readFileSync(__filename));
14+
const chunk = type === 'buffer' ? buffer : buffer.toString('utf8');
15+
16+
const input = zlib.createGzip();
17+
const output = zlib.createGunzip();
18+
19+
let readFromOutput = 0;
20+
input.pipe(output);
21+
if (type === 'string')
22+
output.setEncoding('utf8');
23+
output.on('data', (chunk) => readFromOutput += chunk.length);
24+
25+
function write() {
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

+7-2
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)