Files
mtusi2026lab3/bench/track1/profile.py
T
Alexey BodanyukandClaude Opus 5.5 2033440cdb bench: трек 1 — оркестратор серий, k6 open-loop, py-spy sidecar
- bench/track1/run.py: на каждую конфигурацию x повтор — свежий стенд
  (отдельный compose-проект, пустые volumes), образ API из git-коммита
  (git archive), ступени constant-arrival-rate; CPU каждого контейнера,
  ожидание пула (STATS_LOG_SECONDS), pg_stat_database/pg_stat_statements,
  счётчики HAProxy; raw JSON + results.csv, без токенов.
- compose.bench.yaml: cpuset для k6 / LB+Valkey / PostgreSQL / API, чтобы
  генератор и LB не отнимали CPU у реплик.
- bench/track1/profile.py + pyspy/: py-spy с хешем пакета, профиль одной
  реплики под нагрузкой.
- API: STATS_LOG_SECONDS (по умолчанию выключено) — счётчики пула и CPU
  процесса в лог; API_IMAGE в compose для образа из коммита.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 13:51:37 +03:00

126 lines
6.1 KiB
Python

#!/usr/bin/env python3
"""Where does one API replica spend its CPU under the track 1 workload? (py-spy sidecar)
python bench/track1/profile.py --label baseline --rate 260 --config replicas=2,api_cpus=1.0
Starts a fresh bench stand, runs k6 at a fixed arrival rate and samples ONE replica with
py-spy (sidecar sharing its PID namespace, SYS_PTRACE only for the sidecar, image
bench/track1/pyspy with a hash-pinned py-spy). Output evidence/track1/profile-<label>.txt:
share of samples per library ("where the time goes") and the top functions by self/total time.
"""
from __future__ import annotations
import argparse
import collections
import shutil
import subprocess
import sys
import tempfile
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import run as bench # noqa: E402
ROOT = bench.ROOT
IMAGE = "mtusi-pyspy:lab3"
CATEGORIES = [ # first match wins; path fragments of the frames' files
("psycopg (PostgreSQL driver + pool)", ("psycopg",)),
("redis-py (Valkey client)", ("redis/",)),
("uvicorn + httptools (HTTP server)", ("uvicorn", "httptools")),
("starlette (routing, middleware, responses)", ("starlette",)),
("logging (access log JSON lines)", ("logging/", "logging_setup.py")),
("json (stdlib)", ("json/",)),
("asyncio / uvloop (event loop)", ("asyncio", "uvloop")),
("messenger (our code)", ("messenger/",)),
]
def category(stack: list[str]) -> str:
"""The innermost frame that belongs to a known library decides the category."""
for frame in reversed(stack):
for name, needles in CATEGORIES:
if any(n in frame for n in needles):
return name
return "other"
def short(frame: str) -> str:
name, _, where = frame.partition(" (")
file = where.rstrip(")").split(":")[0].rsplit("/", 2)[-2:]
return f"{name} ({'/'.join(file)})"
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
parser.add_argument("--label", required=True)
parser.add_argument("--config", default="replicas=2,api_cpus=1.0")
parser.add_argument("--rate", type=int, default=260)
parser.add_argument("--seconds", type=int, default=20)
parser.add_argument("--top", type=int, default=25)
parser.add_argument("--git-ref", default="HEAD")
args = parser.parse_args()
subprocess.run(["docker", "build", "-q", "-t", IMAGE, str(ROOT / "bench" / "track1" / "pyspy")], check=True,
capture_output=True)
cfg = bench.parse_config(args.config)
sha, bench.IMAGE["tag"] = bench.build_image(args.git_ref)
bench.start_stand(cfg)
work = tempfile.mkdtemp(prefix="track1-profile-")
try:
Path(work, "chats.json").write_text(__import__("json").dumps(bench.make_chats(64)), encoding="utf-8")
warmup, duration = 8, args.seconds + 8
k6 = subprocess.Popen(["docker", "compose", "-p", bench.PROJECT, *bench.FILES, "run", "--rm", "--no-deps",
"-v", f"{work}:/data:ro", "-e", f"RATE={args.rate}", "-e", f"WARMUP={warmup}s",
"-e", f"DURATION={duration}s", "-e", f"MAX_VUS={args.rate * 2}", "k6", "run", "--quiet",
"/scripts/messages.js"], cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
text=True, encoding="utf-8")
time.sleep(warmup + 4)
target = bench.compose("ps", "-q", "api").split()[0]
subprocess.run(["docker", "run", "--rm", "--pid", f"container:{target}", "--cap-add", "SYS_PTRACE",
"-v", f"{work}:/out", IMAGE, "record", "--pid", "1", "--duration", str(args.seconds),
"--rate", "250", "--format", "raw", "--nonblocking", "--output", "/out/stacks.txt"],
check=True, capture_output=True, timeout=args.seconds + 120)
out, _ = k6.communicate(timeout=300)
data = __import__("json").loads(out[out.index(bench.MARK) + len(bench.MARK):].strip().splitlines()[0])
ok_rps = (bench.metric(data, "ok_messages{phase:measure}", "count") or 0) / duration
stacks = []
for line in Path(work, "stacks.txt").read_text(encoding="utf-8").splitlines():
stack, _, count = line.rpartition(" ")
frames = [f for f in stack.split(";") if f and not f.startswith("process ") and not f.startswith("thread ")]
if frames:
stacks.append((frames, int(count)))
finally:
shutil.rmtree(work, ignore_errors=True)
bench.compose("down", "-v", "--remove-orphans", check=False, timeout=300)
total = sum(c for _, c in stacks)
idle = sum(c for f, c in stacks if any("select" in x and ("selectors" in x or "uvloop" in x) for x in f[-2:]))
cats = collections.Counter()
self_time, total_time = collections.Counter(), collections.Counter()
for frames, count in stacks:
cats[category(frames)] += count
self_time[short(frames[-1])] += count
for name in {short(f) for f in frames}:
total_time[name] += count
lines = [f"# py-spy, {args.seconds} s at 250 Hz, ONE replica of {bench.config_name(cfg)}, k6 {args.rate} req/s "
f"(ok {ok_rps:.1f} req/s for the whole stand)", f"# label {args.label}, commit {sha}; samples {total} "
f"(idle in the event loop poll: {100 * idle / max(total, 1):.1f} %)", "",
"## Share of samples by library (innermost known frame)"]
for name, count in cats.most_common():
lines.append(f"{100 * count / total:6.1f} % {name}")
lines += ["", f"## Top {args.top} functions by SELF time (on top of the stack)"]
for name, count in self_time.most_common(args.top):
lines.append(f"{100 * count / total:6.1f} % {name}")
lines += ["", f"## Top {args.top} functions by TOTAL time (anywhere in the stack)"]
for name, count in total_time.most_common(args.top):
lines.append(f"{100 * count / total:6.1f} % {name}")
dest = ROOT / "evidence" / "track1" / f"profile-{args.label}.txt"
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_text("\n".join(lines) + "\n", encoding="utf-8")
print("\n".join(lines[:40]))
return 0
if __name__ == "__main__":
raise SystemExit(main())