forked from mtusi2026/mtusi2026lab4
34 lines
1.5 KiB
JavaScript
34 lines
1.5 KiB
JavaScript
// Peak RSS of one full decode check with the same sharp pipeline as
|
|
// app/src/attachments/image/image-decoder.ts, on worst-case inputs near
|
|
// MAX_IMAGE_PIXELS. Runs where the API runs (same libvips, VIPS_DISC_THRESHOLD):
|
|
// docker compose exec -T api node - < scripts/lab4/decode_memory.mjs # reads files from IMAGES (paths in the container)
|
|
// or on the host from app/: IMAGES="a.png b.jpg" node ../scripts/lab4/decode_memory.mjs
|
|
import { readFileSync } from 'node:fs';
|
|
import { createRequire } from 'node:module';
|
|
|
|
const sharp = createRequire(`${process.cwd()}/`)('sharp');
|
|
sharp.cache(false);
|
|
sharp.concurrency(1);
|
|
for (const file of (process.env.IMAGES ?? '').split(/\s+/u).filter(Boolean)) {
|
|
const data = readFileSync(file);
|
|
const meta = await sharp(data).metadata();
|
|
const base = process.memoryUsage().rss;
|
|
let peak = base;
|
|
const timer = setInterval(() => {
|
|
peak = Math.max(peak, process.memoryUsage().rss);
|
|
}, 5);
|
|
const started = Date.now();
|
|
let verdict = 'ok';
|
|
try {
|
|
await sharp(data, { failOn: 'warning', sequentialRead: true, limitInputPixels: 40_000_000 })
|
|
.resize(64, 64, { fit: 'inside' })
|
|
.toBuffer();
|
|
} catch (error) {
|
|
verdict = `rejected: ${String(error.message).split('\n')[0]}`;
|
|
}
|
|
clearInterval(timer);
|
|
console.log(
|
|
`${meta.format} ${meta.width}x${meta.height} ${meta.channels}ch ${meta.depth}${meta.isProgressive ? ' progressive' : ''}${meta.isPalette ? ' palette' : ''} bytes=${data.length} decode_ms=${Date.now() - started} peak_delta_rss_mib=${((peak - base) / 1048576).toFixed(1)} ${verdict}`,
|
|
);
|
|
}
|