Skip to content

Instantly share code, notes, and snippets.

@shuhei

shuhei/README.md

Created Jul 3, 2019
Embed
What would you like to do?
gzip chunkSize and readable/writableHighWatermark

Not much difference...

default: chunkSize 16KB, hwm: 16KB x 20.80 ops/sec ±1.65% (51 runs sampled)
chunkSize 128KB, hwm: 16KB x 22.86 ops/sec ±1.07% (55 runs sampled)
chunkSize 128KB, hwm: 128KB x 21.54 ops/sec ±8.46% (52 runs sampled)
chunkSize 16KB, hwm: 128KB x 21.48 ops/sec ±1.87% (52 runs sampled)
Fastest is [ 'chunkSize 128KB, hwm: 16KB', 'chunkSize 128KB, hwm: 128KB' ]
const { once } = require("events");
const { Suite } = require("benchmark");
const { HttpsAgent } = require('agentkeepalive');
const url = require("url");
const http = require("http");
const https = require("https");
const zlib = require("zlib");
const agent = new HttpsAgent();
async function makeRequest({
chunkSize,
hwm,
} = {}) {
const targetUrl = "https://jsonplaceholder.typicode.com/photos";
const req = https.request({
...url.parse(targetUrl),
agent,
headers: {
"Accept-Encoding": "gzip"
}
});
req.end();
const [res] = await once(req, "response");
res.writableHighwatermark = hwm;
res.readableHighWatermark = hwm;
const unzipped = res.pipe(
zlib.createGunzip({
chunkSize
})
);
let chunkCount = 0;
for await (const chunk of unzipped) {
chunkCount++;
}
// console.log("done", { chunkCount, chunkSize, hwm });
}
function createTest(options) {
return {
defer: true,
async fn(deferred) {
await makeRequest(options);
deferred.resolve();
}
};
}
(async () => {
// Warm up
for (let i = 0; i < 10; i++) {
await makeRequest();
}
const suite = new Suite();
suite
.add('default: chunkSize 16KB, hwm: 16KB', createTest())
.add('chunkSize 128KB, hwm: 16KB', createTest({
chunkSize: 128 * 1024,
hwm: 16 * 1024,
}))
.add('chunkSize 128KB, hwm: 128KB', createTest({
chunkSize: 128 * 1024,
hwm: 128 * 1024,
}))
.add('chunkSize 16KB, hwm: 128KB', createTest({
chunkSize: 16 * 1024,
hwm: 128 * 1024,
}))
.on('cycle', (event) => {
console.log(String(event.target));
})
.on('complete', () => {
console.log('Fastest is', suite.filter('fastest').map('name'));
})
.run();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.