Files
mtusi2026lab2/student-tests/bench.py
T
BVT2304_Shiyan f07ad1e097
Skeleton contracts / contracts (pull_request) Canceled after 0s
lab2 - оценка 5
2026-10-07 19:06:42 +03:00

276 lines
8.5 KiB
Python

#!/usr/bin/env python3
"""Track 1 bench: матрица нагрузка x размер истории для in-memory store.
Только stdlib. Пример:
python student-tests/bench.py --label before --container mtusi-messenger-lab1
"""
import argparse
import csv
import http.client
import json
import subprocess
import threading
import time
import uuid
class Client:
def __init__(self, host, port, user_id=None):
self.host, self.port, self.user_id = host, port, user_id
self.conn = None
def _ensure(self):
if self.conn is None:
self.conn = http.client.HTTPConnection(self.host, self.port, timeout=30)
def request(self, method, path, body=None):
headers = {}
data = None
if body is not None:
headers["Content-Type"] = "application/json"
data = json.dumps(body).encode()
if self.user_id:
headers["X-User-Id"] = self.user_id
for attempt in (0, 1):
try:
self._ensure()
self.conn.request(method, path, body=data, headers=headers)
resp = self.conn.getresponse()
payload = resp.read()
return resp.status, (json.loads(payload) if payload else None)
except Exception:
self.conn = None
if attempt:
raise
time.sleep(0.05)
class Phase:
def __init__(self):
self.lock = threading.Lock()
self.lat = []
self.errors = 0
self.requests = 0
def add(self, ms, ok):
with self.lock:
self.lat.append(ms)
self.requests += 1
if not ok:
self.errors += 1
def row(self):
with self.lock:
lat = sorted(self.lat)
n = len(lat)
if n == 0:
return 0, 0, 0, 0
def p(q):
return lat[min(n - 1, int((q * n) // 100))]
return n, p(50), p(95), p(99)
class RssSampler:
def __init__(self, container, interval=2.0):
self.container, self.interval = container, interval
self.samples = []
self.stop = threading.Event()
self.th = threading.Thread(target=self._run, daemon=True)
def _run(self):
while not self.stop.is_set():
try:
out = subprocess.run(
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", self.container],
capture_output=True, text=True, timeout=10).stdout.strip().splitlines()
if out:
self.samples.append(parse_mib(out[0].split("/")[0].strip()))
except Exception:
pass
self.stop.wait(self.interval)
def start(self):
self.th.start()
return self
def finish(self):
self.stop.set()
self.th.join(timeout=5)
if not self.samples:
return 0.0, 0.0
return sum(self.samples) / len(self.samples), max(self.samples)
def parse_mib(s):
s = s.strip()
if s.endswith("GiB"):
return float(s[:-3]) * 1024
if s.endswith("MiB"):
return float(s[:-3])
if s.endswith("KiB"):
return float(s[:-4]) / 1024
return 0.0
def run_phase(host, port, clients, duration, op):
ph = Phase()
deadline = time.perf_counter() + duration
def worker(idx):
c = Client(host, port, op_user(idx))
while time.perf_counter() < deadline:
t0 = time.perf_counter()
ok = op(c, idx)
ph.add((time.perf_counter() - t0) * 1000.0, ok)
threads = [threading.Thread(target=worker, args=(i,)) for i in range(clients)]
for t in threads:
t.start()
for t in threads:
t.join()
return ph
USERS = []
def op_user(idx):
return USERS[idx % len(USERS)]
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--base-url", default="http://localhost:8080")
ap.add_argument("--label", required=True)
ap.add_argument("--container", default="mtusi-messenger-lab1")
ap.add_argument("--sizes", default="10000,50000,150000")
ap.add_argument("--clients", default="8,32,96")
ap.add_argument("--duration", type=float, default=10.0)
ap.add_argument("--out", default="student-tests/results")
args = ap.parse_args()
hostport = args.base_url.split("://")[-1].split("/")
host, _, port = hostport[0].partition(":")
port = int(port or 80)
sizes = [int(x) for x in args.sizes.split(",")]
clients = [int(x) for x in args.clients.split(",")]
os_makedirs(args.out)
admin = Client(host, port)
global USERS
n_users = max(clients)
for i in range(n_users):
st, body = admin.request("POST", "/api/v1/users",
{"username": "b" + uuid.uuid4().hex[:12], "display_name": "Bench"})
assert st == 201, (st, body)
USERS.append(body["id"])
admin.user_id = USERS[0] # <-- ДОБАВИТЬ ЭТУ СТРОКУ: чаты создаёт участник
st, chat = admin.request("POST", "/api/v1/chats",
{"title": "bench", "member_ids": USERS})
assert st == 201, (st, chat)
chat_id = chat["id"]
st, chat2 = admin.request("POST", "/api/v1/chats",
{"title": "bench-writes", "member_ids": USERS})
assert st == 201
chat2_id = chat2["id"]
rows = []
have = 0
for H in sizes:
# досеиваем историю до H (кумулятивно, экономим время)
if H > have:
seed(host, port, chat_id, H - have)
have = H
for C in clients:
print(f"[{args.label}] read H={H} C={C} ...", flush=True)
rss = RssSampler(args.container).start()
ph = run_phase(host, port, C, args.duration, make_read_op(chat_id))
avg, mx = rss.finish()
n, p50, p95, p99 = ph.row()
rows.append([args.label, "read", H, C, args.duration, n, ph.errors,
round(n / args.duration, 1), p50, p95, p99, round(avg, 1), round(mx, 1)])
for C in clients:
print(f"[{args.label}] write C={C} ...", flush=True)
rss = RssSampler(args.container).start()
ph = run_phase(host, port, C, args.duration, make_write_op(chat2_id))
avg, mx = rss.finish()
n, p50, p95, p99 = ph.row()
rows.append([args.label, "write", 0, C, args.duration, n, ph.errors,
round(n / args.duration, 1), p50, p95, p99, round(avg, 1), round(mx, 1)])
path = f"{args.out}/{args.label}.csv"
with open(path, "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["label", "phase", "history", "clients", "duration_s", "requests",
"errors", "rps", "p50_ms", "p95_ms", "p99_ms", "rss_avg_mib", "rss_max_mib"])
w.writerows(rows)
print(f"saved {path}")
def os_makedirs(p):
import os
os.makedirs(p, exist_ok=True)
def seed(host, port, chat_id, count):
threads_n = 16
per = count // threads_n
lock = threading.Lock()
state = {"done": 0, "errors": 0}
t0 = time.perf_counter()
def worker(idx):
c = Client(host, port, USERS[0])
for j in range(per):
st, _ = c.request("POST", f"/api/v1/chats/{chat_id}/messages",
{"text": f"seed-{idx}-{j}"})
with lock:
state["done"] += 1
if st != 201:
state["errors"] += 1
threads = [threading.Thread(target=worker, args=(i,)) for i in range(threads_n)]
for t in threads:
t.start()
for t in threads:
t.join()
dt = time.perf_counter() - t0
print(f" seeded {state['done']} msgs in {dt:.1f}s "
f"({state['done']/dt:.0f} rps, errors={state['errors']})", flush=True)
def make_read_op(chat_id):
cursors = {}
def op(c, idx):
cur = cursors.get(idx)
path = f"/api/v1/chats/{chat_id}/messages?limit=50"
if cur:
path += "&cursor=" + cur
st, body = c.request("GET", path)
if st != 200:
cursors[idx] = None
return False
cursors[idx] = body.get("next_cursor")
return True
return op
def make_write_op(chat_id):
counter = {"i": 0}
lock = threading.Lock()
def op(c, idx):
with lock:
counter["i"] += 1
i = counter["i"]
st, _ = c.request("POST", f"/api/v1/chats/{chat_id}/messages", {"text": f"w{i}"})
return st == 201
return op
if __name__ == "__main__":
main()