# Benchmark Sidecar Mask: Slow-Path v7 (segmentacja) vs Fast-Path (.mask.png). # Master kopiowany do tmp/ — oryginały w data/media/originals są niezmienne. import sys, os, json, time, shutil sys.path.insert(0, "services/recolor") ORIG = "data/media/originals/" MASTER = ORIG + "a1/a1bd6aeb76ca7c2b86a5c2196032246647b31e0f1a100b8d17a27717c7b72325.jpg" SWATCH = ORIG + "70/70e6537812c1454fd111c43021ad51a78072283cad9e0837c5d6bedf8e0deb36.jpg" os.makedirs("tmp/sidecar-bench", exist_ok=True) base = "tmp/sidecar-bench/base.jpg" shutil.copyfile(MASTER, base) sidecar = "tmp/sidecar-bench/base.mask.png" if os.path.exists(sidecar): os.remove(sidecar) from fabric_recolor.service import FabricRecolorService # --- przebieg 1: slow-path v7 (brak sidecar -> segmentacja + bake) --- t0 = time.time() svc1 = FabricRecolorService() rep1 = svc1.process_detailed(base, SWATCH, "tmp/sidecar-bench/out-slow.png") wall1 = time.time() - t0 print("SLOW", json.dumps(rep1.__dict__), f"wall={wall1:.1f}s", flush=True) assert os.path.exists(sidecar), "bake nie utworzył sidecar" # --- przebieg 2: fast-path (sidecar istnieje, świeży serwis = zero VRAM) --- t0 = time.time() svc2 = FabricRecolorService() rep2 = svc2.process_detailed(base, SWATCH, "tmp/sidecar-bench/out-fast.png") wall2 = time.time() - t0 print("FAST", json.dumps(rep2.__dict__), f"wall={wall2:.2f}s", flush=True) assert svc2._segmenters is None, "fast-path zbudował segmentery!" import numpy as np from PIL import Image a = np.asarray(Image.open("tmp/sidecar-bench/out-slow.png").convert("RGB"), int) b = np.asarray(Image.open("tmp/sidecar-bench/out-fast.png").convert("RGB"), int) print("max|slow-fast| =", int(np.abs(a - b).max())) print(json.dumps({"slow_s": rep1.elapsed_s, "fast_s": rep2.elapsed_s, "speedup": round(rep1.elapsed_s / max(rep2.elapsed_s, 1e-3), 1)}))