forked from mtusi2026/mtusi2026lab2
174 lines
5.4 KiB
Python
174 lines
5.4 KiB
Python
"""EXPLAIN ANALYZE до/после на 10 000+ сообщений.
|
|
|
|
Сравнивает план OR-курсорa (было) и row-comparison (стало).
|
|
Результаты сохраняются в evidence/explain_pagination.txt.
|
|
|
|
Запуск (стек поднят):
|
|
|
|
python student-tests/explain_pagination.py
|
|
"""
|
|
import base64
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from datetime import datetime
|
|
|
|
sys.path.insert(0, os.path.dirname(__file__))
|
|
from _harness import BASE_URL, call
|
|
|
|
EVIDENCE = os.path.join(os.path.dirname(__file__), "..", "evidence")
|
|
os.makedirs(EVIDENCE, exist_ok=True)
|
|
OUTFILE = os.path.join(EVIDENCE, "explain_pagination.txt")
|
|
|
|
HEADER = "=" * 70
|
|
lines = []
|
|
|
|
|
|
def log(text):
|
|
lines.append(text)
|
|
print(text)
|
|
|
|
|
|
def psql(sql, tuples=False):
|
|
"""Run SQL via docker compose exec psql. Return stdout.
|
|
|
|
tuples=True — режим `-tA` (без заголовков/выравнивания), удобен для
|
|
разбора значений в Python. Для EXPLAIN оставляем обычный вывод.
|
|
"""
|
|
cmd = [
|
|
"docker", "compose", "exec", "-T", "postgres", "psql",
|
|
"-U", "messenger", "-d", "messenger",
|
|
]
|
|
if tuples:
|
|
cmd += ["-tA"]
|
|
cmd += ["-c", sql]
|
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
if result.returncode != 0:
|
|
raise RuntimeError(f"psql error: {result.stderr[:500]}")
|
|
return result.stdout
|
|
|
|
|
|
# ---- Step 1: user + session + chat ------------------------------------------
|
|
log(HEADER)
|
|
log("Step 1: register user and create chat")
|
|
log(HEADER)
|
|
|
|
status, payload, _ = call("POST", "/api/v1/users", body={
|
|
"username": "explain_user",
|
|
"display_name": "EXPLAIN",
|
|
"password": "ExplPass123!",
|
|
})
|
|
if status == 201:
|
|
uid = payload["id"]
|
|
log("Registered new user")
|
|
else:
|
|
cred = base64.b64encode(b"explain_user:ExplPass123!").decode()
|
|
status, payload, _ = call("POST", "/api/v1/auth/sessions",
|
|
headers={"Authorization": "Basic " + cred})
|
|
uid = payload["user"]["id"]
|
|
log(f"User already exists, uid = {uid}")
|
|
|
|
cred = base64.b64encode(b"explain_user:ExplPass123!").decode()
|
|
status, payload, _ = call("POST", "/api/v1/auth/sessions",
|
|
headers={"Authorization": "Basic " + cred})
|
|
token = payload["token"]
|
|
auth = {"Authorization": "Bearer " + token}
|
|
|
|
status, payload, _ = call("POST", "/api/v1/chats",
|
|
body={"title": "explain_test", "member_ids": [uid]},
|
|
headers=auth)
|
|
assert status == 201, f"chat creation failed: {status} {payload}"
|
|
chat_id = payload["id"]
|
|
log(f"chat_id = {chat_id}")
|
|
|
|
# ---- Step 2: bulk-insert 10 000 messages ------------------------------------
|
|
log(HEADER)
|
|
log("Step 2: bulk-insert 10 000 messages via psql")
|
|
log(HEADER)
|
|
|
|
psql(f"""
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT gen_random_uuid(),
|
|
'{chat_id}'::uuid,
|
|
'{uid}'::uuid,
|
|
'msg-' || generate_series(1, 10000),
|
|
now() - (generate_series(1, 10000) || ' seconds')::interval
|
|
""")
|
|
out = psql(f"SELECT count(*) FROM messages WHERE chat_id = '{chat_id}'::uuid", tuples=True)
|
|
log(f"Message count: {out.strip()}")
|
|
|
|
# ---- Step 3: cursor boundary (row 9950) ------------------------------------
|
|
log(HEADER)
|
|
log("Step 3: cursor boundary at row 9950 (late page)")
|
|
log(HEADER)
|
|
|
|
row = psql(f"""
|
|
SELECT id::text, created_at::text FROM messages
|
|
WHERE chat_id = '{chat_id}'::uuid
|
|
ORDER BY created_at ASC, id ASC
|
|
LIMIT 1 OFFSET 9950
|
|
""", tuples=True).strip()
|
|
parts = row.split("|")
|
|
cursor_id = parts[0].strip()
|
|
cursor_ts_raw = parts[1].strip()
|
|
cursor_dt = datetime.fromisoformat(cursor_ts_raw)
|
|
cursor_ts = cursor_dt.isoformat()
|
|
log(f"cursor_id = {cursor_id}")
|
|
log(f"cursor_ts = {cursor_ts}")
|
|
|
|
# ---- Step 4: EXPLAIN ANALYZE — OLD (OR form) --------------------------------
|
|
log(HEADER)
|
|
log("Step 4: EXPLAIN ANALYZE — OLD form (OR cursor)")
|
|
log(HEADER)
|
|
|
|
old_sql = f"""
|
|
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = '{chat_id}'::uuid
|
|
AND (created_at > '{cursor_ts}'::timestamptz
|
|
OR (created_at = '{cursor_ts}'::timestamptz
|
|
AND id > '{cursor_id}'::uuid))
|
|
ORDER BY created_at ASC, id ASC
|
|
LIMIT 51
|
|
"""
|
|
log(psql(old_sql))
|
|
|
|
# ---- Step 5: EXPLAIN ANALYZE — NEW (row-comparison) -------------------------
|
|
log(HEADER)
|
|
log("Step 5: EXPLAIN ANALYZE — NEW form (row-comparison / Index Cond)")
|
|
log(HEADER)
|
|
|
|
new_sql = f"""
|
|
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = '{chat_id}'::uuid
|
|
AND (created_at, id) > ('{cursor_ts}'::timestamptz, '{cursor_id}'::uuid)
|
|
ORDER BY created_at ASC, id ASC
|
|
LIMIT 51
|
|
"""
|
|
log(psql(new_sql))
|
|
|
|
# ---- Step 6: HTTP timing ----------------------------------------------------
|
|
log(HEADER)
|
|
log("Step 6: HTTP timing — late page via API")
|
|
log(HEADER)
|
|
|
|
cursor_value = f"{cursor_ts}|{cursor_id}"
|
|
cursor_encoded = base64.urlsafe_b64encode(cursor_value.encode()).decode()
|
|
log(f"API cursor (base64url): {cursor_encoded[:40]}...")
|
|
|
|
status, payload, elapsed = call(
|
|
"GET",
|
|
f"/api/v1/chats/{chat_id}/messages?limit=100&cursor={cursor_encoded}",
|
|
headers=auth,
|
|
)
|
|
log(f"API response: {status} in {elapsed:.4f}s, {len(payload.get('items', []))} items")
|
|
|
|
# ---- Write output -----------------------------------------------------------
|
|
with open(OUTFILE, "w", encoding="utf-8") as f:
|
|
f.write("\n".join(lines))
|
|
log(f"\nResults written to {OUTFILE}")
|