forked from mtusi2026/mtusi2026lab4
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
132 lines
3.7 KiB
JavaScript
132 lines
3.7 KiB
JavaScript
// One fresh process per input/cache/size combination. Executes the real validator.
|
|
import { createRequire } from 'node:module';
|
|
import { readFile, copyFile, mkdtemp, rm, rmdir } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
import {
|
|
setTimeout as delay,
|
|
setImmediate as tick,
|
|
} from 'node:timers/promises';
|
|
import { performance } from 'node:perf_hooks';
|
|
import assert from 'node:assert/strict';
|
|
|
|
const require = createRequire(`${process.cwd()}/package.json`);
|
|
const sharp = require('sharp');
|
|
const {
|
|
AttachmentService,
|
|
} = require('./dist/Attachment/attachment.service.js');
|
|
const [source, inputMode, cacheMode] = process.argv.slice(2);
|
|
assert.ok(['buffer', 'file'].includes(inputMode));
|
|
assert.ok(['default', 'off'].includes(cacheMode));
|
|
assert.equal(typeof global.gc, 'function');
|
|
if (cacheMode === 'off') sharp.cache(false);
|
|
const validator = new AttachmentService(null, null);
|
|
const snapshot = () => ({ ...process.memoryUsage(), sharp: sharp.cache() });
|
|
const directory = await mkdtemp(join(tmpdir(), 'sharp-profile-'));
|
|
let currentFile;
|
|
let active;
|
|
const sample = () => {
|
|
if (!active) return;
|
|
const memory = process.memoryUsage();
|
|
for (const key of Object.keys(memory))
|
|
active.peak[key] = Math.max(active.peak[key], memory[key]);
|
|
active.samples++;
|
|
const now = performance.now();
|
|
active.maxSampleGapMs = Math.max(
|
|
active.maxSampleGapMs,
|
|
now - active.lastSample,
|
|
);
|
|
active.lastSample = now;
|
|
};
|
|
const timer = setInterval(sample, 20);
|
|
timer.unref();
|
|
let sequence = 0;
|
|
async function validate(measure) {
|
|
const before = snapshot();
|
|
active = measure
|
|
? {
|
|
peak: { ...process.memoryUsage() },
|
|
samples: 0,
|
|
lastSample: performance.now(),
|
|
maxSampleGapMs: 0,
|
|
}
|
|
: undefined;
|
|
const started = performance.now();
|
|
let input;
|
|
try {
|
|
if (inputMode === 'file') {
|
|
// Real complete uses a unique temporary path for every upload, then unlinks it.
|
|
currentFile = join(directory, `${sequence++}.png`);
|
|
await copyFile(source, currentFile);
|
|
input = currentFile;
|
|
} else input = await readFile(source);
|
|
sample();
|
|
const afterInput = snapshot();
|
|
await validator.validateImage(input, 'image/png');
|
|
sample();
|
|
const afterValidation = snapshot();
|
|
input = null;
|
|
if (currentFile) {
|
|
await rm(currentFile);
|
|
currentFile = undefined;
|
|
}
|
|
sample();
|
|
const result = measure
|
|
? {
|
|
before,
|
|
afterInput,
|
|
afterValidation,
|
|
afterRelease: snapshot(),
|
|
peak: active.peak,
|
|
samples: active.samples,
|
|
maxSampleGapMs: active.maxSampleGapMs,
|
|
elapsedMs: performance.now() - started,
|
|
}
|
|
: undefined;
|
|
active = undefined;
|
|
return result;
|
|
} finally {
|
|
input = null;
|
|
}
|
|
}
|
|
try {
|
|
for (let i = 0; i < 4; i++) await validate(false);
|
|
const baseline = snapshot();
|
|
const uploads = [];
|
|
for (let i = 0; i < 3; i++) uploads.push(await validate(true));
|
|
const afterSeries = snapshot();
|
|
await delay(1000);
|
|
const afterIdle = snapshot();
|
|
// Diagnostic only: do not force GC during the measured series.
|
|
global.gc();
|
|
await tick();
|
|
global.gc();
|
|
await tick();
|
|
const afterGc = snapshot();
|
|
sharp.cache(false);
|
|
global.gc();
|
|
await tick();
|
|
const afterCacheClear = snapshot();
|
|
console.log(
|
|
JSON.stringify({
|
|
status: 'passed',
|
|
inputMode,
|
|
cacheMode,
|
|
intervalMs: 20,
|
|
nodeVersion: process.version,
|
|
sharpVersions: sharp.versions,
|
|
warmups: 4,
|
|
baseline,
|
|
uploads,
|
|
afterSeries,
|
|
afterIdle,
|
|
afterGc,
|
|
afterCacheClear,
|
|
}),
|
|
);
|
|
} finally {
|
|
clearInterval(timer);
|
|
if (currentFile) await rm(currentFile, { force: true });
|
|
await rmdir(directory);
|
|
}
|