From 43e59641801fa016c18d6bdf36074abc3058cad4 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 03:50:54 +0300 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20evidence=20=D0=B4=D0=BB=D1=8F=20lab2=20+=20?= =?UTF-8?q?=D0=BA=D1=8D=D1=88=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/auth.py | 5 +- app/main.py | 7 +- app/repositories/chats.py | 3 +- app/repositories/messages.py | 82 ++++++++++++- compose.yaml | 1 + evidence/persistence.txt | Bin 0 -> 380 bytes evidence/track_a_after.json | 28 +++++ evidence/track_a_before.json | 28 +++++ scripts/track_a_bench.py | 224 +++++++++++++++++++++++++++++++++++ 9 files changed, 371 insertions(+), 7 deletions(-) create mode 100644 evidence/persistence.txt create mode 100644 evidence/track_a_after.json create mode 100644 evidence/track_a_before.json create mode 100644 scripts/track_a_bench.py diff --git a/app/auth.py b/app/auth.py index 3ca76bb3..d996abec 100644 --- a/app/auth.py +++ b/app/auth.py @@ -2,6 +2,7 @@ import hashlib import os import secrets from datetime import datetime, timezone +from uuid import UUID import bcrypt from fastapi import Depends, Header, HTTPException @@ -79,4 +80,6 @@ async def revoke_current_session(authorization: str, db: AsyncSession) -> None: if not authorization or not authorization.startswith("Bearer "): raise _unauthorized_bearer() token = authorization[7:] - await redis_client.delete(f"session:{hash_token(token)}") \ No newline at end of file + deleted = await redis_client.delete(f"session:{hash_token(token)}") + if deleted == 0: + raise _unauthorized_bearer() \ No newline at end of file diff --git a/app/main.py b/app/main.py index 7fc02243..0de4c6cd 100644 --- a/app/main.py +++ b/app/main.py @@ -171,10 +171,9 @@ async def login( raise HTTPException( status_code=401, detail="Unauthorized", headers={"WWW-Authenticate": basic_challenge} ) - token = generate_token() expires = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL) token = await create_session(str(user.id)) - await db.commit() + return { "token": token, "token_type": "Bearer", @@ -271,6 +270,7 @@ async def list_messages( chat = await chats_repo.get_if_member(db, chat_id, user.id) if chat is None: raise HTTPException(404, "Chat not found") + parsed = None if cursor is not None: try: @@ -278,11 +278,12 @@ async def list_messages( parsed = (datetime.fromisoformat(c_created), UUID(c_id)) except Exception: raise HTTPException(400, "Invalid cursor") + rows, next_cursor = await messages_repo.list_page(db, chat_id, limit, parsed) + nxt = f"{next_cursor[0].isoformat()}|{next_cursor[1]}" if next_cursor else None return {"items": [_msg_json(m) for m in rows], "next_cursor": nxt} - if __name__ == "__main__": import uvicorn diff --git a/app/repositories/chats.py b/app/repositories/chats.py index 44fe9649..db82b9d1 100644 --- a/app/repositories/chats.py +++ b/app/repositories/chats.py @@ -10,7 +10,6 @@ from ..db.models import Chat, ChatMember, User async def create_atomic( db: AsyncSession, title: str, member_ids: list[UUID] ) -> Chat: - # проверка всех участников found = ( (await db.execute(select(User.id).where(User.id.in_(member_ids)))) .scalars() @@ -22,7 +21,7 @@ async def create_atomic( chat = Chat(title=title) db.add(chat) - await db.flush() # получить chat.id + await db.flush() for uid in member_ids: db.add(ChatMember(chat_id=chat.id, user_id=uid)) diff --git a/app/repositories/messages.py b/app/repositories/messages.py index 81df513a..22b9549c 100644 --- a/app/repositories/messages.py +++ b/app/repositories/messages.py @@ -1,11 +1,52 @@ +import json +import os from datetime import datetime +from types import SimpleNamespace from uuid import UUID +import redis.asyncio as redis from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from ..db.models import Message +redis_client = redis.from_url(os.getenv("REDIS_URL", "redis://redis:6379/0")) +CACHE_TTL = 30 + + +def _serialize(rows: list[Message], next_cursor): + return { + "items": [ + { + "id": str(m.id), + "chat_id": str(m.chat_id), + "sender_id": str(m.sender_id), + "text": m.text, + "created_at": m.created_at.isoformat(), + } + for m in rows + ], + "next_cursor": ( + [next_cursor[0].isoformat(), str(next_cursor[1])] if next_cursor else None + ), + } + + +def _deserialize(data: dict): + items = [ + SimpleNamespace( + id=UUID(row["id"]), + chat_id=UUID(row["chat_id"]), + sender_id=UUID(row["sender_id"]), + text=row["text"], + created_at=datetime.fromisoformat(row["created_at"]), + ) + for row in data["items"] + ] + nc = data.get("next_cursor") + next_cursor = (datetime.fromisoformat(nc[0]), UUID(nc[1])) if nc else None + return items, next_cursor + async def create( db: AsyncSession, chat_id: UUID, sender_id: UUID, text: str @@ -17,6 +58,15 @@ async def create( return msg +async def create_and_invalidate( + db: AsyncSession, chat_id: UUID, sender_id: UUID, text: str +) -> Message: + msg = await create(db, chat_id, sender_id, text) + keys = [f"chat:{chat_id}:first_page:{n}" for n in (10, 20, 50)] + await redis_client.delete(*keys) + return msg + + async def list_page( db: AsyncSession, chat_id: UUID, @@ -41,4 +91,34 @@ async def list_page( rows = rows[:limit] last = rows[-1] next_cursor = (last.created_at, last.id) - return rows, next_cursor \ No newline at end of file + return rows, next_cursor + + +async def list_page_cached( + db: AsyncSession, + chat_id: UUID, + limit: int, + cursor: tuple[datetime, UUID] | None, +) -> tuple[list, tuple[datetime, UUID] | None, bool]: + """Возвращает (rows, next_cursor, cached).""" + use_cache = cursor is None and limit <= 50 + key = f"chat:{chat_id}:first_page:{limit}" + + if use_cache: + raw = await redis_client.get(key) + if raw is not None: + try: + rows, next_cursor = _deserialize(json.loads(raw)) + return rows, next_cursor, True + except Exception: + pass # повреждённый кэш — игнорируем и идём в БД + + rows, next_cursor = await list_page(db, chat_id, limit, cursor) + + if use_cache: + try: + await redis_client.setex(key, CACHE_TTL, json.dumps(_serialize(rows, next_cursor))) + except Exception: + pass # Redis недоступен — не падаем, просто без кэша + + return rows, next_cursor, False \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index 514566a4..50895e0f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,7 @@ services: postgres: image: postgres:16.2-alpine + command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements"] environment: POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 0000000000000000000000000000000000000000..c25cebacdd766ee09d14e1573471e896f0a4fc5d GIT binary patch literal 380 zcmZ{gK@Ng25Jmsm#5?o^96(ntOkA3H0#Z_AMdi>HGz5_j-R;(@cEI#A`5 zYvQgVQuC6dwYsoMA6WrbroDE$>ZBX%0QaA#wUarXmTsOdjqYgodM{W(x4|&$ srwO+n%ySj_8MPN8o7mfPFNq+Tl(35PAC-I>EF^UKdQS&(EtDd literal 0 HcmV?d00001 diff --git a/evidence/track_a_after.json b/evidence/track_a_after.json new file mode 100644 index 00000000..a56ef422 --- /dev/null +++ b/evidence/track_a_after.json @@ -0,0 +1,28 @@ +{ + "requests": 300, + "successful": 300, + "errors": 0, + "elapsed_s": 7.643, + "rps": 39.25, + "latency_ms": { + "p50": 30.019, + "p95": 35.281, + "p99": 38.614, + "min": 4.795, + "max": 41.083, + "mean": 25.476 + }, + "pg_stat": { + "total_calls_to_messages": 1, + "details": [ + { + "calls": 1, + "rows": 51, + "total_ms": 0.043902000000000004, + "mean_ms": 0.043902000000000004 + } + ] + }, + "history": 1000, + "chat_id": "0e5fb96c-b17d-4a31-b2cf-dc234db24711" +} \ No newline at end of file diff --git a/evidence/track_a_before.json b/evidence/track_a_before.json new file mode 100644 index 00000000..9ebd9a1c --- /dev/null +++ b/evidence/track_a_before.json @@ -0,0 +1,28 @@ +{ + "requests": 300, + "successful": 300, + "errors": 0, + "elapsed_s": 7.83, + "rps": 38.31, + "latency_ms": { + "p50": 30.395, + "p95": 33.586, + "p99": 36.868, + "min": 5.165, + "max": 50.705, + "mean": 26.099 + }, + "pg_stat": { + "total_calls_to_messages": 301, + "details": [ + { + "calls": 301, + "rows": 15351, + "total_ms": 18.329712000000015, + "mean_ms": 0.060896053156146185 + } + ] + }, + "history": 1000, + "chat_id": "e339b648-9722-47fa-871e-15c99a00fc5b" +} \ No newline at end of file diff --git a/scripts/track_a_bench.py b/scripts/track_a_bench.py new file mode 100644 index 00000000..bfca7523 --- /dev/null +++ b/scripts/track_a_bench.py @@ -0,0 +1,224 @@ +#!/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() \ No newline at end of file