"""Isolate the actual Sharp validator: Buffer/file input, operation cache on/off.""" import importlib.util import json from pathlib import Path import subprocess import sys import tempfile from direct_client import ROOT, Failure, check, docker spec = importlib.util.spec_from_file_location('upload_metrics', ROOT / 'test/concurrent-uploads.py') metrics = importlib.util.module_from_spec(spec) spec.loader.exec_module(metrics) def main(): report = {'status': 'failed', 'cases': [], 'method': 'Fresh Node process per case, real AttachmentService.validateImage; 4 warm-ups, 3 measured operations; unique file paths; post-series GC and cache clearing are diagnostics only. RSS is not comparable to a full API process.'} try: with tempfile.TemporaryDirectory(prefix='sharp-memory-', dir=ROOT / 'evidence') as directory: for side in [512, 1792]: path = Path(directory) / 'image.png' image = metrics.png_image(side) + metrics.chunk(b'IEND', b'') path.write_bytes(image) for round_number in [1, 2]: for cache in (['default', 'off'] if round_number == 1 else ['off', 'default']): for mode in (['buffer', 'file'] if round_number == 1 else ['file', 'buffer']): output = docker('compose', 'run', '--rm', '--no-deps', '-T', '-e', 'UPLOAD_MEMORY_PROFILE=0', '-v', f'{directory}:/profile-input:ro', '-v', f'{ROOT / "test/sharp-memory-profile.mjs"}:/app/test/sharp-memory-profile.mjs:ro', '--entrypoint', 'node', 'api', '--expose-gc', 'test/sharp-memory-profile.mjs', '/profile-input/image.png', mode, cache) result = json.loads(output) check(result['status'] == 'passed' and len(result['uploads']) == 3, 'Validator profile failed') report['cases'].append({'round': round_number, 'sidePixels': side, 'fileBytes': len(image), **result}) print(f'PASS: round {round_number}, {side}px, {mode}, cache={cache}', flush=True) report['status'] = 'passed' except BaseException as error: report['errorType'] = type(error).__name__ raise finally: (ROOT / 'evidence/sharp-memory-profile.json').write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8') if __name__ == '__main__': try: main() except (Failure, ValueError, OSError, subprocess.SubprocessError) as error: print('FAIL: ' + (str(error) if isinstance(error, Failure) else type(error).__name__), file=sys.stderr) sys.exit(1)