Files

69 lines
4.6 KiB
Python

#!/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()<deadline:
for aid,item in list(pending.items()):
status=c.call('GET','/api/v1/attachments/'+aid,actor=alice,schema='Attachment')['json']
now=time.monotonic()
if status['status']=='failed': raise RuntimeError('pipeline job failed')
if status['status']=='processing' and 'first_processing' not in item: item['first_processing']=now
if status['status']=='ready':
ready_ms.append((now-item['accepted'])*1000)
queue_wait_ms.append(((item.get('first_processing',now))-item['accepted'])*1000)
del pending[aid]
if pending: time.sleep(.1)
stop.set(); sampler.join(timeout=2)
check(not pending,'ready timeout for %s attachments'%len(pending))
elapsed=time.monotonic()-started
def pct(xs,p): return round(sorted(xs)[max(0,math.ceil(p*len(xs))-1)],2) if xs else None
def mib(v):
try:
token=v.split()[0]; n=float(token[:-3]); unit=token[-3:]
return n*(1024 if unit=='GiB' else 1)
except Exception:return -1
peak=max(rss,key=mib) if rss else None
result={'track':'queue-overload','profile':a.profile,'worker_concurrency':a.worker_concurrency,'upload_concurrency':a.upload_concurrency,'accepted':len(jobs),'failed':0,'elapsed_seconds':round(elapsed,3),'throughput_ready_per_second':round(len(jobs)/elapsed,3),'ready_latency_ms':{'p50':pct(ready_ms,.5),'p95':pct(ready_ms,.95),'p99':pct(ready_ms,.99)},'estimated_queue_wait_ms':{'p50':pct(queue_wait_ms,.5),'p95':pct(queue_wait_ms,.95)},'worker_rss_samples':rss,'worker_rss_peak_sample':peak,'total_upload_bytes':sum(x['size_bytes'] for x in jobs),'limitation':'Closed-loop synthetic workload; queue wait is bounded by 100 ms status polling and can miss short processing states. Docker RSS sampling is 250 ms.'}
dest=Path(a.output); dest.parent.mkdir(parents=True,exist_ok=True); dest.write_text(json.dumps(result,indent=2)+'\n'); print(json.dumps(result,indent=2))
if __name__=='__main__': run(main)