Skip to content

Commit

Permalink
Benchmarks: ensure PNG tests use consistent settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Aug 21, 2020
1 parent eaecb73 commit 3917efd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
6 changes: 3 additions & 3 deletions test/bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"benchmark": "^2.1.4",
"gm": "^1.23.1",
"imagemagick": "^0.1.3",
"jimp": "^0.9.3",
"mapnik": "^4.3.1",
"jimp": "^0.16.0",
"mapnik": "^4.5.2",
"semver": "^7.1.1"
},
"license": "Apache-2.0",
"engines": {
"node": ">=8.5.0"
"node": ">=10.16.0"
}
}
61 changes: 50 additions & 11 deletions test/bench/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,9 @@ async.series({
},
// PNG
png: function (callback) {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
const inputPngBuffer = fs.readFileSync(fixtures.inputPngAlphaPremultiplicationLarge);
const pngSuite = new Benchmark.Suite('png');
const minSamples = 64;
// jimp
pngSuite.add('jimp-buffer-buffer', {
defer: true,
Expand All @@ -576,6 +577,8 @@ async.series({
} else {
image
.resize(width, height)
.deflateLevel(6)
.filterType(0)

This comment has been minimized.

Copy link
@kleisauke

kleisauke Aug 24, 2020

Contributor

Jimp uses Z_RLE as default zlib compression strategy, see:
pngjs/pngjs@303e231
https://github.com/oliver-moran/jimp/blob/8cd6eb00c6fd34ac802cf85da57313b126deea21/packages/type-png/src/index.js#L57

Should we set it to Z_FILTERED (i.e. .deflateStrategy(1)) since this is the default strategy for libpng?:
https://github.com/glennrp/libpng/blob/3cec1a16f5ec9ad925ce71785a2b429b02de5fec/scripts/pnglibconf.dfa#L274-L276

We could also experiment with switching from Z_FILTERED to Z_RLE in libpng. For example:

sed -i'.bak' 's/Z_FILTERED/Z_RLE/g' scripts/pnglibconf.dfa

(untested)

This comment has been minimized.

Copy link
@lovell

lovell Aug 24, 2020

Author Owner

Oh, good spot, perhaps we should hardcode png_set_compression_strategy( write->pPng, 3 ) in libvips? Some quick testing here suggests it's at least 10% faster, especially at higher compression levels, with no increase in file size.

.getBuffer(jimp.MIME_PNG, function (err) {
if (err) {
throw err;
Expand All @@ -589,12 +592,14 @@ async.series({
}).add('jimp-file-file', {
defer: true,
fn: function (deferred) {
jimp.read(fixtures.inputPng, function (err, image) {
jimp.read(fixtures.inputPngAlphaPremultiplicationLarge, function (err, image) {
if (err) {
throw err;
} else {
image
.resize(width, height)
.deflateLevel(6)
.filterType(0)
.write(fixtures.outputPng, function (err) {
if (err) {
throw err;
Expand All @@ -610,7 +615,7 @@ async.series({
pngSuite.add('mapnik-file-file', {
defer: true,
fn: function (deferred) {
mapnik.Image.open(fixtures.inputPng, function (err, img) {
mapnik.Image.open(fixtures.inputPngAlphaPremultiplicationLarge, function (err, img) {
if (err) throw err;
img.premultiply(function (err, img) {
if (err) throw err;
Expand Down Expand Up @@ -657,11 +662,15 @@ async.series({
defer: true,
fn: function (deferred) {
imagemagick.resize({
srcPath: fixtures.inputPng,
srcPath: fixtures.inputPngAlphaPremultiplicationLarge,
dstPath: fixtures.outputPng,
width: width,
height: height,
filter: 'Lanczos'
filter: 'Lanczos',
customArgs: [
'-define', 'PNG:compression-level=6',
'-define', 'PNG:compression-filter=0'
]
}, function (err) {
if (err) {
throw err;
Expand All @@ -675,9 +684,11 @@ async.series({
pngSuite.add('gm-file-file', {
defer: true,
fn: function (deferred) {
gm(fixtures.inputPng)
gm(fixtures.inputPngAlphaPremultiplicationLarge)
.filter('Lanczos')
.resize(width, height)
.define('PNG:compression-level=6')
.define('PNG:compression-filter=0')
.write(fixtures.outputPng, function (err) {
if (err) {
throw err;
Expand All @@ -689,9 +700,11 @@ async.series({
}).add('gm-file-buffer', {
defer: true,
fn: function (deferred) {
gm(fixtures.inputPng)
gm(fixtures.inputPngAlphaPremultiplicationLarge)
.filter('Lanczos')
.resize(width, height)
.define('PNG:compression-level=6')
.define('PNG:compression-filter=0')
.toBuffer(function (err, buffer) {
if (err) {
throw err;
Expand All @@ -705,9 +718,11 @@ async.series({
// sharp
pngSuite.add('sharp-buffer-file', {
defer: true,
minSamples,
fn: function (deferred) {
sharp(inputPngBuffer)
.resize(width, height)
.png({ compressionLevel: 6 })
.toFile(fixtures.outputPng, function (err) {
if (err) {
throw err;
Expand All @@ -718,9 +733,11 @@ async.series({
}
}).add('sharp-buffer-buffer', {
defer: true,
minSamples,
fn: function (deferred) {
sharp(inputPngBuffer)
.resize(width, height)
.png({ compressionLevel: 6 })
.toBuffer(function (err, buffer) {
if (err) {
throw err;
Expand All @@ -732,9 +749,11 @@ async.series({
}
}).add('sharp-file-file', {
defer: true,
minSamples,
fn: function (deferred) {
sharp(fixtures.inputPng)
sharp(fixtures.inputPngAlphaPremultiplicationLarge)
.resize(width, height)
.png({ compressionLevel: 6 })
.toFile(fixtures.outputPng, function (err) {
if (err) {
throw err;
Expand All @@ -745,9 +764,11 @@ async.series({
}
}).add('sharp-file-buffer', {
defer: true,
minSamples,
fn: function (deferred) {
sharp(fixtures.inputPng)
sharp(fixtures.inputPngAlphaPremultiplicationLarge)
.resize(width, height)
.png({ compressionLevel: 6 })
.toBuffer(function (err, buffer) {
if (err) {
throw err;
Expand All @@ -759,10 +780,11 @@ async.series({
}
}).add('sharp-progressive', {
defer: true,
minSamples,
fn: function (deferred) {
sharp(inputPngBuffer)
.resize(width, height)
.png({ progressive: true })
.png({ compressionLevel: 6, progressive: true })
.toBuffer(function (err, buffer) {
if (err) {
throw err;
Expand All @@ -774,10 +796,27 @@ async.series({
}
}).add('sharp-adaptiveFiltering', {
defer: true,
minSamples,
fn: function (deferred) {
sharp(inputPngBuffer)
.resize(width, height)
.png({ adaptiveFiltering: true, compressionLevel: 6 })
.toBuffer(function (err, buffer) {
if (err) {
throw err;
} else {
assert.notStrictEqual(null, buffer);
deferred.resolve();
}
});
}
}).add('sharp-compressionLevel=9', {
defer: true,
minSamples,
fn: function (deferred) {
sharp(inputPngBuffer)
.resize(width, height)
.png({ adaptiveFiltering: true })
.png({ compressionLevel: 9 })
.toBuffer(function (err, buffer) {
if (err) {
throw err;
Expand Down

0 comments on commit 3917efd

Please sign in to comment.