forked from mtusi2026/mtusi2026lab2
Persist messenger in PostgreSQL with sessions, cache and recovery evidence
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
"""Bounded closed-loop HTTP load, raw latency and Docker resource samples."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import csv
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
import httpx
|
||||
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
def docker(*args):
|
||||
return subprocess.check_output(['docker',*args],cwd=ROOT,text=True,encoding='utf-8').strip()
|
||||
rows=[]; samples=[]; done=threading.Event()
|
||||
def sample():
|
||||
while not done.is_set():
|
||||
data=docker('stats','--no-stream','--format','{{json .}}',*docker('compose','ps','-q').splitlines())
|
||||
samples.extend({'elapsed_s':round(time.perf_counter()-start,3),**json.loads(line)} for line in data.splitlines())
|
||||
done.wait(1)
|
||||
with httpx.Client(base_url='http://127.0.0.1:8080',timeout=10) as c:
|
||||
body={'username':'load_'+uuid.uuid4().hex[:18],'display_name':'Load','password':'synthetic-password-123'}
|
||||
user=c.post('/api/v1/users',json=body).json()
|
||||
auth=c.post('/api/v1/auth/sessions',auth=(body['username'],body['password'])).json()
|
||||
headers={'Authorization':'Bearer '+auth['token']}
|
||||
chat=c.post('/api/v1/chats',headers=headers,json={'title':'load','member_ids':[user['id']]}).json()
|
||||
path='/api/v1/chats/'+chat['id']+'/messages'
|
||||
# Warm up the pool/cache for 2 seconds, outside measurement.
|
||||
deadline=time.perf_counter()+2
|
||||
while time.perf_counter()<deadline: c.get(path,headers=headers)
|
||||
start=time.perf_counter(); deadline=start+10
|
||||
sampler=threading.Thread(target=sample,daemon=True); sampler.start()
|
||||
def work(worker):
|
||||
i=0
|
||||
while time.perf_counter()<deadline:
|
||||
now=time.perf_counter(); method='POST' if i%5==0 else 'GET'; status=0
|
||||
try:
|
||||
r=c.request(method,path,headers=headers,**({'json':{'text':'synthetic '*100}} if method=='POST' else {'params':{'limit':50}}))
|
||||
status=r.status_code
|
||||
except httpx.HTTPError: pass
|
||||
rows.append({'worker':worker,'method':method,'elapsed_s':round(now-start,6),'latency_ms':round((time.perf_counter()-now)*1000,3),'status':status})
|
||||
i+=1
|
||||
with ThreadPoolExecutor(max_workers=10) as pool: list(pool.map(work,range(10)))
|
||||
duration=time.perf_counter()-start
|
||||
done.set(); sampler.join(10)
|
||||
success=sum(r['status'] in (200,201) for r in rows)
|
||||
latencies=sorted(r['latency_ms'] for r in rows)
|
||||
summary={'warmup_s':2,'duration_s':round(duration,3),'concurrency':10,'profile':'closed-loop: 1 write then 4 history reads per worker; 1000-character messages',
|
||||
'requests':len(rows),'success_rps':round(success/duration,2),'error_fraction':1-success/len(rows),
|
||||
**{f'p{p}_ms':latencies[math.ceil(len(latencies)*p/100)-1] for p in (50,95,99)},
|
||||
'statuses':{str(s):sum(r['status']==s for r in rows) for s in {r['status'] for r in rows}},'resource_source':'Docker stats samples; CPU percent is host-core percentage, memory is cgroup working set'}
|
||||
out=ROOT/'evidence'; out.mkdir(exist_ok=True)
|
||||
with (out/'http-load.csv').open('w',newline='',encoding='utf-8') as f:
|
||||
writer=csv.DictWriter(f,fieldnames=list(rows[0])); writer.writeheader(); writer.writerows(rows)
|
||||
(out/'http-resources.json').write_text(json.dumps(samples,indent=2)+'\n',encoding='utf-8')
|
||||
(out/'http-summary.json').write_text(json.dumps(summary,indent=2)+'\n',encoding='utf-8')
|
||||
print(json.dumps(summary,indent=2))
|
||||
assert success==len(rows),'Unexpected failed requests'
|
||||
Reference in New Issue
Block a user