Files
mtusi2026lab6/student-tests/resources.py
T

53 lines
2.4 KiB
Python

"""Finite simultaneous jobs including 40 MP; record actual worker cgroup peak/RSS."""
import concurrent.futures
import hashlib
import json
from pathlib import Path
import subprocess
import sys
import threading
import time
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client
from images import png
def docker(*args):return subprocess.check_output(['docker',*args],text=True).strip()
def memory(container):
code="import json;from pathlib import Path;p=Path('/sys/fs/cgroup');print(json.dumps({k:(p/k).read_text().strip() for k in ['memory.current','memory.peak','memory.events','memory.stat','cpu.stat']}))"
return json.loads(docker('exec',container,'python','-c',code))
def main():
c=Client(timeout=30);a,b,e,chat=c.bootstrap()
profiles=[(320,200,False),(1800,1400,True),(10000,4000,False)]
result=[]
for w,h,noise in profiles:
content=png(w,h,noise=noise)
docker('compose','restart','worker');container=docker('compose','ps','-q','worker')
before=memory(container);samples=[];done=threading.Event();start=time.monotonic()
def sample():
while not done.is_set():
samples.append({'seconds':round(time.monotonic()-start,4),'worker':memory(container)})
done.wait(.05)
sampler=threading.Thread(target=sample);sampler.start()
barrier=threading.Barrier(10)
def upload(i):
barrier.wait();t=time.monotonic()
r=Client(timeout=30).call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=load.png',202,actor=a,raw=content,headers={'Content-Type':'image/png'},schema='Attachment')
accepted=time.monotonic()-t
ready=Client(timeout=30).wait_ready(b,r['json'])
assert ready['sha256']==hashlib.sha256(content).hexdigest()
return {'index':i,'accepted_seconds':accepted,'ready_seconds':time.monotonic()-t,'status':ready['status']}
try:
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool: replies=list(pool.map(upload,range(10)))
finally:done.set();sampler.join()
after=memory(container);assert 'oom_kill 0' in after['memory.events']
result.append({'width':w,'height':h,'bytes':len(content),'clients':10,'before':before,'after':after,'samples':samples,'replies':replies,'seconds':time.monotonic()-start})
print(json.dumps({'method':'10 simultaneous uploads, worker concurrency=1; cgroup v2 samples include child renderer; anon is resident anonymous memory, memory.peak includes tmpfs/kernel/cache','experiments':result},indent=2))
if __name__=='__main__':main()