#!/usr/bin/env python3 """Closed-loop image pipeline experiment; call once per WORKER_CONCURRENCY setting.""" from concurrent.futures import ThreadPoolExecutor, as_completed import hashlib, io, json, math, os, random, subprocess, threading, time from pathlib import Path from client import Client, arguments, check, run from PIL import Image def main(): def configure(p): p.add_argument('--count',type=int,default=24) p.add_argument('--profile',choices=['small','mixed','large'],default='mixed') p.add_argument('--upload-concurrency',type=int,default=8) p.add_argument('--worker-concurrency',type=int,required=True) p.add_argument('--output',required=True) a=arguments('Synthetic image pipeline throughput, queue wait, ready latency and worker RSS',configure) check(1<=a.count<=500 and 1<=a.upload_concurrency<=64,'invalid experiment bounds') c=Client(a.base_url,a.ca_file,timeout=30); alice,bob,_,chat=c.bootstrap() profiles={'small':[(320,200)],'mixed':[(320,200),(1024,768),(2048,1536)],'large':[(2048,1536)]} dimensions=profiles[a.profile] random.seed(20261106) def image_bytes(index): w,h=dimensions[index%len(dimensions)]; rng=random.Random(index+700) im=Image.frombytes('RGB',(w,h),rng.randbytes(w*h*3)) buf=io.BytesIO(); im.save(buf,format='PNG',compress_level=1); return buf.getvalue(),(w,h) barrier=threading.Barrier(a.upload_concurrency) def upload(i): raw,size=image_bytes(i); local=Client(a.base_url,a.ca_file,timeout=60); barrier.wait() t=time.monotonic() res=local.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=bench-%s.png'%i,202,actor=alice,raw=raw,headers={'Content-Type':'image/png'},schema='Attachment') return {'id':res['json']['id'],'accepted':time.monotonic(),'upload_seconds':time.monotonic()-t,'size_bytes':len(raw),'source_size':size} rss=[]; stop=threading.Event() def sample_rss(): while not stop.is_set(): try: out=subprocess.check_output(['docker','stats','--no-stream','--format','{{.MemUsage}}','mtusi_lab5_solution-worker-1'],stderr=subprocess.DEVNULL,text=True,timeout=3).strip() used=out.split('/')[0].strip(); rss.append(used) except Exception: pass stop.wait(.25) sampler=threading.Thread(target=sample_rss,daemon=True); sampler.start() started=time.monotonic() with ThreadPoolExecutor(max_workers=a.upload_concurrency) as pool: jobs=list(pool.map(upload,range(a.count))) upload_done=time.monotonic(); pending={x['id']:x for x in jobs}; ready_ms=[]; queue_wait_ms=[]; deadline=time.monotonic()+180 while pending and time.monotonic()