Add encoding/decoding times to console

This intentionally excludes time of loading corresponding modules, and only measures actual processing.

While this is not perfect as it's not integrated in the UI (cc @jakearchibald), it gives at least some way to measure performance of different codecs and their integrations on particular files.
This commit is contained in:
Ingvar Stepanyan
2020-04-09 13:24:58 +01:00
committed by Ingvar Stepanyan
parent 164191d746
commit 4621cbcae9

View File

@@ -2,13 +2,18 @@ import { expose } from 'comlink';
import { isHqx } from '../resize/processor-meta'; import { isHqx } from '../resize/processor-meta';
import { clamp } from '../util'; import { clamp } from '../util';
function timed<T>(name: string, func: () => Promise<T>) {
console.time(name);
return func().finally(() => console.timeEnd(name));
}
async function mozjpegEncode( async function mozjpegEncode(
data: ImageData, options: import('../mozjpeg/encoder-meta').EncodeOptions, data: ImageData, options: import('../mozjpeg/encoder-meta').EncodeOptions,
): Promise<ArrayBuffer> { ): Promise<ArrayBuffer> {
const { encode } = await import( const { encode } = await import(
/* webpackChunkName: "process-mozjpeg-enc" */ /* webpackChunkName: "process-mozjpeg-enc" */
'../mozjpeg/encoder'); '../mozjpeg/encoder');
return encode(data, options); return timed('mozjpegEncode', () => encode(data, options));
} }
async function quantize( async function quantize(
@@ -17,7 +22,7 @@ async function quantize(
const { process } = await import( const { process } = await import(
/* webpackChunkName: "process-imagequant" */ /* webpackChunkName: "process-imagequant" */
'../imagequant/processor'); '../imagequant/processor');
return process(data, opts); return timed('quantize', () => process(data, opts));
} }
async function rotate( async function rotate(
@@ -27,7 +32,7 @@ async function rotate(
/* webpackChunkName: "process-rotate" */ /* webpackChunkName: "process-rotate" */
'../rotate/processor'); '../rotate/processor');
return rotate(data, opts); return timed('rotate', () => rotate(data, opts));
} }
async function resize( async function resize(
@@ -43,13 +48,13 @@ async function resize(
const ratio = Math.max(widthRatio, heightRatio); const ratio = Math.max(widthRatio, heightRatio);
if (ratio <= 1) return data; if (ratio <= 1) return data;
const factor = clamp(Math.ceil(ratio), { min: 2, max: 4 }) as 2|3|4; const factor = clamp(Math.ceil(ratio), { min: 2, max: 4 }) as 2|3|4;
return hqx(data, { factor }); return timed('hqx', () => hqx(data, { factor }));
} }
const { resize } = await import( const { resize } = await import(
/* webpackChunkName: "process-resize" */ /* webpackChunkName: "process-resize" */
'../resize/processor'); '../resize/processor');
return resize(data, opts); return timed('resize', () => resize(data, opts));
} }
async function optiPngEncode( async function optiPngEncode(
@@ -58,7 +63,7 @@ async function optiPngEncode(
const { compress } = await import( const { compress } = await import(
/* webpackChunkName: "process-optipng" */ /* webpackChunkName: "process-optipng" */
'../optipng/encoder'); '../optipng/encoder');
return compress(data, options); return timed('optiPngEncode', () => compress(data, options));
} }
async function webpEncode( async function webpEncode(
@@ -67,14 +72,14 @@ async function webpEncode(
const { encode } = await import( const { encode } = await import(
/* webpackChunkName: "process-webp-enc" */ /* webpackChunkName: "process-webp-enc" */
'../webp/encoder'); '../webp/encoder');
return encode(data, options); return timed('webpEncode', () => encode(data, options));
} }
async function webpDecode(data: ArrayBuffer): Promise<ImageData> { async function webpDecode(data: ArrayBuffer): Promise<ImageData> {
const { decode } = await import( const { decode } = await import(
/* webpackChunkName: "process-webp-dec" */ /* webpackChunkName: "process-webp-dec" */
'../webp/decoder'); '../webp/decoder');
return decode(data); return timed('webpDecode', () => decode(data));
} }
const exports = { const exports = {