libdistbot/benchmark/liquid_rescale.js

43 lines
1.4 KiB
JavaScript

const path = require('node:path');
const fs = require('node:fs');
const concolor = require('concolor');
const gm = require('gm').subClass({ imageMagick: true });
const { liquidRescale } = require('../dist/index.js');
const { Suite } = require('benchmark');
const image = fs.readFileSync(path.join(path.resolve(), 'benchmark', 'bear.png'));
const suite = new Suite("Liquid rescale and resize image");
function liquidRescaleWithGm() {
return new Promise((resolve, reject) => {
gm(image).size((err, size) => {
if (err) reject(err);
const width = size.width;
const height = size.height;
gm(image).out('-liquid-rescale', "400x400")
.toBuffer('JPEG', (err, buf) => {
if (err) reject(err);
gm(buf).resize(width, height)
.toBuffer('JPEG', (err, buf) => {
err ? reject(err) : resolve(buf);
});
});
});
});
}
suite
.add('gm', async () => {
await liquidRescaleWithGm();
})
.add('native', async () => {
await liquidRescale(image, 400, 400);
})
.on('cycle', (event) => console.info(String(event.target)))
.on('complete', function () {
console.info(`${this.name} bench suite: Fastest is ${concolor`${this.filter('fastest').map('name')}(green)`}`);
})
.run({ async: true });