forked from mtusi2026/mtusi2026lab2
Lab2: перенос исходников app/ и собственных тестов из lab1
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import math
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
BASE = "http://127.0.0.1:8080"
|
||||
OUT = Path(__file__).resolve().parents[1] / "evidence" / "track1.json"
|
||||
|
||||
|
||||
def call(method, path, body=None, actor=None):
|
||||
h = {"Accept": "application/json"}
|
||||
if actor:
|
||||
h["X-User-Id"] = actor
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode("utf-8")
|
||||
h["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(BASE + path, data=data, headers=h, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
return r.status, json.loads(r.read())
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, None
|
||||
|
||||
|
||||
def setup(history: int):
|
||||
s, u = call("POST", "/api/v1/users",
|
||||
{"username": f"load_{uuid.uuid4().hex[:10]}",
|
||||
"display_name": "load"})
|
||||
assert s == 201
|
||||
s, chat = call("POST", "/api/v1/chats",
|
||||
{"title": "load", "member_ids": [u["id"]]},
|
||||
actor=u["id"])
|
||||
assert s == 201
|
||||
for i in range(history):
|
||||
s, _ = call("POST", f"/api/v1/chats/{chat['id']}/messages",
|
||||
{"text": f"seed-{i}"}, actor=u["id"])
|
||||
assert s == 201
|
||||
return u, chat
|
||||
|
||||
|
||||
def measure(history, concurrency, per_worker=50):
|
||||
u, chat = setup(history)
|
||||
lat = []
|
||||
|
||||
def worker():
|
||||
for _ in range(per_worker):
|
||||
t0 = time.perf_counter()
|
||||
s, _ = call("GET", f"/api/v1/chats/{chat['id']}/messages?limit=50",
|
||||
actor=u["id"])
|
||||
if s == 200:
|
||||
lat.append(time.perf_counter() - t0)
|
||||
|
||||
t0 = time.perf_counter()
|
||||
with ThreadPoolExecutor(max_workers=concurrency) as ex:
|
||||
list(ex.map(lambda _: worker(), range(concurrency)))
|
||||
elapsed = time.perf_counter() - t0
|
||||
|
||||
lat.sort()
|
||||
def pct(p):
|
||||
return round(1000 * lat[max(0, math.ceil(p * len(lat)) - 1)], 3) if lat else None
|
||||
|
||||
return {
|
||||
"history": history,
|
||||
"concurrency": concurrency,
|
||||
"successful_rps": round(len(lat) / elapsed, 2),
|
||||
"p50_ms": pct(0.50), "p95_ms": pct(0.95), "p99_ms": pct(0.99),
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
rows = []
|
||||
for history in (100, 1000, 10000):
|
||||
for conc in (1, 10, 50):
|
||||
r = measure(history, conc)
|
||||
rows.append(r)
|
||||
print(r)
|
||||
OUT.parent.mkdir(parents=True, exist_ok=True)
|
||||
OUT.write_text(json.dumps(rows, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user