Files
mtusi2026lab4/student-tests/upload_load.py

48 lines
1.9 KiB
Python

"""10 concurrent real PNG uploads at two sizes; RSS includes all API processes.
Run in api1: /proc gives that replica's RSS; Docker stats sampled by run-all.py
records memory (including tmpfs/cache) for BOTH replicas during the same run.
"""
import concurrent.futures
import io
import json
import os
from pathlib import Path
import statistics
import sys
import time
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
from client import Client, check
from PIL import Image
def main():
c = Client('http://lb:8080',timeout=60)
alice,bob,eve,chat = c.bootstrap()
results = []
for side in (512,1800):
out = io.BytesIO()
Image.frombytes('RGB',(side,side),os.urandom(side*side*3)).save(out,format='PNG')
data = out.getvalue()
c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=warm.png',201,
actor=alice,raw=data,headers={'Content-Type':'image/png'},schema='Attachment')
for repeat in range(3):
start = time.monotonic()
def one(_):
return c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=load.png',
actor=alice,expected=201,raw=data,headers={'Content-Type':'image/png'},schema='Attachment')['elapsed']
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
times = sorted(pool.map(one,range(10)))
duration = time.monotonic()-start
results.append(dict(size_bytes=len(data),pixels=side*side,concurrency=10,repeat=repeat+1,
duration_seconds=duration,successful_rps=10/duration,errors=0,
p50_seconds=statistics.median(times),p95_seconds=times[9],p99_seconds=times[9]))
print(json.dumps({'result':'PASS','profile':'closed-loop, 10 simultaneous uploads, 3 repeats/size',
'results':results},indent=2))
if __name__ == '__main__':
main()