Replace the in-memory store with PostgreSQL 18.6: SQLAlchemy 2.1 async engine on asyncpg, alembic migrations with an async env.py, repos on AsyncSession and services that own the transaction. Username uniqueness is enforced by the database (ON CONFLICT), a chat and its members are created in one transaction, member order is kept in chat_members.position, and a composite foreign key makes the sender a member of the chat. History and chat lists use keyset pagination on (chat_id, created_at, id) and (user_id, created_at, chat_id). The compose stand runs postgres, a one-shot migrate service on the API image and the API. All Postgres addresses come from .env without literals; only the 127.0.0.1 publish address stays literal. make run_dev starts Postgres and applies migrations before the server, and make env_setup generates a random Postgres password. Unavailable PostgreSQL yields 503: get_db_session bounds the request with asyncio and classifies driver errors. BoundedCloseConnection limits asyncpg's graceful close, which otherwise waited without limit on a frozen server and held the request until it was resumed. Add scripts/restart.sh for make test-persistence and make test_isolated, which runs the image on a throwaway test database. Remove make run_no_compose. X-User-Id identification is unchanged until the session-auth change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
846 lines
31 KiB
Python
846 lines
31 KiB
Python
"""Разложение стоимости запроса внутри процесса: диагностика трека 1.
|
||
|
||
Запуск — `make bench_inproc BENCH_LABEL=<метка>`. На хосте скрипт собирает
|
||
образ стенда, запускает себя в контейнере и пишет
|
||
`evidence/profile/<метка>.txt`; здесь только stdlib. В контейнере
|
||
(`--child`) он импортирует приложение и вызывает его как ASGI, без
|
||
uvicorn и сети. Числа объясняют, куда уходит время запроса; пропускную
|
||
способность и задержки меряет только `make bench`. Методика — design
|
||
change `perf-bottleneck-fix`, граница — `.claude/rules/bench.md`.
|
||
"""
|
||
|
||
import argparse
|
||
import asyncio
|
||
import cProfile
|
||
import functools
|
||
import importlib.metadata
|
||
import json
|
||
import os
|
||
import platform
|
||
import pstats
|
||
import random
|
||
import re
|
||
import statistics
|
||
import subprocess
|
||
import sys
|
||
import time
|
||
from collections.abc import Awaitable, Callable
|
||
from datetime import UTC, datetime, timedelta, timezone
|
||
from pathlib import Path
|
||
from typing import Annotated, Any
|
||
from uuid import UUID
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
SCRIPT = Path(__file__).resolve()
|
||
APP_IMAGE = "mtusi-messenger:bench"
|
||
CONTAINER = "mtusi-bench-inproc"
|
||
RESULT_MARK = "INPROC_RESULT "
|
||
# незакоммиченные изменения считаются только по тому, что попадает в замер
|
||
APP_FILES = (
|
||
"src",
|
||
"Dockerfile",
|
||
"pyproject.toml",
|
||
"uv.lock",
|
||
".dockerignore",
|
||
".python-version",
|
||
"bench/inproc.py",
|
||
)
|
||
|
||
# эталон формата времени: запись, которой оно сериализовалось до правки.
|
||
# Литерал, а не константа из src: расхождение с кодом должно быть видно
|
||
REFERENCE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
|
||
RFC3339_RE = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z")
|
||
|
||
SEED = 20260930
|
||
SIZE = 20_000
|
||
TEXT_LENGTH = 64
|
||
PAGE_LIMIT = 50
|
||
CURSOR_POOL = 300
|
||
# запусков процесса на каждый режим: от запуска к запуску время запроса
|
||
# гуляет на 1–3%, внутри запуска — меньше
|
||
LAUNCHES = 3
|
||
# режим логов: уровень и флаги процесса. INFO — как на стенде; INFO_NULL —
|
||
# записи собираются, но stdout процесса смотрит в /dev/null
|
||
MODES: dict[str, tuple[str, list[str]]] = {
|
||
"INFO": ("INFO", []),
|
||
"INFO_NULL": ("INFO", ["--null-stdout"]),
|
||
"WARNING": ("WARNING", []),
|
||
}
|
||
REPEATS = 5
|
||
REQUESTS = 3000
|
||
PART_ROUNDS = 2000
|
||
VALUE_ROUNDS = 100_000
|
||
PROFILE_REQUESTS = 2000
|
||
PROFILE_TOP = 15
|
||
FORMAT_SAMPLES = 20_000
|
||
TIME_NAMES = ("to_rfc3339", "strftime", "isoformat", "astimezone")
|
||
|
||
|
||
class InprocError(Exception):
|
||
"""Диагностика не может продолжать: сообщение для человека."""
|
||
|
||
|
||
def spread(values: list[float]) -> dict[str, float]:
|
||
return {
|
||
"median": round(statistics.median(values), 3),
|
||
"min": round(min(values), 3),
|
||
"max": round(max(values), 3),
|
||
}
|
||
|
||
|
||
# ------------------------------------------------------------- в контейнере
|
||
|
||
|
||
def reference_rfc3339(value: datetime, fmt: str) -> str:
|
||
return value.astimezone(UTC).strftime(fmt)
|
||
|
||
|
||
def format_values() -> list[datetime]:
|
||
"""Случайные значения 1970–2100 и граничные случаи формата."""
|
||
rng = random.Random(SEED)
|
||
start = datetime(1970, 1, 1, tzinfo=UTC)
|
||
span = int((datetime(2100, 1, 1, tzinfo=UTC) - start).total_seconds())
|
||
values = [
|
||
start
|
||
+ timedelta(
|
||
seconds=rng.randrange(span),
|
||
microseconds=rng.randrange(1_000_000),
|
||
)
|
||
for _ in range(FORMAT_SAMPLES)
|
||
]
|
||
moment = datetime(2026, 9, 30, 12, 0, 0, tzinfo=UTC)
|
||
values += [moment.replace(microsecond=m) for m in (0, 1, 999_999)]
|
||
values += [
|
||
start,
|
||
datetime(2026, 12, 31, 23, 59, 59, 999_999, tzinfo=UTC),
|
||
datetime(2028, 2, 29, 0, 0, 0, tzinfo=UTC),
|
||
]
|
||
# другой часовой пояс: после приведения к UTC меняются день и год
|
||
for hours in (3, -8):
|
||
zone = timezone(timedelta(hours=hours))
|
||
values += [
|
||
datetime(2026, 12, 31, 23, 30, 0, 0, tzinfo=zone),
|
||
datetime(2027, 1, 1, 1, 30, 0, 123, tzinfo=zone),
|
||
]
|
||
return values
|
||
|
||
|
||
def check_format(
|
||
to_text: Callable[[datetime], str],
|
||
from_text: Callable[[str], datetime],
|
||
fmt: str,
|
||
) -> dict[str, Any]:
|
||
"""Текущий форматтер против эталона; первое расхождение — в ответ."""
|
||
values = format_values()
|
||
for value in values:
|
||
expected = reference_rfc3339(value, fmt)
|
||
got = to_text(value)
|
||
problem = None
|
||
if got != expected:
|
||
problem = "differs from reference"
|
||
elif not RFC3339_RE.fullmatch(got):
|
||
problem = "not YYYY-MM-DDTHH:MM:SS.ffffffZ"
|
||
elif from_text(got) != value:
|
||
problem = "does not parse back to the same instant"
|
||
if problem:
|
||
return {
|
||
"checked": len(values),
|
||
"mismatch": {
|
||
"problem": problem,
|
||
"value": value.isoformat(),
|
||
"expected": expected,
|
||
"got": got,
|
||
},
|
||
}
|
||
return {"checked": len(values), "mismatch": None}
|
||
|
||
|
||
class Probe:
|
||
"""Прямые ASGI-вызовы приложения."""
|
||
|
||
def __init__(self, app: Any) -> None:
|
||
self.app = app
|
||
|
||
@staticmethod
|
||
def headers(
|
||
user: str | None,
|
||
body: bytes = b"",
|
||
) -> list[tuple[bytes, bytes]]:
|
||
result = [(b"host", b"inproc")]
|
||
if body:
|
||
result.append((b"content-type", b"application/json"))
|
||
result.append((b"content-length", str(len(body)).encode()))
|
||
if user:
|
||
result.append((b"x-user-id", user.encode()))
|
||
return result
|
||
|
||
async def request(
|
||
self,
|
||
method: str,
|
||
path: str,
|
||
headers: list[tuple[bytes, bytes]],
|
||
*,
|
||
query: bytes = b"",
|
||
body: bytes = b"",
|
||
) -> tuple[int, bytes]:
|
||
scope = {
|
||
"type": "http",
|
||
"asgi": {"version": "3.0"},
|
||
"http_version": "1.1",
|
||
"method": method,
|
||
"scheme": "http",
|
||
"path": path,
|
||
"raw_path": path.encode(),
|
||
"query_string": query,
|
||
"root_path": "",
|
||
"headers": headers,
|
||
"client": ("127.0.0.1", 1),
|
||
"server": ("127.0.0.1", 8080),
|
||
}
|
||
messages: list[dict[str, Any]] = []
|
||
|
||
async def receive() -> dict[str, Any]:
|
||
return {"type": "http.request", "body": body, "more_body": False}
|
||
|
||
async def send(message: dict[str, Any]) -> None:
|
||
messages.append(message)
|
||
|
||
await self.app(scope, receive, send)
|
||
return (
|
||
messages[0]["status"],
|
||
b"".join(m.get("body", b"") for m in messages[1:]),
|
||
)
|
||
|
||
async def create(
|
||
self,
|
||
path: str,
|
||
payload: dict[str, Any],
|
||
user: str | None = None,
|
||
) -> dict[str, Any]:
|
||
body = json.dumps(payload).encode()
|
||
status, raw = await self.request(
|
||
"POST",
|
||
path,
|
||
self.headers(user, body),
|
||
body=body,
|
||
)
|
||
if status != 201:
|
||
raise InprocError(f"POST {path}: {status} {raw[:200]!r}")
|
||
return json.loads(raw)
|
||
|
||
|
||
async def repeat(
|
||
fn: Callable[[], Awaitable[None]],
|
||
count: int,
|
||
) -> dict[str, Any]:
|
||
"""Прогрев и REPEATS повторов; время и CPU процесса на один запрос."""
|
||
await fn()
|
||
walls, cpus = [], []
|
||
for _ in range(REPEATS):
|
||
wall, cpu = time.perf_counter(), time.process_time()
|
||
await fn()
|
||
walls.append((time.perf_counter() - wall) / count * 1e6)
|
||
cpus.append((time.process_time() - cpu) / count * 1e6)
|
||
return {"wall_us": spread(walls), "cpu_us": spread(cpus)}
|
||
|
||
|
||
def repeat_sync(fn: Callable[[], Any], count: int) -> dict[str, float]:
|
||
for _ in range(count):
|
||
fn()
|
||
walls = []
|
||
for _ in range(REPEATS):
|
||
wall = time.perf_counter()
|
||
for _ in range(count):
|
||
fn()
|
||
walls.append((time.perf_counter() - wall) / count * 1e6)
|
||
return spread(walls)
|
||
|
||
|
||
def function_label(file: str, line: int, name: str) -> str:
|
||
if file == "~":
|
||
return name
|
||
if "site-packages/" in file:
|
||
short = file.split("site-packages/", 1)[1]
|
||
elif file.startswith(os.getcwd() + "/"):
|
||
short = file[len(os.getcwd()) + 1 :]
|
||
else:
|
||
short = "/".join(file.split("/")[-2:])
|
||
return f"{short}:{line}({name})"
|
||
|
||
|
||
async def profile(
|
||
fn: Callable[[], Awaitable[None]],
|
||
count: int,
|
||
) -> dict[str, Any]:
|
||
"""cProfile: число вызовов и порядок функций, не доли времени."""
|
||
profiler = cProfile.Profile()
|
||
profiler.enable()
|
||
await fn()
|
||
profiler.disable()
|
||
rows = []
|
||
for (file, line, name), (_, calls, own, total, _) in pstats.Stats(
|
||
profiler,
|
||
).stats.items():
|
||
if file == __file__:
|
||
continue
|
||
rows.append(
|
||
{
|
||
"function": function_label(file, line, name),
|
||
"name": name,
|
||
"calls": round(calls / count, 2),
|
||
"own_us": round(own / count * 1e6, 2),
|
||
"total_us": round(total / count * 1e6, 2),
|
||
},
|
||
)
|
||
rows.sort(key=lambda row: row["own_us"], reverse=True)
|
||
return {
|
||
"top": rows[:PROFILE_TOP],
|
||
"time": [
|
||
row for row in rows if any(n in row["name"] for n in TIME_NAMES)
|
||
],
|
||
}
|
||
|
||
|
||
async def measure(args: argparse.Namespace) -> dict[str, Any]:
|
||
# приложение импортируется только здесь: на хосте скрипт — stdlib
|
||
sys.path.insert(0, os.getcwd())
|
||
from pydantic import PlainSerializer
|
||
from src.infra.memory import MemoryStore
|
||
|
||
from src.api.schemas import Message, MessagePage
|
||
from src.core.services import MessageService
|
||
from src.main import app
|
||
from src.utils.utc import from_rfc3339, to_rfc3339
|
||
|
||
result: dict[str, Any] = {
|
||
"level": os.environ.get("APP__LOG_LEVEL", "INFO"),
|
||
"versions": {
|
||
"python": platform.python_version(),
|
||
**{
|
||
name: importlib.metadata.version(name)
|
||
for name in (
|
||
"fastapi",
|
||
"starlette",
|
||
"pydantic",
|
||
"pydantic_core",
|
||
"structlog",
|
||
)
|
||
},
|
||
},
|
||
"platform": {
|
||
"machine": platform.machine(),
|
||
"libc": " ".join(platform.libc_ver()),
|
||
},
|
||
}
|
||
|
||
def reference(value: datetime) -> str:
|
||
return reference_rfc3339(value, args.reference_format)
|
||
|
||
class ReferenceMessage(Message):
|
||
created_at: Annotated[
|
||
datetime,
|
||
PlainSerializer(reference, return_type=str),
|
||
]
|
||
|
||
class ReferencePage(MessagePage):
|
||
items: list[ReferenceMessage]
|
||
|
||
class NativeMessage(Message):
|
||
created_at: datetime
|
||
|
||
class NativePage(MessagePage):
|
||
items: list[NativeMessage]
|
||
|
||
if args.parts:
|
||
result["format"] = check_format(
|
||
to_rfc3339,
|
||
from_rfc3339,
|
||
args.reference_format,
|
||
)
|
||
if result["format"]["mismatch"]:
|
||
return result
|
||
|
||
# lifespan при прямых ASGI-вызовах не выполняется
|
||
app.state.store = MemoryStore()
|
||
probe = Probe(app)
|
||
user = (
|
||
await probe.create(
|
||
"/api/v1/users",
|
||
{"username": "inproc_reader", "display_name": "Reader"},
|
||
)
|
||
)["id"]
|
||
chats = [
|
||
(
|
||
await probe.create(
|
||
"/api/v1/chats",
|
||
{"title": title, "member_ids": [user]},
|
||
user,
|
||
)
|
||
)["id"]
|
||
for title in ("read", "write")
|
||
]
|
||
read_path, write_path = (f"/api/v1/chats/{c}/messages" for c in chats)
|
||
body = json.dumps({"text": "x" * TEXT_LENGTH}).encode()
|
||
get_headers = probe.headers(user)
|
||
post_headers = probe.headers(user, body)
|
||
|
||
async def post(path: str, count: int) -> None:
|
||
for _ in range(count):
|
||
status, _ = await probe.request(
|
||
"POST",
|
||
path,
|
||
post_headers,
|
||
body=body,
|
||
)
|
||
if status != 201:
|
||
raise InprocError(f"POST {path}: {status}")
|
||
|
||
await post(read_path, SIZE)
|
||
cursors: list[str] = []
|
||
query = f"limit={PAGE_LIMIT}".encode()
|
||
while len(cursors) < CURSOR_POOL:
|
||
status, raw = await probe.request(
|
||
"GET",
|
||
read_path,
|
||
get_headers,
|
||
query=query,
|
||
)
|
||
cursor = json.loads(raw)["next_cursor"]
|
||
if status != 200 or cursor is None:
|
||
raise InprocError(f"cursor walk: {status}, next_cursor {cursor}")
|
||
cursors.append(cursor)
|
||
query = f"limit={PAGE_LIMIT}&cursor={cursor}".encode()
|
||
rng = random.Random(SEED)
|
||
queries = [
|
||
f"limit={PAGE_LIMIT}&cursor={rng.choice(cursors)}".encode()
|
||
for _ in range(REQUESTS)
|
||
]
|
||
|
||
async def reads(count: int = REQUESTS) -> None:
|
||
for query in queries[:count]:
|
||
status, _ = await probe.request(
|
||
"GET",
|
||
read_path,
|
||
get_headers,
|
||
query=query,
|
||
)
|
||
if status != 200:
|
||
raise InprocError(f"GET {read_path}: {status}")
|
||
|
||
async def writes(count: int = REQUESTS) -> None:
|
||
await post(write_path, count)
|
||
|
||
result["requests"] = {
|
||
"read": await repeat(reads, REQUESTS),
|
||
"write": await repeat(writes, REQUESTS),
|
||
}
|
||
if not args.parts:
|
||
return result
|
||
|
||
dto = await MessageService(store=app.state.store).list_messages(
|
||
chat_id=UUID(chats[0]),
|
||
user_id=UUID(user),
|
||
limit=PAGE_LIMIT,
|
||
cursor=cursors[len(cursors) // 2],
|
||
)
|
||
current = MessagePage.model_validate(dto)
|
||
reference_page = ReferencePage.model_validate(dto)
|
||
native = NativePage.model_validate(dto)
|
||
if current.model_dump_json() != reference_page.model_dump_json():
|
||
result["format"]["mismatch"] = {
|
||
"problem": "page JSON differs from reference",
|
||
"value": "page of messages",
|
||
"expected": reference_page.model_dump_json()[:200],
|
||
"got": current.model_dump_json()[:200],
|
||
}
|
||
return result
|
||
moment = dto.items[0].created_at
|
||
result["parts"] = {
|
||
"dto_to_model": repeat_sync(
|
||
functools.partial(MessagePage.model_validate, dto),
|
||
PART_ROUNDS,
|
||
),
|
||
"json_current": repeat_sync(current.model_dump_json, PART_ROUNDS),
|
||
"json_reference": repeat_sync(
|
||
reference_page.model_dump_json,
|
||
PART_ROUNDS,
|
||
),
|
||
"json_native": repeat_sync(native.model_dump_json, PART_ROUNDS),
|
||
"value_current": repeat_sync(
|
||
functools.partial(to_rfc3339, moment),
|
||
VALUE_ROUNDS,
|
||
),
|
||
"value_reference": repeat_sync(
|
||
functools.partial(reference, moment),
|
||
VALUE_ROUNDS,
|
||
),
|
||
}
|
||
result["profile"] = {
|
||
"read": await profile(
|
||
functools.partial(reads, PROFILE_REQUESTS),
|
||
PROFILE_REQUESTS,
|
||
),
|
||
"write": await profile(
|
||
functools.partial(writes, PROFILE_REQUESTS),
|
||
PROFILE_REQUESTS,
|
||
),
|
||
}
|
||
return result
|
||
|
||
|
||
def child(args: argparse.Namespace) -> int:
|
||
"""Логи приложения идут в stdout, результат — одной строкой в stderr."""
|
||
if args.null_stdout:
|
||
# до импорта приложения: его логгер берёт sys.stdout при настройке
|
||
os.dup2(os.open(os.devnull, os.O_WRONLY), 1)
|
||
result = asyncio.run(measure(args))
|
||
print(RESULT_MARK + json.dumps(result), file=sys.stderr, flush=True)
|
||
return 3 if (result.get("format") or {}).get("mismatch") else 0
|
||
|
||
|
||
# ------------------------------------------------------------------ на хосте
|
||
|
||
|
||
def sh(*args: str, check: bool = True) -> str:
|
||
proc = subprocess.run(
|
||
args,
|
||
capture_output=True,
|
||
text=True,
|
||
check=False,
|
||
env={**os.environ, "LC_ALL": "C"},
|
||
)
|
||
if check and proc.returncode != 0:
|
||
raise InprocError(
|
||
f"{' '.join(args[:4])}…: exit {proc.returncode}\n"
|
||
f"{(proc.stdout + proc.stderr)[-2000:]}",
|
||
)
|
||
return proc.stdout.strip()
|
||
|
||
|
||
def run_child(level: str, extra: list[str]) -> dict[str, Any]:
|
||
"""Один запуск процесса в контейнере.
|
||
|
||
Контейнер отсоединён (`-d`), как контейнер стенда: с подключённым
|
||
клиентом docker каждая строка лога обходится процессу заметно дороже.
|
||
"""
|
||
sh(
|
||
"docker",
|
||
"run",
|
||
"-d",
|
||
"--name",
|
||
CONTAINER,
|
||
"--cpus=1",
|
||
"--memory=2g",
|
||
"--memory-swap=2g",
|
||
"--network=none",
|
||
"--no-healthcheck",
|
||
"-e",
|
||
f"APP__LOG_LEVEL={level}",
|
||
"-v",
|
||
f"{SCRIPT}:/bench/inproc.py:ro",
|
||
"--entrypoint",
|
||
"python",
|
||
APP_IMAGE,
|
||
"/bench/inproc.py",
|
||
"--child",
|
||
*extra,
|
||
)
|
||
try:
|
||
code = sh("docker", "wait", CONTAINER)
|
||
proc = subprocess.run(
|
||
["docker", "logs", CONTAINER],
|
||
stdout=subprocess.DEVNULL,
|
||
stderr=subprocess.PIPE,
|
||
text=True,
|
||
check=False,
|
||
)
|
||
finally:
|
||
sh("docker", "rm", "-f", CONTAINER, check=False)
|
||
for line in reversed(proc.stderr.splitlines()):
|
||
if line.startswith(RESULT_MARK):
|
||
return json.loads(line[len(RESULT_MARK) :])
|
||
raise InprocError(f"child {level}: exit {code}\n{proc.stderr[-2000:]}")
|
||
|
||
|
||
def passport(argv: list[str], label: str) -> dict[str, Any]:
|
||
docker = json.loads(sh("docker", "info", "--format", "{{json .}}"))
|
||
machine = platform.machine()
|
||
system = platform.system()
|
||
if system == "Darwin":
|
||
system = f"macOS {platform.mac_ver()[0]}"
|
||
machine = sh("sysctl", "-n", "machdep.cpu.brand_string", check=False)
|
||
return {
|
||
"label": label,
|
||
"date": datetime.now(UTC).isoformat(timespec="seconds"),
|
||
"command": " ".join(["uv run python bench/inproc.py", *argv]),
|
||
"commit": sh("git", "-C", str(ROOT), "rev-parse", "--short", "HEAD"),
|
||
"dirty": bool(
|
||
sh(
|
||
"git",
|
||
"-C",
|
||
str(ROOT),
|
||
"status",
|
||
"--porcelain",
|
||
"--",
|
||
*APP_FILES,
|
||
),
|
||
),
|
||
"image": sh("docker", "image", "inspect", "-f", "{{.Id}}", APP_IMAGE),
|
||
"host": f"{machine}, {system}",
|
||
"docker": (
|
||
f"Docker {docker.get('ServerVersion')}, VM "
|
||
f"{docker.get('NCPU')} CPU"
|
||
),
|
||
}
|
||
|
||
|
||
def cell(value: dict[str, float], digits: int = 1) -> str:
|
||
return (
|
||
f"{value['median']:.{digits}f} "
|
||
f"({value['min']:.{digits}f}–{value['max']:.{digits}f})"
|
||
)
|
||
|
||
|
||
def across(
|
||
launches: list[dict[str, Any]],
|
||
scenario: str,
|
||
key: str = "wall_us",
|
||
) -> dict[str, float]:
|
||
"""Медианы запусков процесса: их медиана и разброс."""
|
||
return spread(
|
||
[run["requests"][scenario][key]["median"] for run in launches],
|
||
)
|
||
|
||
|
||
def render(
|
||
meta: dict[str, Any],
|
||
launches: dict[str, list[dict[str, Any]]],
|
||
) -> str:
|
||
info = launches["INFO"][0]
|
||
versions = info["versions"]
|
||
parts = info["parts"]
|
||
native = parts["json_native"]["median"]
|
||
current_cost = parts["json_current"]["median"] - native
|
||
reference_cost = parts["json_reference"]["median"] - native
|
||
# медианы по запускам: [режим][сценарий]
|
||
time = {
|
||
mode: {
|
||
scenario: across(launches[mode], scenario)
|
||
for scenario in ("read", "write")
|
||
}
|
||
for mode in MODES
|
||
}
|
||
total = {s: time["INFO"][s]["median"] for s in ("read", "write")}
|
||
log_build = {
|
||
s: time["INFO_NULL"][s]["median"] - time["WARNING"][s]["median"]
|
||
for s in ("read", "write")
|
||
}
|
||
log_write = {
|
||
s: total[s] - time["INFO_NULL"][s]["median"] for s in ("read", "write")
|
||
}
|
||
model = parts["dto_to_model"]["median"]
|
||
rest = time["WARNING"]["read"]["median"] - model - native - current_cost
|
||
lines = [
|
||
f"# Разложение стоимости запроса: метка `{meta['label']}`",
|
||
"",
|
||
f"- Дата: {meta['date']}",
|
||
f"- Commit: `{meta['commit']}`, незакоммиченные изменения "
|
||
f"приложения: {'есть' if meta['dirty'] else 'нет'}",
|
||
f"- Команда: `{meta['command']}`",
|
||
f"- Образ: `{APP_IMAGE}` {meta['image'][:19]}…; "
|
||
+ ", ".join(f"{name} {ver}" for name, ver in versions.items())
|
||
+ f"; {info['platform']['libc']}, {info['platform']['machine']}",
|
||
"- Контейнер: `--cpus=1 --memory=2g --network=none`, отсоединён "
|
||
f"(`docker run -d`), как контейнер стенда; хост: {meta['host']}; "
|
||
f"{meta['docker']}",
|
||
f"- Данные: {SIZE} сообщений по {TEXT_LENGTH} символа, страница "
|
||
f"{PAGE_LIMIT}, зерно {SEED}",
|
||
"- Приложение вызывается в том же процессе как ASGI, без uvicorn "
|
||
"и сети. Это объяснение, куда уходит время запроса, а не замер "
|
||
"стенда: пропускную способность и задержки меряет `make bench`.",
|
||
f"- Время на запрос: {LAUNCHES} запуска процесса на каждый режим "
|
||
f"логов, в запуске — медиана {REPEATS} повторов по {REQUESTS} "
|
||
"запросов; значение — медиана (min–max) по запускам, мкс.",
|
||
"- Режимы логов: INFO — как на стенде, stdout контейнера; INFO с "
|
||
"stdout процесса в `/dev/null` — записи собираются, но не "
|
||
"пишутся в контейнер; WARNING — записи INFO отбрасываются.",
|
||
f"- Части ответа: один запуск, медиана (min–max) {REPEATS} "
|
||
"повторов, мкс. Варианты сравниваются внутри одного процесса.",
|
||
"",
|
||
"## Формат времени",
|
||
"",
|
||
f"Значений сверено с эталоном `{REFERENCE_FORMAT}`: "
|
||
f"{info['format']['checked']}; расхождений нет, обратный разбор "
|
||
"совпал, JSON страницы совпал с эталонным.",
|
||
"",
|
||
"## Сводка: из чего складывается запрос, мкс",
|
||
"",
|
||
"| Часть | read | write |",
|
||
"|---|---|---|",
|
||
"| Всего внутри приложения, логи INFO | "
|
||
f"{total['read']:.1f} | {total['write']:.1f} |",
|
||
"| логи: запись в stdout контейнера | "
|
||
f"{log_write['read']:.1f} | {log_write['write']:.1f} |",
|
||
"| логи: сборка записи (structlog, JSON) | "
|
||
f"{log_build['read']:.1f} | {log_build['write']:.1f} |",
|
||
f"| форматирование времени в ответе | {current_cost:.1f} | — |",
|
||
f"| JSON ответа без форматирования времени | {native:.1f} | — |",
|
||
f"| DTO → модель ответа | {model:.1f} | — |",
|
||
"| остальное: маршрутизация, зависимости, сервис | "
|
||
f"{rest:.1f} | {time['WARNING']['write']['median']:.1f} |",
|
||
"",
|
||
"Строки про логи и «остальное» — разности медиан разных запусков; "
|
||
"части ответа измерены напрямую.",
|
||
"",
|
||
"## Время на запрос",
|
||
"",
|
||
"| Сценарий | Логи INFO | Логи INFO, stdout в /dev/null | "
|
||
"Логи WARNING |",
|
||
"|---|---|---|---|",
|
||
]
|
||
for scenario in ("read", "write"):
|
||
lines.append(
|
||
f"| {scenario} | "
|
||
+ " | ".join(cell(time[mode][scenario]) for mode in MODES)
|
||
+ " |",
|
||
)
|
||
lines += [
|
||
"",
|
||
"CPU процесса на запрос при логах INFO: "
|
||
+ ", ".join(
|
||
f"{name} {cell(across(launches['INFO'], name, 'cpu_us'))}"
|
||
for name in ("read", "write")
|
||
)
|
||
+ ".",
|
||
"",
|
||
f"## Построение ответа страницы ({PAGE_LIMIT} сообщений)",
|
||
"",
|
||
"| Шаг | мкс |",
|
||
"|---|---|",
|
||
f"| DTO → модель ответа | {cell(parts['dto_to_model'])} |",
|
||
f"| модель → JSON, текущий форматтер | "
|
||
f"{cell(parts['json_current'])} |",
|
||
f"| модель → JSON, эталонный `strftime` | "
|
||
f"{cell(parts['json_reference'])} |",
|
||
f"| модель → JSON, без Python-сериализатора времени | "
|
||
f"{cell(parts['json_native'])} |",
|
||
"| цена форматирования времени: текущий − без сериализатора | "
|
||
f"{current_cost:.1f} |",
|
||
f"| то же для эталонного `strftime` | {reference_cost:.1f} |",
|
||
f"| одно значение, текущий форматтер | "
|
||
f"{cell(parts['value_current'], 3)} |",
|
||
f"| одно значение, эталонный `strftime` | "
|
||
f"{cell(parts['value_reference'], 3)} |",
|
||
"",
|
||
f"## Профиль вызовов (cProfile, {PROFILE_REQUESTS} запросов)",
|
||
"",
|
||
"cProfile завышает цену кода с большим числом мелких вызовов: "
|
||
"отсюда берутся число вызовов и порядок функций, не доли времени.",
|
||
]
|
||
for scenario in ("read", "write"):
|
||
data = info["profile"][scenario]
|
||
lines += [
|
||
"",
|
||
f"### {scenario}: форматирование времени",
|
||
"",
|
||
"| Функция | Вызовов на запрос | Своё время, мкс | "
|
||
"С вложенными, мкс |",
|
||
"|---|---|---|---|",
|
||
]
|
||
lines += [profile_row(row) for row in data["time"]]
|
||
lines += [
|
||
"",
|
||
f"### {scenario}: {PROFILE_TOP} функций по своему времени",
|
||
"",
|
||
"| Функция | Вызовов на запрос | Своё время, мкс | "
|
||
"С вложенными, мкс |",
|
||
"|---|---|---|---|",
|
||
]
|
||
lines += [profile_row(row) for row in data["top"]]
|
||
return "\n".join(lines) + "\n"
|
||
|
||
|
||
def profile_row(row: dict[str, Any]) -> str:
|
||
return (
|
||
f"| `{row['function']}` | {row['calls']:g} | "
|
||
f"{row['own_us']:.1f} | {row['total_us']:.1f} |"
|
||
)
|
||
|
||
|
||
def host(args: argparse.Namespace, argv: list[str]) -> int:
|
||
if not re.fullmatch(r"[A-Za-z0-9._-]+", args.label or ""):
|
||
print(f"bad label {args.label!r}: use [A-Za-z0-9._-]", flush=True)
|
||
return 2
|
||
out = ROOT / "evidence" / "profile" / f"{args.label}.txt"
|
||
if out.exists():
|
||
print(f"{out.relative_to(ROOT)} already exists", flush=True)
|
||
return 2
|
||
extra = ["--reference-format", args.reference_format]
|
||
names = sh("docker", "ps", "-a", "--format", "{{.Names}}", check=False)
|
||
if CONTAINER in names.split():
|
||
# чужой запуск не трогаем: уборка ниже — только за собой
|
||
print(
|
||
f"container {CONTAINER} already exists: another run is going "
|
||
f"or a crashed one left it; docker rm -f {CONTAINER}",
|
||
flush=True,
|
||
)
|
||
return 1
|
||
launches: dict[str, list[dict[str, Any]]] = {mode: [] for mode in MODES}
|
||
try:
|
||
sh("docker", "build", "-q", "-t", APP_IMAGE, str(ROOT))
|
||
meta = passport(argv, args.label)
|
||
print(f"image {APP_IMAGE} built, commit {meta['commit']}", flush=True)
|
||
# режимы чередуются: дрейф хоста не ложится на один из них
|
||
for number in range(1, LAUNCHES + 1):
|
||
for mode, (level, flags) in MODES.items():
|
||
first = not launches["INFO"]
|
||
result = run_child(
|
||
level,
|
||
[*extra, *flags, *(["--parts"] if first else [])],
|
||
)
|
||
mismatch = (result.get("format") or {}).get("mismatch")
|
||
if mismatch:
|
||
print(
|
||
f"time format check failed: {mismatch['problem']}\n"
|
||
f" value: {mismatch['value']}\n"
|
||
f" expected: {mismatch['expected']}\n"
|
||
f" got: {mismatch['got']}",
|
||
flush=True,
|
||
)
|
||
return 1
|
||
launches[mode].append(result)
|
||
requests = result["requests"]
|
||
print(
|
||
f"launch {number}/{LAUNCHES} {mode:<9} "
|
||
f"read {requests['read']['wall_us']['median']:.1f} "
|
||
f"write {requests['write']['wall_us']['median']:.1f} us",
|
||
flush=True,
|
||
)
|
||
except InprocError as exc:
|
||
print(f"inproc failed: {exc}", flush=True)
|
||
return 1
|
||
text = render(meta, launches)
|
||
out.parent.mkdir(parents=True, exist_ok=True)
|
||
out.write_text(text, encoding="utf-8")
|
||
print(text, flush=True)
|
||
print(f"saved to {out.relative_to(ROOT)}", flush=True)
|
||
return 0
|
||
|
||
|
||
def main(argv: list[str]) -> int:
|
||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||
parser.add_argument("--label")
|
||
parser.add_argument("--child", action="store_true")
|
||
parser.add_argument("--parts", action="store_true")
|
||
parser.add_argument("--null-stdout", action="store_true")
|
||
# отладка сверки: с другим эталоном команда обязана упасть
|
||
parser.add_argument("--reference-format", default=REFERENCE_FORMAT)
|
||
args = parser.parse_args(argv)
|
||
if args.child:
|
||
return child(args)
|
||
return host(args, argv)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main(sys.argv[1:]))
|