forked from mtusi2026/mtusi2026lab5
51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
||
"""Peak RSS of `djpeg -strict -scale 1/8` on worst-case 40 Mpx JPEGs (sizing the API limit).
|
||
|
||
Run in a throwaway container with Pillow + libjpeg-turbo-progs:
|
||
python scripts/jpeg_decode_memory.py gen # writes /tmp/j/*.jpg (8000×5000)
|
||
python scripts/jpeg_decode_memory.py measure # one fresh child per file
|
||
The measuring parent stays tiny so RUSAGE_CHILDREN reflects djpeg only.
|
||
"""
|
||
|
||
import os
|
||
import resource
|
||
import subprocess
|
||
import sys
|
||
import time
|
||
|
||
OUT = "/tmp/j"
|
||
|
||
|
||
def gen() -> None:
|
||
from PIL import Image
|
||
|
||
os.makedirs(OUT, exist_ok=True)
|
||
noise = Image.effect_noise((8000, 5000), 64).convert("L")
|
||
rgb = Image.merge("RGB", (noise, noise.rotate(180), noise.transpose(Image.FLIP_LEFT_RIGHT)))
|
||
for name, img, kw in (
|
||
("baseline_420", rgb, dict(quality=30)),
|
||
("progressive_420", rgb, dict(quality=30, progressive=True)),
|
||
("progressive_444", rgb, dict(quality=20, progressive=True, subsampling=0)),
|
||
("progressive_cmyk", rgb.convert("CMYK"), dict(quality=15, progressive=True, subsampling=0)),
|
||
):
|
||
img.save(os.path.join(OUT, name + ".jpg"), "JPEG", **kw)
|
||
|
||
|
||
def measure_one(path: str) -> None:
|
||
t0 = time.monotonic()
|
||
proc = subprocess.run(["djpeg", "-strict", "-scale", "1/8", "-outfile", os.devnull, path], capture_output=True)
|
||
took = time.monotonic() - t0
|
||
peak = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss / 1024
|
||
print(f"{os.path.basename(path):22} {os.path.getsize(path) / 2**20:6.2f} MiB rc={proc.returncode} "
|
||
f"{took:5.2f}s djpeg_peak_rss={peak:.1f} MiB", flush=True)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
if sys.argv[1] == "gen":
|
||
gen()
|
||
elif sys.argv[1] == "one":
|
||
measure_one(sys.argv[2])
|
||
else:
|
||
for name in sorted(os.listdir(OUT)):
|
||
subprocess.run([sys.executable, __file__, "one", os.path.join(OUT, name)], check=True)
|