feat: bound upload memory with per-replica slots and a body deadline

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-22 00:23:37 +03:00
co-authored by Claude Opus 5.5
parent fc1524d0d6
commit 6cab9d3c41
55 changed files with 4133 additions and 209 deletions
+974
View File
@@ -0,0 +1,974 @@
"""Профиль загрузок: тела, план, разбор и сводка без docker.
Оркестрирует прогоны `run.py` (профили `uploads`, `uploads_quick`), здесь
— чистые функции на stdlib: PNG из шума на `random.Random(seed)`, план
прогонов кругами, строки k6 о каждой загрузке, строки сэмплера в режиме
`peak`, строка `runs.csv`, сводка по ячейкам и проверка гипотез H1–H4.
Методика — design change `upload-memory`, решение 10; правило —
`.claude/rules/bench.md`, «Профиль загрузок». Проверка —
`uv run python bench/uploads.py --selftest`.
"""
import hashlib
import random
import re
import statistics
import struct
import sys
import zlib
from collections.abc import Iterable
from dataclasses import dataclass, field
from typing import Any
# лимит тела контракта (contracts/README.md, «Вложения (с lab4)»)
MAX_BODY_BYTES = 10 * 1024 * 1024
MIB = 2**20
# ширина PNG из шума: строка 1024 × 3 байта RGB и байт фильтра
PNG_WIDTH = 1024
PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
# варианты кода (решение 10): образ commit до change, checkout без лимита,
# checkout с параметрами по умолчанию
VARIANTS = ("before", "unlimited", "limited")
VARIANT_NAMES = {
"before": "до",
"unlimited": "без лимита",
"limited": "с лимитом",
}
# коды ответов в runs.csv; 0 — ошибка соединения (ответа нет)
CODES = (201, 429, 400, 413, 415, 502, 503, 504, 0)
CODE_FIELDS = [f"s{code}" if code else "s_conn_error" for code in CODES]
RUN_FIELDS = [
"run",
"variant",
"size",
"body_bytes",
"clients",
"repeat",
"order",
"mem_before_mib",
"start_peak_mib",
"peak_mib",
"peak_source",
"anon_max_mib",
"sock_max_mib",
"oom_kill",
"exit_code",
"oom_killed",
*CODE_FIELDS,
"s_other",
"requests",
"window_s",
"uploads_per_s",
"p50_ms",
"p95_ms",
"p99_ms",
"s3_peak_mib",
"s3_anon_max_mib",
"lb_peak_mib",
"lb_sock_max_mib",
"host_swapouts",
"flags",
]
# ---------------------------------------------------------------- тела
def chunk(kind: bytes, body: bytes) -> bytes:
"""Чанк PNG с верной CRC."""
crc = zlib.crc32(kind + body)
return struct.pack(">I", len(body)) + kind + body + struct.pack(">I", crc)
def png_noise(height: int, seed: int, width: int = PNG_WIDTH) -> bytes:
"""Настоящий RGB PNG из шума `random.Random(seed)`: строки с фильтром
0, IDAT — zlib уровня 1 (шум не сжимается, размер ≈ width × height ×
3). Один `seed` — одни и те же байты."""
noise = random.Random(seed)
rows = b"".join(
b"\x00" + noise.randbytes(width * 3) for _ in range(height)
)
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
return (
PNG_SIGNATURE
+ chunk(b"IHDR", ihdr)
+ chunk(b"IDAT", zlib.compress(rows, 1))
+ chunk(b"IEND", b"")
)
def build_bodies(
sizes: Iterable[tuple[str, int]],
seed: int,
) -> dict[str, bytes]:
"""Тела профиля: имя размера → PNG высоты из профиля; зерно тела —
`seed` + высота: у полного и быстрого профиля тело одного размера —
одни и те же байты. Больше лимита контракта — отказ: такое тело
получало бы 413, а не нагружало память."""
bodies = {}
for name, height in sizes:
data = png_noise(height, seed + height)
if len(data) > MAX_BODY_BYTES:
raise ValueError(
f"body {name}: {len(data)} bytes > {MAX_BODY_BYTES}",
)
bodies[name] = data
return bodies
def body_passport(name: str, height: int, data: bytes) -> dict[str, Any]:
return {
"size": name,
"width": PNG_WIDTH,
"height": height,
"bytes": len(data),
"mib": round(len(data) / MIB, 3),
"sha256": hashlib.sha256(data).hexdigest(),
}
# ---------------------------------------------------------------- план
def plan_runs(
variants: Iterable[str],
sizes: Iterable[str],
clients: Iterable[int],
repeats: int,
seed: int,
) -> list[dict[str, Any]]:
"""Прогоны кругами: в круге r — перестановка ячеек (вариант × размер
× клиенты) генератором `Random(seed + r)`; круг не начинается с ячейки,
на которой кончился прежний (как `scaling.plan_cells`)."""
cells = [
(variant, size, count)
for variant in variants
for size in sizes
for count in clients
]
plan: list[dict[str, Any]] = []
previous = None
for round_no in range(1, repeats + 1):
order = list(range(len(cells)))
random.Random(seed + round_no).shuffle(order)
if len(order) > 1 and order[0] == previous:
order[0], order[1] = order[1], order[0]
for index in order:
variant, size, count = cells[index]
plan.append(
{
"run": f"{variant}-{size}-c{count}-r{round_no}",
"variant": variant,
"size": size,
"clients": count,
"repeat": round_no,
"order": len(plan) + 1,
},
)
previous = order[-1]
return plan
# ------------------------------------------------------------ строки k6
T0_RE = re.compile(r"\bup-t0,(?P<t0>\d+)")
UPLOAD_RE = re.compile(
r"\bup,(?P<vu>\d+),(?P<iteration>\d+),(?P<start>\d+),"
r"(?P<duration>\d+),(?P<status>\d+),(?P<instance>[A-Za-z0-9._-]+)",
)
@dataclass(frozen=True)
class Upload:
"""Одна загрузка: отправка (мс часов ВМ), длительность, статус."""
vu: int
start_ms: int
duration_ms: int
status: int
instance: str
def parse_k6(text: str) -> tuple[int | None, list[Upload]]:
"""Строки `uploads.js`: `up-t0,<мс>` из setup и `up,<vu>,<итерация>,
<отправка>,<длительность>,<статус>,<X-Instance-Id>` на каждую
загрузку. Строки консоли k6 обёрнуты в его формат — ищется начало
`up`."""
t0 = None
uploads = []
for line in text.splitlines():
if match := T0_RE.search(line):
t0 = int(match["t0"])
elif match := UPLOAD_RE.search(line):
uploads.append(
Upload(
vu=int(match["vu"]),
start_ms=int(match["start"]),
duration_ms=int(match["duration"]),
status=int(match["status"]),
instance=match["instance"],
),
)
return t0, uploads
def percentile(values: list[float], q: float) -> float | None:
"""Перцентиль с линейной интерполяцией (как p(N) k6)."""
if not values:
return None
ordered = sorted(values)
pos = (len(ordered) - 1) * q
low = int(pos)
high = min(low + 1, len(ordered) - 1)
return ordered[low] + (ordered[high] - ordered[low]) * (pos - low)
def k6_stats(
uploads: list[Upload],
t0: int,
ramp_s: float,
window_s: float,
) -> dict[str, Any]:
"""Ответы по кодам — за весь прогон (разгон и окно, в том числе после
гибели реплики); загрузок 201 в секунду и перцентили успешных — по
загрузкам, отправленным в окне `[t0 + ramp, t0 + ramp + window)`."""
counts = dict.fromkeys(CODE_FIELDS, 0)
other = 0
for item in uploads:
if item.status in CODES:
key = f"s{item.status}" if item.status else "s_conn_error"
counts[key] += 1
else:
other += 1
w0 = t0 + ramp_s * 1000
w1 = w0 + window_s * 1000
ok = [
item.duration_ms
for item in uploads
if item.status == 201 and w0 <= item.start_ms < w1
]
def ms(value: float | None) -> float | None:
return None if value is None else round(value, 1)
return {
**counts,
"s_other": other,
"requests": len(uploads),
"window_s": window_s,
"uploads_per_s": round(len(ok) / window_s, 2),
"p50_ms": ms(percentile(ok, 0.5)),
"p95_ms": ms(percentile(ok, 0.95)),
"p99_ms": ms(percentile(ok, 0.99)),
}
# -------------------------------------------------------- строки сэмплера
@dataclass
class Peak:
"""Память одного контейнера за окно по строкам сэмплера `peak`."""
start_peak: int | None = None
reset: bool = False
peak: int | None = None
first_current: int | None = None
max_current: int = 0
max_anon: int = 0
max_sock: int = 0
oom_kill_start: int | None = None
oom_kill_end: int | None = None
samples: int = 0
missing: int = 0
@property
def oom_kills(self) -> int:
if self.oom_kill_start is None or self.oom_kill_end is None:
return 0
return self.oom_kill_end - self.oom_kill_start
@property
def window_peak(self) -> int | None:
"""Пик окна: значение `memory.peak` через свой дескриптор после
сброса; без сброса или без чтения (контейнер погиб) — наибольшая
выборка `memory.current`."""
if self.reset and self.peak is not None:
return max(self.peak, self.max_current)
return self.max_current or None
@property
def source(self) -> str:
return "reset" if self.reset and self.peak is not None else "sampled"
@dataclass
class PeakSamples:
roles: dict[str, Peak] = field(default_factory=dict)
ended: bool = False
def parse_peak(text: str) -> PeakSamples:
"""Строки сэмплера в режиме `peak` (`bench/sampler.sh`):
`start <роль> <байт>|missing`, `reset <роль> ok|failed`,
`pk <нс> <роль> <id12> <current> <anon> <sock> <oom_kill>` или
`pk <нс> <роль> <id12> missing`, `peak <роль> <байт>|missing`,
`end <нс>`."""
result = PeakSamples()
def role(name: str) -> Peak:
return result.roles.setdefault(name, Peak())
for line in text.splitlines():
parts = line.split()
if not parts:
continue
kind = parts[0]
if kind == "start" and len(parts) == 3 and parts[2].isdigit():
role(parts[1]).start_peak = int(parts[2])
elif kind == "reset" and len(parts) >= 3:
role(parts[1]).reset = parts[2] == "ok"
elif kind == "pk" and len(parts) == 8:
item = role(parts[2])
current, anon, sock, oom = (int(v) for v in parts[4:8])
item.samples += 1
if item.first_current is None:
item.first_current = current
item.max_current = max(item.max_current, current)
item.max_anon = max(item.max_anon, anon)
item.max_sock = max(item.max_sock, sock)
if item.oom_kill_start is None:
item.oom_kill_start = oom
item.oom_kill_end = oom
elif kind == "pk" and len(parts) == 5 and parts[4] == "missing":
role(parts[2]).missing += 1
elif kind == "peak" and len(parts) == 3 and parts[2].isdigit():
role(parts[1]).peak = int(parts[2])
elif kind == "end":
result.ended = True
return result
def mib(value: int | None) -> float | None:
return None if value is None else round(value / MIB, 1)
def run_row(
run: dict[str, Any],
body_bytes: int,
peaks: PeakSamples,
stats: dict[str, Any],
state: dict[str, Any],
swapouts: int | None,
) -> dict[str, Any]:
"""Строка `runs.csv` прогона. Пометки: `oom` — реплику завершило ядро
из-за памяти (`OOMKilled` контейнера или `oom_kill` cgroup),
`host_swap` — хост выгружал страницы в swap во время прогона,
`peak_sampled` — пик окна по выборкам, а не по сброшенному
`memory.peak`."""
app = peaks.roles.get("api", Peak())
s3 = peaks.roles.get("s3", Peak())
lb = peaks.roles.get("haproxy", Peak())
oom = bool(state.get("oom_killed")) or app.oom_kills > 0
flags = []
if oom:
flags.append("oom")
if swapouts:
flags.append("host_swap")
if app.source == "sampled":
flags.append("peak_sampled")
if state.get("s3_down"):
flags.append("s3_down")
return {
**{key: run[key] for key in ("run", "variant", "size", "clients")},
"repeat": run["repeat"],
"order": run["order"],
"body_bytes": body_bytes,
"mem_before_mib": mib(app.first_current),
"start_peak_mib": mib(app.start_peak),
"peak_mib": mib(app.window_peak),
"peak_source": app.source,
"anon_max_mib": mib(app.max_anon),
"sock_max_mib": mib(app.max_sock),
"oom_kill": app.oom_kills,
"exit_code": state.get("exit_code"),
"oom_killed": bool(state.get("oom_killed")),
**stats,
"s3_peak_mib": mib(s3.window_peak),
"s3_anon_max_mib": mib(s3.max_anon),
"lb_peak_mib": mib(lb.window_peak),
"lb_sock_max_mib": mib(lb.max_sock),
"host_swapouts": swapouts,
"flags": " ".join(flags),
}
# ---------------------------------------------------------------- сводка
def num(value: Any) -> float | None:
if value is None or value == "":
return None
return float(value)
def spread(values: Iterable[float | None], digits: int = 1) -> str:
"""Медиана и min–max повторов; один повтор — значение."""
present = [v for v in values if v is not None]
if not present:
return "—"
mid = statistics.median(present)
if len(present) == 1:
return f"{mid:.{digits}f}"
return (
f"{mid:.{digits}f} ({min(present):.{digits}f}–"
f"{max(present):.{digits}f})"
)
def median(values: Iterable[float | None]) -> float | None:
present = [v for v in values if v is not None]
return statistics.median(present) if present else None
def is_oom(row: dict[str, Any]) -> bool:
return "oom" in str(row.get("flags", "")).split()
def errors_5xx(row: dict[str, Any]) -> int:
return sum(int(num(row.get(f"s{code}")) or 0) for code in (502, 503, 504))
def cells(
rows: list[dict[str, Any]],
) -> dict[tuple[str, str, int], list[dict[str, Any]]]:
grouped: dict[tuple[str, str, int], list[dict[str, Any]]] = {}
for row in rows:
key = (row["variant"], row["size"], int(row["clients"]))
grouped.setdefault(key, []).append(row)
return grouped
def per_client_bodies(row: dict[str, Any]) -> float | None:
"""Рост пика окна над памятью до окна на клиента, в телах."""
peak, before = num(row.get("peak_mib")), num(row.get("mem_before_mib"))
body = num(row.get("body_bytes"))
clients = int(row["clients"])
if peak is None or before is None or not body or not clients:
return None
return (peak - before) * MIB / body / clients
def hypotheses(
rows: list[dict[str, Any]],
*,
memory_limit_mib: float,
big: str,
) -> list[dict[str, str]]:
"""Проверка гипотез H1–H4 числами ячеек (design change
`upload-memory`, решение 10). Итог — «да», «нет» или «нет данных»;
вывод по гипотезе пишет `REPORT.md`."""
grouped = cells(rows)
def group(variant: str, size: str, clients: int) -> list[dict[str, Any]]:
return grouped.get((variant, size, clients), [])
def peak(variant: str, clients: int) -> float | None:
return median(num(r["peak_mib"]) for r in group(variant, big, clients))
out = []
# H1: с лимитом пик ≤ половины лимита, рост сверх C=10 ≤ 1 MiB на
# клиента, OOM и 5xx нет, лишние — 429
limited = [r for r in rows if r["variant"] == "limited"]
levels = [c for c in (10, 20, 40) if group("limited", big, c)]
if levels:
peaks = {c: peak("limited", c) for c in levels}
base = peaks.get(10)
growth_ok = all(
base is not None
and peaks[c] is not None
and peaks[c] - base <= (c - 10)
for c in levels
if c > 10
)
half_ok = all(
v is not None and v <= memory_limit_mib / 2 for v in peaks.values()
)
oom = sum(is_oom(r) for r in limited)
fives = sum(errors_5xx(r) for r in limited)
busy = sum(int(num(r.get("s429")) or 0) for r in limited)
verdict = half_ok and growth_ok and not oom and not fives
out.append(
{
"id": "H1",
"text": "«с лимитом»: пик при 10 MiB и C = 10, 20, 40 ≤ "
"половины лимита; сверх C = 10 — ≤ 1 MiB на клиента; OOM "
"и 5xx нет, лишние — 429",
"numbers": "пик, MiB: "
+ ", ".join(f"C={c} {fmt(peaks[c])}" for c in levels)
+ f" (половина лимита {memory_limit_mib / 2:g}); OOM "
f"{oom}, 5xx {fives}, 429 {busy}",
"result": "да" if verdict else "нет",
},
)
else:
out.append(no_data("H1"))
# H2: без лимита рост ~1,2–1,3 тела на клиента, 40 × 10 MiB — OOM; «до»
# — ~2,1 тела на клиента и OOM уже при 20 × 10 MiB
parts = []
for variant in ("unlimited", "before"):
for clients in (10, 20, 40):
items = group(variant, big, clients)
if not items:
continue
ooms = sum(is_oom(r) for r in items)
slope = median(
per_client_bodies(r) for r in items if not is_oom(r)
)
parts.append(
f"{VARIANT_NAMES[variant]} C={clients}: "
f"{fmt(slope, 2)} тела/клиента, OOM {ooms}/{len(items)}",
)
if parts:
unl40 = group("unlimited", big, 40)
bef20 = group("before", big, 20)
slope10 = median(
per_client_bodies(r)
for r in group("unlimited", big, 10)
if not is_oom(r)
)
checks = [
slope10 is not None and slope10 <= 1.5,
bool(unl40) and all(is_oom(r) for r in unl40),
]
if bef20:
checks.append(all(is_oom(r) for r in bef20))
out.append(
{
"id": "H2",
"text": "«без лимита»: пик растёт на ~1,2–1,3 тела на "
"клиента, 40 × 10 MiB — OOM; «до» — ~2,1 тела и OOM при "
"20 × 10 MiB",
"numbers": "; ".join(parts),
"result": "да" if all(checks) else "нет",
},
)
else:
out.append(no_data("H2"))
# H3: загрузок/с при C ≥ 10 у «с лимитом» ≥ 90 % «без лимита», пока тот
# жив
ratios = []
for size in sorted({r["size"] for r in rows}):
for clients in (10, 20, 40):
lim = group("limited", size, clients)
unl = group("unlimited", size, clients)
if not lim or not unl or any(is_oom(r) for r in unl):
continue
a = median(num(r["uploads_per_s"]) for r in lim)
b = median(num(r["uploads_per_s"]) for r in unl)
if a is not None and b:
ratios.append((size, clients, a, b, a / b))
if ratios:
out.append(
{
"id": "H3",
"text": "загрузок 201 в секунду при C ≥ 10: «с лимитом» ≥ 90 % "
"«без лимита» на тех же C, пока тот жив",
"numbers": "; ".join(
f"{s} C={c}: {a:.2f} / {b:.2f} = {ratio:.0%}"
for s, c, a, b, ratio in ratios
),
"result": "да" if all(r[4] >= 0.9 for r in ratios) else "нет",
},
)
else:
out.append(no_data("H3"))
# H4: 503 нет ни в одном варианте, пока реплика жива
alive = [r for r in rows if not is_oom(r)]
if alive:
count = sum(int(num(r.get("s503")) or 0) for r in alive)
out.append(
{
"id": "H4",
"text": "503 из-за срока записи в S3 нет ни в одном варианте, "
"пока реплика жива",
"numbers": f"503 в {len(alive)} прогонах без OOM: {count}",
"result": "да" if count == 0 else "нет",
},
)
else:
out.append(no_data("H4"))
return out
def no_data(name: str) -> dict[str, str]:
return {"id": name, "text": "", "numbers": "", "result": "нет данных"}
def fmt(value: float | None, digits: int = 1) -> str:
return "—" if value is None else f"{value:.{digits}f}"
def render_summary(meta: dict[str, Any], rows: list[dict[str, Any]]) -> str:
"""`summary.md` метки: паспорт кратко, таблица ячеек (медиана и
min–max повторов), сценарии спеков и гипотезы."""
profile = meta["profile"]
info = meta.get("uploads", {})
bodies = info.get("bodies", {})
big = max(bodies, key=lambda k: bodies[k]["bytes"]) if bodies else ""
limit_mib = (
round(
(meta.get("limits", {}).get("api", {}).get("memory") or 0) / MIB,
)
or 512
)
lines = [
f"# Профиль загрузок: метка `{meta['label']}`",
"",
f"- Статус: **{meta['status']}**"
+ (f" ({meta['reason']})" if meta.get("reason") else ""),
f"- Commit: `{meta['git']['commit']}`"
+ (" + незакоммиченные изменения" if meta["git"]["dirty"] else ""),
f"- Профиль `{profile['name']}`: варианты "
+ ", ".join(
f"«{VARIANT_NAMES[v]}»" for v in profile["uploads_variants"]
)
+ (
f" (образ «до» — commit `{info.get('before_commit')}`)"
if info.get("before_commit")
else ""
)
+ f"; клиенты {', '.join(map(str, profile['uploads_clients']))}; "
f"повторов {profile['repeats']}; окно {profile['uploads_window_s']} "
f"с после {profile['uploads_ramp_s']} с разгона",
"- Тела: "
+ ", ".join(
f"{name} — {item['bytes']} байт ({item['width']} × "
f"{item['height']}), SHA-256 `{item['sha256'][:16]}…`"
for name, item in bodies.items()
),
f"- Реплика {profile['cpus']} CPU / {profile['memory']} без swap, "
f"буфер приёма `{meta.get('stand', {}).get('api_tcp_rmem', '—')}`; "
f"S3 {profile['s3_cpus']} CPU / {profile['s3_memory']}; LB "
f"{profile['lb_cpus']} CPU / {profile['lb_memory']}",
"- Пик — `memory.peak` cgroup, сброшенный через свой дескриптор "
"перед нагрузкой (`peak_source` = reset); без сброса — наибольшая "
"выборка раз в "
f"{profile['uploads_sample_interval_s']:g} с (пометка "
"`peak_sampled`). Память до окна — первая выборка. Ответы по кодам "
"— за весь прогон, загрузок/с и перцентили — по загрузкам окна. "
"Значения — медиана (min–max) повторов",
"- Посторонние контейнеры в VM во время замера: "
+ (", ".join(meta.get("other_containers") or []) or "нет"),
"",
"## Ячейки",
"",
"«Пик старта» — `memory.peak` реплики до сброса (с её запуска: "
"фиктивный хэш пароля, разогревающая загрузка); пик окна его не "
"включает.",
"",
"| Вариант | Тело | C | Пик старта, MiB | До окна, MiB | Пик, MiB | "
"anon, MiB | sock, MiB | OOM | 201 | 429 | 5xx | ошибка соединения | "
"Загрузок/с | p50, мс | p95, мс | S3 пик, MiB | S3 anon, MiB | "
"LB пик, MiB | Пометки |",
"|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|"
"---|---|---|---|",
]
grouped = cells(rows)
order = {v: i for i, v in enumerate(VARIANTS)}
for (variant, size, clients), items in sorted(
grouped.items(),
key=lambda kv: (order.get(kv[0][0], 9), kv[0][1], kv[0][2]),
):
flags = sorted({f for r in items for f in str(r["flags"]).split()})
def col(key: str, digits: int = 1, rows: list = items) -> str:
return spread((num(r.get(key)) for r in rows), digits)
fives = [float(errors_5xx(r)) for r in items]
lines.append(
f"| {VARIANT_NAMES.get(variant, variant)} | {size} | {clients} | "
f"{col('start_peak_mib')} | "
f"{col('mem_before_mib')} | {col('peak_mib')} | "
f"{col('anon_max_mib')} | {col('sock_max_mib')} | "
f"{sum(is_oom(r) for r in items)}/{len(items)} | "
f"{col('s201', 0)} | {col('s429', 0)} | {spread(fives, 0)} | "
f"{col('s_conn_error', 0)} | {col('uploads_per_s', 2)} | "
f"{col('p50_ms', 0)} | {col('p95_ms', 0)} | "
f"{col('s3_peak_mib')} | {col('s3_anon_max_mib')} | "
f"{col('lb_peak_mib')} | {' '.join(flags) or '—'} |",
)
lines.append("")
lines += scenario_lines(rows, big)
lines += [
"## Гипотезы",
"",
"Гипотезы — `REPORT.md`, раздел загрузок (commit до прогонов); "
f"здесь — их проверка числами ячеек тела {big or '—'}, лимит "
f"реплики {limit_mib} MiB.",
"",
"| Гипотеза | Утверждение | Числа | Выполнена |",
"|---|---|---|---|",
]
for item in hypotheses(rows, memory_limit_mib=limit_mib, big=big):
lines.append(
f"| {item['id']} | {item['text'] or '—'} | "
f"{item['numbers'] or '—'} | {item['result']} |",
)
lines.append("")
if meta.get("host_warnings"):
lines += ["## Предупреждения о хосте", ""]
lines += [f"- {w}" for w in meta["host_warnings"]]
lines.append("")
return "\n".join(lines)
def scenario_lines(rows: list[dict[str, Any]], big: str) -> list[str]:
"""Сценарии спеков, которые доказывает метка (исключения сверки
покрытия `student-tests/spec_coverage.py`)."""
grouped = cells(rows)
lines = ["## Сценарии спеков", ""]
parts = []
for variant in ("unlimited", "before"):
items = grouped.get((variant, big, 10), [])
slope = median(per_client_bodies(r) for r in items if not is_oom(r))
if items:
parts.append(
f"«{VARIANT_NAMES[variant]}» — {fmt(slope, 2)} тела на "
"загрузку",
)
if parts:
lines.append(
"- `attachments`, «Одна копия тела при записи в S3» (C = 10, "
f"{big}): рост пика над памятью до окна — "
+ "; ".join(parts)
+ " (порог «без лимита» — 1,5 тела, «до» — больше).",
)
lim = [grouped.get(("limited", big, c), []) for c in (10, 20, 40)]
if any(lim):
flat = [r for items in lim for r in items]
lines.append(
"- `attachments`, «Память не растёт с числом загрузок» и "
"`load-balancing`, «Буферы соединений ограничены» («с лимитом», "
f"{big}): пик реплики "
+ ", ".join(
f"C={c} {fmt(median(num(r['peak_mib']) for r in items))}"
for c, items in zip((10, 20, 40), lim, strict=True)
if items
)
+ " MiB, sock "
+ ", ".join(
f"C={c} {fmt(median(num(r['sock_max_mib']) for r in items))}"
for c, items in zip((10, 20, 40), lim, strict=True)
if items
)
+ " MiB, LB "
+ ", ".join(
f"C={c} {fmt(median(num(r['lb_peak_mib']) for r in items))}"
for c, items in zip((10, 20, 40), lim, strict=True)
if items
)
+ f" MiB; OOM {sum(is_oom(r) for r in flat)}, 5xx "
f"{sum(errors_5xx(r) for r in flat)}, 429 "
f"{sum(int(num(r.get('s429')) or 0) for r in flat)}.",
)
lines.append("")
return lines
# --------------------------------------------------------------- selftest
def selftest() -> int:
failures: list[str] = []
def expect(name: str, got: Any, want: Any) -> None:
if got != want:
failures.append(f"{name}: got {got!r}, want {want!r}")
sizes = (("1MiB", 341), ("10MiB", 3409))
first = build_bodies(sizes, 7)
again = build_bodies(sizes, 7)
expect("bodies deterministic", first == again, True)
expect("bodies differ by seed", build_bodies(sizes, 8) == first, False)
for name, data in first.items():
expect(f"{name} <= limit", len(data) <= MAX_BODY_BYTES, True)
expect(f"{name} is PNG", data[:8], PNG_SIGNATURE)
expect("1MiB size", 1_000_000 < len(first["1MiB"]) < 1_100_000, True)
expect(
"10MiB size",
10_400_000 < len(first["10MiB"]) <= MAX_BODY_BYTES,
True,
)
try:
build_bodies((("big", 3500),), 1)
failures.append("body over the limit accepted")
except ValueError:
pass
plan = plan_runs(("unlimited", "limited"), ("10MiB",), (1, 10, 40), 3, 5)
expect("plan length", len(plan), 18)
expect("plan unique", len({p["run"] for p in plan}), 18)
for r in (1, 2):
last = [p for p in plan if p["repeat"] == r][-1]
head = [p for p in plan if p["repeat"] == r + 1][0]
expect(
f"round {r + 1} head",
(head["variant"], head["clients"])
!= (last["variant"], last["clients"]),
True,
)
expect(
"plan stable",
plan
== plan_runs(
("unlimited", "limited"),
("10MiB",),
(1, 10, 40),
3,
5,
),
True,
)
console = "\n".join(
[
'time="…" level=info msg="up-t0,1000" source=console',
"up,1,0,1100,90,201,abc",
"up,2,0,4100,80,201,abc",
"up,2,1,4300,5,429,abc",
"up,3,0,24100,10,201,abc",
"up,3,1,5000,0,0,-",
"up,3,2,5100,12,503,abc",
"garbage",
],
)
t0, ups = parse_k6(console)
expect("t0", t0, 1000)
expect("uploads", len(ups), 6)
stats = k6_stats(ups, t0 or 0, 3, 20)
expect("201", stats["s201"], 3)
expect("429", stats["s429"], 1)
expect("conn", stats["s_conn_error"], 1)
expect("503", stats["s503"], 1)
# окно [4000, 24000): одна 201
expect("uploads/s", stats["uploads_per_s"], 0.05)
expect("p95", stats["p95_ms"], 80.0)
expect("percentile", percentile([1, 2, 3, 4], 0.5), 2.5)
sampler = "\n".join(
[
"start api 160000000",
"start s3 50000000",
"reset api ok",
"reset s3 failed",
"pk 1 api abc 100000000 90000000 1000000 0",
"pk 2 api abc 150000000 140000000 4000000 0",
"pk 3 api abc missing",
"pk 1 s3 def 40000000 30000000 0 0",
"pk 2 s3 def 60000000 35000000 0 0",
"peak api 152000000",
"peak s3 70000000",
"end 4",
],
)
parsed = parse_peak(sampler)
api = parsed.roles["api"]
expect("ended", parsed.ended, True)
expect("api window peak", api.window_peak, 152000000)
expect("api source", api.source, "reset")
expect("api first", api.first_current, 100000000)
expect("api missing", api.missing, 1)
expect("s3 sampled", parsed.roles["s3"].window_peak, 60000000)
expect("s3 source", parsed.roles["s3"].source, "sampled")
oom = parse_peak("pk 1 api a 1 1 1 0\npk 2 api a 2 2 2 1\n")
expect("oom kills", oom.roles["api"].oom_kills, 1)
run = plan[0]
row = run_row(run, 10_000_000, parsed, stats, {"oom_killed": True}, 3)
expect("row flags", row["flags"], "oom host_swap")
expect("row fields", set(RUN_FIELDS) >= set(row), True)
expect("row peak", row["peak_mib"], 145.0)
def synth(variant: str, clients: int, peak: float, **extra: Any) -> dict:
base = {
"run": f"{variant}-{clients}",
"variant": variant,
"size": "10MiB",
"clients": clients,
"body_bytes": 10 * MIB,
"mem_before_mib": 100.0,
"peak_mib": peak,
"uploads_per_s": 8.0,
"s201": 100,
"s429": 0,
"s503": 0,
"flags": "",
"sock_max_mib": 5,
"lb_peak_mib": 50,
}
return {**base, **extra}
rows = [
synth("limited", 10, 170),
synth("limited", 20, 178, s429=5),
synth("limited", 40, 180, s429=60),
synth("unlimited", 10, 230),
synth("unlimited", 20, 350),
synth("unlimited", 40, 512, flags="oom"),
synth("before", 10, 316),
synth("before", 20, 512, flags="oom"),
]
result = {
h["id"]: h["result"]
for h in hypotheses(rows, memory_limit_mib=512, big="10MiB")
}
expect("H1", result["H1"], "да")
expect("H2", result["H2"], "да")
expect("H3", result["H3"], "да")
expect("H4", result["H4"], "да")
worse = rows + [synth("limited", 40, 300, flags="", s503=1)]
result = {
h["id"]: h["result"]
for h in hypotheses(worse, memory_limit_mib=512, big="10MiB")
}
expect("H1 worse", result["H1"], "нет")
expect("H4 worse", result["H4"], "нет")
meta = {
"label": "selftest",
"status": "complete",
"git": {"commit": "x", "dirty": False},
"profile": {
"name": "uploads_quick",
"uploads_variants": ["unlimited", "limited"],
"uploads_clients": [1, 10, 40],
"repeats": 1,
"uploads_window_s": 20,
"uploads_ramp_s": 3,
"uploads_sample_interval_s": 0.2,
"cpus": "1",
"memory": "512m",
"s3_cpus": "1",
"s3_memory": "512m",
"lb_cpus": "1",
"lb_memory": "256m",
},
"uploads": {"bodies": {"10MiB": body_passport("10MiB", 3409, b"x")}},
}
text = render_summary(meta, rows)
expect("summary has H1", "| H1 |" in text, True)
expect("summary has cells", "| с лимитом | 10MiB | 40 |" in text, True)
if failures:
print("selftest failed:\n " + "\n ".join(failures), flush=True)
return 1
print("selftest ok", flush=True)
return 0
if __name__ == "__main__":
if sys.argv[1:] == ["--selftest"]:
sys.exit(selftest())
print("usage: uv run python bench/uploads.py --selftest", flush=True)
sys.exit(2)