Files
mtusi2026lab4/tests/upload_memory.py
T
2026-11-12 19:38:03 +03:00

79 lines
3.8 KiB
Python

"""10 simultaneous binary uploads; sample per-container cgroup memory including tmpfs."""
import hashlib
import io
import json
import os
import subprocess
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from PIL import Image
from client import Client, arguments, check, run
def main():
args=arguments('Upload concurrency and memory experiment')
c=Client(args.base_url,args.ca_file,timeout=30)
alice,bob,_,chat=c.bootstrap()
containers=subprocess.check_output(['docker','compose','ps','-q','api'],text=True).split()
check(len(containers)==2,'experiment requires two API replicas')
# One sampler process per container, not one docker invocation per sample.
sampler='''import json,time
from pathlib import Path
values=[]
for _ in range(750):
values.append(int(Path('/sys/fs/cgroup/memory.current').read_text()))
time.sleep(.02)
print(json.dumps({'baseline_bytes':values[0],'sampled_peak_bytes':max(values),'samples':len(values),'interval_seconds':.02,'cgroup_limit_bytes':int(Path('/sys/fs/cgroup/memory.max').read_text())}))
'''
samplers=[subprocess.Popen(['docker','exec',cid,'python','-c',sampler],stdout=subprocess.PIPE,text=True)
for cid in containers]
time.sleep(.2)
results=[]
try:
for side in (256,1280):
source=io.BytesIO()
Image.frombytes('RGB',(side,side),os.urandom(side*side*3)).save(source,format='PNG')
content=source.getvalue()
barrier=threading.Barrier(10)
def upload(_):
local=Client(args.base_url,args.ca_file,timeout=30)
barrier.wait()
started=time.perf_counter()
response=local.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=memory.png',
expected=[201,503],actor=alice,raw=content,headers={'Content-Type':'image/png'})
if response['status']==201:
att=json.loads(response['bytes'])
check(att['sha256']==hashlib.sha256(content).hexdigest(),'upload checksum differs')
downloaded=local.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)
check(downloaded['bytes']==content,'concurrent upload lost original')
return {'status':response['status'],'seconds':round(time.perf_counter()-started,4)}
with ThreadPoolExecutor(max_workers=10) as pool:
responses=list(pool.map(upload,range(10)))
check(all(r['status']==201 for r in responses),'configured 10-upload profile rejected a request')
results.append({'dimensions':[side,side],'file_bytes':len(content),
'concurrent_uploads':10,'responses':responses})
memory=[]
for process in samplers:
output,_=process.communicate(timeout=25)
check(process.returncode==0,'memory sampler failed')
memory.append(json.loads(output))
check(all(m['sampled_peak_bytes']<m['cgroup_limit_bytes'] for m in memory),'memory reached container limit')
print(json.dumps({'result':'PASS','api_replicas':2,'uploads_per_replica_limit':5,
'decode_concurrency_per_replica':1,'temporary_storage_per_replica_bytes':64*1024*1024,
'profiles':results,'memory':memory,'limitations':[
'20ms sampling can miss short peaks',
'two synthetic image sizes do not cover the worst-case 40MP input',
'cgroup memory includes Python, image decoding and tmpfs; experiment is not a throughput benchmark',
'deployment restores the default admission limit of 1 after this experiment'
]}),flush=True)
finally:
for process in samplers:
if process.poll() is None:
process.terminate();process.wait()
if __name__=='__main__':
run(main)