"""Ten simultaneous HTTP uploads, bounded synthetic PNGs, no retries.""" from concurrent.futures import ThreadPoolExecutor import hashlib import io import json from pathlib import Path import random import statistics import sys import threading import time sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests')) from client import Client,check from PIL import Image def percentile(values,p): if not values: return None values=sorted(values); pos=(len(values)-1)*p; lo=int(pos); hi=min(lo+1,len(values)-1) return values[lo]+(values[hi]-values[lo])*(pos-lo) def main(): c=Client('http://lb:8080',timeout=40) alice,bob,eve,chat=c.bootstrap() results=[] for side in (1024,1792): rng=random.Random(2301+side) image=Image.frombytes('RGB',(side,side),rng.randbytes(side*side*3)) buff=io.BytesIO(); image.save(buff,format='PNG'); data=buff.getvalue(); del image,buff check(len(data)<=10*1024*1024,'invalid benchmark size') # Warmup separately; no retry hides transient failures. c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=warmup.png',201,actor=alice,raw=data,headers={'Content-Type':'image/png'},schema='Attachment') for repeat in range(3): barrier=threading.Barrier(11); start_event=threading.Event() def task(_): local=Client('http://lb:8080',timeout=40); barrier.wait(); start_event.wait(); started=time.monotonic() try: att=local.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=load.png',201,actor=alice,raw=data,headers={'Content-Type':'image/png'},schema='Attachment')['json'] check(att['sha256']==hashlib.sha256(data).hexdigest(),'bad checksum') return {'ok':True,'seconds':time.monotonic()-started,'id':att['id']} except Exception as exc: return {'ok':False,'seconds':time.monotonic()-started,'error':type(exc).__name__} with ThreadPoolExecutor(max_workers=10) as pool: futures=[pool.submit(task,i) for i in range(10)] barrier.wait(); started=time.monotonic(); start_event.set() rows=[future.result() for future in futures] elapsed=time.monotonic()-started times=[r['seconds'] for r in rows if r['ok']] for row in rows: if row['ok']: check(c.call('GET','/api/v1/attachments/'+row['id']+'/content',actor=bob)['bytes']==data,'confirmed upload not durable') results.append({'size_bytes':len(data),'side':side,'repeat':repeat+1,'concurrency':10,'attempts':10,'successes':len(times),'errors':10-len(times),'elapsed_seconds':elapsed,'successful_rps':len(times)/elapsed,'p50_seconds':percentile(times,.5),'p95_seconds':percentile(times,.95),'p99_seconds':percentile(times,.99)}) print(json.dumps({'result':'PASS' if all(r['errors']==0 for r in results) else 'FAIL','profile':'closed-loop batch, 10 simultaneous uploads, 3 repeats per size, 1 warmup, all confirmed originals downloaded','runs':results})) check(all(r['errors']==0 for r in results),'upload failures') if __name__=='__main__': main()