forked from mtusi2026/mtusi2026lab2
224 lines
7.6 KiB
Python
224 lines
7.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Замер производительности чтения первой страницы истории.
|
|
Используется до и после включения кэша (track A).
|
|
|
|
Запуск:
|
|
python scripts/track_a_bench.py --history 1000 --requests 300 --output evidence/track_a_before.json
|
|
python scripts/track_a_bench.py --history 1000 --requests 300 --output evidence/track_a_after.json
|
|
|
|
Опционально: --reset-pg-stat и --read-pg-stat для подсчёта SQL-запросов
|
|
(требует включённого pg_stat_statements в postgres).
|
|
"""
|
|
import argparse
|
|
import base64
|
|
import json
|
|
import math
|
|
import statistics
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
BASE = "http://127.0.0.1:8080"
|
|
PASSWORD = "Example-only-not-a-real-secret-123"
|
|
|
|
|
|
def call(method, path, body=None, token=None, raw_basic=None, timeout=30):
|
|
headers = {"Accept": "application/json"}
|
|
if token:
|
|
headers["Authorization"] = f"Bearer {token}"
|
|
elif raw_basic:
|
|
headers["Authorization"] = f"Basic {raw_basic}"
|
|
|
|
data = None
|
|
if body is not None:
|
|
data = json.dumps(body).encode("utf-8")
|
|
headers["Content-Type"] = "application/json"
|
|
|
|
req = urllib.request.Request(BASE + path, data=data, headers=headers, method=method)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=timeout) as r:
|
|
payload = r.read()
|
|
return r.status, (json.loads(payload) if payload else None)
|
|
except urllib.error.HTTPError as e:
|
|
payload = e.read()
|
|
try:
|
|
return e.code, (json.loads(payload) if payload else None)
|
|
except ValueError:
|
|
return e.code, None
|
|
|
|
|
|
def setup(history: int):
|
|
name = f"bench_{uuid.uuid4().hex[:8]}"
|
|
s, u = call("POST", "/api/v1/users", {
|
|
"username": name, "display_name": name, "password": PASSWORD,
|
|
})
|
|
assert s == 201, f"create_user: {s} {u}"
|
|
|
|
cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode()
|
|
s, session = call("POST", "/api/v1/auth/sessions", raw_basic=cred)
|
|
assert s == 201, f"login: {s} {session}"
|
|
token = session["token"]
|
|
|
|
s, chat = call("POST", "/api/v1/chats",
|
|
{"title": "bench", "member_ids": [u["id"]]}, token=token)
|
|
assert s == 201, f"create_chat: {s} {chat}"
|
|
cid = chat["id"]
|
|
|
|
print(f"[setup] seeding {history} messages into chat {cid} ...", flush=True)
|
|
for i in range(history):
|
|
s, _ = call("POST", f"/api/v1/chats/{cid}/messages",
|
|
{"text": f"m{i}"}, token=token)
|
|
if s != 201:
|
|
print(f" seed failed at {i}: {s}")
|
|
break
|
|
if (i + 1) % 500 == 0:
|
|
print(f" {i + 1}/{history}", flush=True)
|
|
|
|
return token, cid
|
|
|
|
|
|
def percentiles(latencies_ms: list[float]):
|
|
if not latencies_ms:
|
|
return {}
|
|
s = sorted(latencies_ms)
|
|
|
|
def pct(p):
|
|
idx = max(0, math.ceil(p * len(s)) - 1)
|
|
return round(s[idx], 3)
|
|
|
|
return {
|
|
"p50": pct(0.50),
|
|
"p95": pct(0.95),
|
|
"p99": pct(0.99),
|
|
"min": round(s[0], 3),
|
|
"max": round(s[-1], 3),
|
|
"mean": round(statistics.mean(s), 3),
|
|
}
|
|
|
|
|
|
def measure(token, cid, requests: int, path: str = "messages"):
|
|
latencies = []
|
|
errors = 0
|
|
t_start = time.perf_counter()
|
|
|
|
for i in range(requests):
|
|
t0 = time.perf_counter()
|
|
s, _ = call("GET", f"/api/v1/chats/{cid}/{path}?limit=50", token=token)
|
|
dt_ms = (time.perf_counter() - t0) * 1000
|
|
if s == 200:
|
|
latencies.append(dt_ms)
|
|
else:
|
|
errors += 1
|
|
|
|
elapsed = time.perf_counter() - t_start
|
|
return {
|
|
"requests": requests,
|
|
"successful": len(latencies),
|
|
"errors": errors,
|
|
"elapsed_s": round(elapsed, 3),
|
|
"rps": round(len(latencies) / elapsed, 2) if elapsed > 0 else None,
|
|
"latency_ms": percentiles(latencies),
|
|
}
|
|
|
|
|
|
def reset_pg_stat():
|
|
"""Сбросить pg_stat_statements. Требует включённого расширения в postgres."""
|
|
import subprocess
|
|
cmd = [
|
|
"docker", "compose", "exec", "-T", "postgres",
|
|
"psql", "-U", "messenger", "-d", "messenger",
|
|
"-c", "SELECT pg_stat_statements_reset();",
|
|
]
|
|
try:
|
|
subprocess.run(cmd, check=True, capture_output=True, timeout=15)
|
|
print("[pg] pg_stat_statements_reset ok")
|
|
except Exception as e:
|
|
print(f"[pg] reset failed (ok if extension not enabled): {e}")
|
|
|
|
|
|
def read_pg_stat() -> dict:
|
|
"""Прочитать счётчик SQL-запросов к messages."""
|
|
import subprocess
|
|
query = (
|
|
"SELECT calls, rows, total_exec_time, mean_exec_time "
|
|
"FROM pg_stat_statements "
|
|
"WHERE query ILIKE '%FROM messages%' "
|
|
"AND query ILIKE '%ORDER BY%' "
|
|
"ORDER BY calls DESC LIMIT 5;"
|
|
)
|
|
cmd = [
|
|
"docker", "compose", "exec", "-T", "postgres",
|
|
"psql", "-U", "messenger", "-d", "messenger",
|
|
"-t", "-A", "-F", ",", "-c", query,
|
|
]
|
|
try:
|
|
out = subprocess.run(cmd, check=True, capture_output=True, text=True, timeout=15)
|
|
lines = [l.strip() for l in out.stdout.splitlines() if l.strip()]
|
|
total_calls = 0
|
|
rows = []
|
|
for line in lines:
|
|
parts = line.split(",")
|
|
if len(parts) >= 4:
|
|
try:
|
|
calls = int(parts[0])
|
|
total_calls += calls
|
|
rows.append({
|
|
"calls": calls,
|
|
"rows": int(parts[1]),
|
|
"total_ms": float(parts[2]),
|
|
"mean_ms": float(parts[3]),
|
|
})
|
|
except ValueError:
|
|
continue
|
|
return {"total_calls_to_messages": total_calls, "details": rows}
|
|
except Exception as e:
|
|
print(f"[pg] read failed (ok if extension not enabled): {e}")
|
|
return {"error": str(e)}
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser(description="Track A bench: измерение чтения первой страницы")
|
|
ap.add_argument("--history", type=int, default=1000,
|
|
help="Сколько сообщений засеять в чат")
|
|
ap.add_argument("--requests", type=int, default=300,
|
|
help="Сколько GET-запросов сделать для замера")
|
|
ap.add_argument("--output", required=True,
|
|
help="Путь к JSON-файлу результата")
|
|
ap.add_argument("--reset-pg-stat", action="store_true",
|
|
help="Сбросить pg_stat_statements перед замером")
|
|
ap.add_argument("--read-pg-stat", action="store_true",
|
|
help="Прочитать счётчик SQL после замера")
|
|
args = ap.parse_args()
|
|
|
|
if args.reset_pg_stat:
|
|
reset_pg_stat()
|
|
|
|
print(f"[1/3] setup: {args.history} messages", flush=True)
|
|
token, cid = setup(args.history)
|
|
|
|
# прогрев соединений
|
|
call("GET", f"/api/v1/chats/{cid}/messages?limit=50", token=token)
|
|
|
|
print(f"[2/3] measure: {args.requests} GET requests", flush=True)
|
|
result = measure(token, cid, args.requests)
|
|
|
|
if args.read_pg_stat:
|
|
print("[3/3] read pg_stat_statements", flush=True)
|
|
result["pg_stat"] = read_pg_stat()
|
|
|
|
result["history"] = args.history
|
|
result["chat_id"] = cid
|
|
|
|
out_path = Path(args.output)
|
|
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
out_path.write_text(json.dumps(result, indent=2), encoding="utf-8")
|
|
|
|
print(json.dumps(result, indent=2))
|
|
print(f"\nSaved: {out_path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |