Skip to content

Commit c670358

Browse files
addaleaxMylesBorins
authored andcommitted
benchmark,test: add brotli
Co-authored-by: Hackzzila <[email protected]> Backport-PR-URL: #27681 PR-URL: #24938 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent 53e9c85 commit c670358

16 files changed

+267
-49
lines changed

benchmark/zlib/creation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const zlib = require('zlib');
44

55
const bench = common.createBenchmark(main, {
66
type: [
7-
'Deflate', 'DeflateRaw', 'Inflate', 'InflateRaw', 'Gzip', 'Gunzip', 'Unzip'
7+
'Deflate', 'DeflateRaw', 'Inflate', 'InflateRaw', 'Gzip', 'Gunzip', 'Unzip',
8+
'BrotliCompress', 'BrotliDecompress'
89
],
910
options: ['true', 'false'],
1011
n: [5e5]

benchmark/zlib/pipe.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ const zlib = require('zlib');
66
const bench = common.createBenchmark(main, {
77
inputLen: [1024],
88
duration: [5],
9-
type: ['string', 'buffer']
9+
type: ['string', 'buffer'],
10+
algorithm: ['gzip', 'brotli']
1011
});
1112

12-
function main({ inputLen, duration, type }) {
13+
function main({ inputLen, duration, type, algorithm }) {
1314
const buffer = Buffer.alloc(inputLen, fs.readFileSync(__filename));
1415
const chunk = type === 'buffer' ? buffer : buffer.toString('utf8');
1516

16-
const input = zlib.createGzip();
17-
const output = zlib.createGunzip();
17+
const input = algorithm === 'gzip' ?
18+
zlib.createGzip() : zlib.createBrotliCompress();
19+
const output = algorithm === 'gzip' ?
20+
zlib.createGunzip() : zlib.createBrotliDecompress();
1821

1922
let readFromOutput = 0;
2023
input.pipe(output);

test/fixtures/person.jpg.br

44.1 KB
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const zlib = require('zlib');
5+
const fixtures = require('../common/fixtures');
6+
7+
const file = fixtures.readSync('person.jpg');
8+
const chunkSize = 16;
9+
const deflater = new zlib.BrotliCompress();
10+
11+
const chunk = file.slice(0, chunkSize);
12+
const expectedFull = Buffer.from('iweA/9j/4AAQSkZJRgABAQEASA==', 'base64');
13+
let actualFull;
14+
15+
deflater.write(chunk, function() {
16+
deflater.flush(function() {
17+
const bufs = [];
18+
let buf;
19+
while (buf = deflater.read())
20+
bufs.push(buf);
21+
actualFull = Buffer.concat(bufs);
22+
});
23+
});
24+
25+
process.once('exit', function() {
26+
assert.deepStrictEqual(actualFull, expectedFull);
27+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
// Test unzipping a file that was created with a non-node brotli lib,
3+
// piped in as fast as possible.
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const zlib = require('zlib');
8+
const path = require('path');
9+
const fixtures = require('../common/fixtures');
10+
11+
const tmpdir = require('../common/tmpdir');
12+
tmpdir.refresh();
13+
14+
const decompress = new zlib.BrotliDecompress();
15+
16+
const fs = require('fs');
17+
18+
const fixture = fixtures.path('person.jpg.br');
19+
const unzippedFixture = fixtures.path('person.jpg');
20+
const outputFile = path.resolve(tmpdir.path, 'person.jpg');
21+
const expect = fs.readFileSync(unzippedFixture);
22+
const inp = fs.createReadStream(fixture);
23+
const out = fs.createWriteStream(outputFile);
24+
25+
inp.pipe(decompress).pipe(out);
26+
out.on('close', common.mustCall(() => {
27+
const actual = fs.readFileSync(outputFile);
28+
assert.strictEqual(actual.length, expect.length);
29+
for (let i = 0, l = actual.length; i < l; i++) {
30+
assert.strictEqual(actual[i], expect[i], `byte[${i}]`);
31+
}
32+
}));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
// Test compressing and uncompressing a string with brotli
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const zlib = require('zlib');
7+
8+
const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' +
9+
't. Morbi faucibus, purus at gravida dictum, libero arcu ' +
10+
'convallis lacus, in commodo libero metus eu nisi. Nullam' +
11+
' commodo, neque nec porta placerat, nisi est fermentum a' +
12+
'ugue, vitae gravida tellus sapien sit amet tellus. Aenea' +
13+
'n non diam orci. Proin quis elit turpis. Suspendisse non' +
14+
' diam ipsum. Suspendisse nec ullamcorper odio. Vestibulu' +
15+
'm arcu mi, sodales non suscipit id, ultrices ut massa. S' +
16+
'ed ac sem sit amet arcu malesuada fermentum. Nunc sed. ';
17+
const expectedBase64Compress = 'G/gBQBwHdky2aHV5KK9Snf05//1pPdmNw/7232fnIm1IB' +
18+
'K1AA8RsN8OB8Nb7Lpgk3UWWUlzQXZyHQeBBbXMTQXC1j7' +
19+
'wg3LJs9LqOGHRH2bj/a2iCTLLx8hBOyTqgoVuD1e+Qqdn' +
20+
'f1rkUNyrWq6LtOhWgxP3QUwdhKGdZm3rJWaDDBV7+pDk1' +
21+
'MIkrmjp4ma2xVi5MsgJScA3tP1I7mXeby6MELozrwoBQD' +
22+
'mVTnEAicZNj4lkGqntJe2qSnGyeMmcFgraK94vCg/4iLu' +
23+
'Tw5RhKhnVY++dZ6niUBmRqIutsjf5TzwF5iAg8a9UkjF5' +
24+
'2eZ0tB2vo6v8SqVfNMkBmmhxr0NT9LkYF69aEjlYzj7IE' +
25+
'KmEUQf1HBogRYhFIt4ymRNEgHAIzOyNEsQM=';
26+
27+
zlib.brotliCompress(inputString, common.mustCall((err, buffer) => {
28+
assert.strictEqual(buffer.toString('base64'), expectedBase64Compress);
29+
}));
30+
31+
const buffer = Buffer.from(expectedBase64Compress, 'base64');
32+
zlib.brotliDecompress(buffer, common.mustCall((err, buffer) => {
33+
assert.strictEqual(buffer.toString(), inputString);
34+
}));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This test ensures that zlib throws a RangeError if the final buffer needs to
5+
// be larger than kMaxLength and concatenation fails.
6+
// https://github.com/nodejs/node/pull/1811
7+
8+
const assert = require('assert');
9+
10+
// Change kMaxLength for zlib to trigger the error without having to allocate
11+
// large Buffers.
12+
const buffer = require('buffer');
13+
const oldkMaxLength = buffer.kMaxLength;
14+
buffer.kMaxLength = 128;
15+
const zlib = require('zlib');
16+
buffer.kMaxLength = oldkMaxLength;
17+
18+
const encoded = Buffer.from('G38A+CXCIrFAIAM=', 'base64');
19+
20+
// Async
21+
zlib.brotliDecompress(encoded, function(err) {
22+
assert.ok(err instanceof RangeError);
23+
});
24+
25+
// Sync
26+
assert.throws(function() {
27+
zlib.brotliDecompressSync(encoded);
28+
}, RangeError);

test/parallel/test-zlib-brotli.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
const assert = require('assert');
5+
const zlib = require('zlib');
6+
7+
// Test some brotli-specific properties of the brotli streams that can not
8+
// be easily covered through expanding zlib-only tests.
9+
10+
const sampleBuffer = fixtures.readSync('/pss-vectors.json');
11+
12+
{
13+
// Test setting the quality parameter at stream creation:
14+
const sizes = [];
15+
for (let quality = zlib.constants.BROTLI_MIN_QUALITY;
16+
quality <= zlib.constants.BROTLI_MAX_QUALITY;
17+
quality++) {
18+
const encoded = zlib.brotliCompressSync(sampleBuffer, {
19+
params: {
20+
[zlib.constants.BROTLI_PARAM_QUALITY]: quality
21+
}
22+
});
23+
sizes.push(encoded.length);
24+
}
25+
26+
// Increasing quality should roughly correspond to decreasing compressed size:
27+
for (let i = 0; i < sizes.length - 1; i++) {
28+
assert(sizes[i + 1] <= sizes[i] * 1.05, sizes); // 5 % margin of error.
29+
}
30+
assert(sizes[0] > sizes[sizes.length - 1], sizes);
31+
}
32+
33+
{
34+
// Test that setting out-of-bounds option values or keys fails.
35+
common.expectsError(() => {
36+
zlib.createBrotliCompress({
37+
params: {
38+
10000: 0
39+
}
40+
});
41+
}, {
42+
code: 'ERR_BROTLI_INVALID_PARAM',
43+
type: RangeError,
44+
message: '10000 is not a valid Brotli parameter'
45+
});
46+
47+
// Test that accidentally using duplicate keys fails.
48+
common.expectsError(() => {
49+
zlib.createBrotliCompress({
50+
params: {
51+
'0': 0,
52+
'00': 0
53+
}
54+
});
55+
}, {
56+
code: 'ERR_BROTLI_INVALID_PARAM',
57+
type: RangeError,
58+
message: '00 is not a valid Brotli parameter'
59+
});
60+
61+
common.expectsError(() => {
62+
zlib.createBrotliCompress({
63+
params: {
64+
// This is a boolean flag
65+
[zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING]: 42
66+
}
67+
});
68+
}, {
69+
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
70+
type: Error,
71+
message: 'Initialization failed'
72+
});
73+
}

test/parallel/test-zlib-bytes-read.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ for (const method of [
2525
['createGzip', 'createGunzip', false],
2626
['createGzip', 'createUnzip', false],
2727
['createDeflate', 'createInflate', true],
28-
['createDeflateRaw', 'createInflateRaw', true]
28+
['createDeflateRaw', 'createInflateRaw', true],
29+
['createBrotliCompress', 'createBrotliDecompress', true]
2930
]) {
3031
let compWriter;
3132
let compData = Buffer.alloc(0);

test/parallel/test-zlib-convenience-methods.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ for (const [type, expect] of [
5252
['gzip', 'unzip', 'Gzip', 'Unzip'],
5353
['deflate', 'inflate', 'Deflate', 'Inflate'],
5454
['deflateRaw', 'inflateRaw', 'DeflateRaw', 'InflateRaw'],
55+
['brotliCompress', 'brotliDecompress',
56+
'BrotliCompress', 'BrotliDecompress'],
5557
]) {
5658
zlib[method[0]](expect, opts, common.mustCall((err, result) => {
5759
zlib[method[1]](result, opts, common.mustCall((err, result) => {

0 commit comments

Comments
 (0)