Files
mtusi2026lab4/bench/plots.py
T
ricoromaandClaude Opus 5.5 5d5d8f9d2a chore: carry lab 3 over
- carry the lab 3 solution over from mtusi2026lab3-safronov
  (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests,
  bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts,
  Dockerfile, compose, pyproject and uv lock
- merge Makefile, .gitignore and .dockerignore (the lab 3 versions only
  add lines to the skeleton, which is the same in lab 4) and
  .env.example (lab 4 skeleton names listed in the header)
- rename the compose project to mtusi-lab4 and the image to :lab4 so
  the stand does not reuse the lab 3 containers and volumes
- update AGENTS.md for lab 4: current lab, origin commit, contract and
  test files of the new skeleton

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:32:33 +03:00

1342 lines
49 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Графики серий трека 1: SVG текстом, только stdlib.
Запуск — `make bench_plots LABELS="scaling-a scaling-b" [PLOTS=scaling]`,
результат — `evidence/plots/<PLOTS>/`: `rstar-by-replicas.svg`,
`p95-by-rate-<серия>.svg`, `cpu-by-component.svg` и `summary.md` (R*,
эффективность роста, R*(1 × 4) / R*(1 × 1), точка насыщения и признаки на
k_sat ± 1). Серия при завершении строит свои `p95-by-rate.svg` и
`cpu-by-component.svg` в каталоге метки (`render_series`). Данные —
`meta.json`, `searches.csv`, `runs.csv` меток; графики выводятся из
неизменяемых меток и пересоздаются той же командой. Решение 13 design
change `scaling-bench`; палитра — категориальные слоты по порядку,
проверенные валидатором на CVD, у каждой метки — `<title>` с числами.
"""
import argparse
import json
import math
import shutil
import sys
from pathlib import Path
from typing import Any
from xml.sax.saxutils import escape
import scaling
ROOT = Path(__file__).resolve().parents[1]
BENCH_DIR = ROOT / "evidence" / "bench"
OUT_DIR = ROOT / "evidence" / "plots"
# категориальные слоты в фиксированном порядке (проверены на CVD для
# соседних пар): конфигурации и компоненты получают цвет по позиции
SERIES = (
"#2a78d6",
"#eb6834",
"#1baf7a",
"#eda100",
"#e87ba4",
"#008300",
"#6250d6",
"#e34948",
)
SURFACE = "#fcfcfb"
INK = "#0b0b0b"
INK_2 = "#52514e"
MUTED = "#898781"
GRID = "#e1e0d9"
AXIS = "#c3c2b7"
FONT = 'system-ui, -apple-system, "Segoe UI", sans-serif'
COMPONENTS = (
("app_cpu_cores", "реплики API"),
("db_cpu_cores", "PostgreSQL"),
("lb_cpu_cores", "HAProxy"),
("cache_cpu_cores", "Valkey"),
("generator_cpu_cores", "k6"),
("vm_rest", "остальное в ВМ"),
)
# классы ожидания активных соединений PostgreSQL (`db_aas_*`, решение 8)
AAS = ("cpu", "wal", "lock", "lwlock", "io", "other")
class Svg:
"""Минимальный построитель SVG: элементы — строки."""
def __init__(self, width: int, height: int, title: str) -> None:
self.width = width
self.height = height
self.parts = [
f'<rect x="0" y="0" width="{width}" height="{height}" '
f'fill="{SURFACE}"/>',
self.text_el(24, 30, title, size=16, weight=600),
]
@staticmethod
def text_el(
x: float,
y: float,
text: str,
*,
size: int = 12,
color: str = INK_2,
anchor: str = "start",
weight: int = 400,
rotate: bool = False,
) -> str:
transform = (
f' transform="rotate(-90 {x:.1f} {y:.1f})"' if rotate else ""
)
return (
f'<text x="{x:.1f}" y="{y:.1f}" font-size="{size}" '
f'font-weight="{weight}" fill="{color}" text-anchor="{anchor}"'
f"{transform}>{escape(text)}</text>"
)
def text(self, *args: Any, **kwargs: Any) -> None:
self.parts.append(self.text_el(*args, **kwargs))
def line(
self,
x1: float,
y1: float,
x2: float,
y2: float,
*,
color: str = GRID,
width: float = 1,
dash: str = "",
) -> None:
extra = f' stroke-dasharray="{dash}"' if dash else ""
self.parts.append(
f'<line x1="{x1:.1f}" y1="{y1:.1f}" x2="{x2:.1f}" y2="{y2:.1f}" '
f'stroke="{color}" stroke-width="{width}"{extra}/>',
)
def polyline(self, points: list[tuple[float, float]], color: str) -> None:
if len(points) < 2:
return
coords = " ".join(f"{x:.1f},{y:.1f}" for x, y in points)
self.parts.append(
f'<polyline points="{coords}" fill="none" stroke="{color}" '
'stroke-width="2" stroke-linejoin="round" '
'stroke-linecap="round"/>',
)
def dot(
self,
x: float,
y: float,
color: str,
tip: str,
*,
hollow: bool = False,
) -> None:
fill = SURFACE if hollow else color
self.parts.append(
f'<g><title>{escape(tip)}</title>'
f'<circle cx="{x:.1f}" cy="{y:.1f}" r="7" fill="transparent"/>'
f'<circle cx="{x:.1f}" cy="{y:.1f}" r="4.5" fill="{fill}" '
f'stroke="{color if hollow else SURFACE}" '
f'stroke-width="2"/></g>',
)
def bar(
self,
x: float,
y: float,
width: float,
height: float,
color: str,
tip: str,
*,
top: bool,
) -> None:
"""Сегмент столбика; верхний — со скруглённым концом 4 px."""
if height <= 0:
return
if top and height > 4:
r = 4
path = (
f"M{x:.1f},{y + height:.1f} L{x:.1f},{y + r:.1f} "
f"Q{x:.1f},{y:.1f} {x + r:.1f},{y:.1f} "
f"L{x + width - r:.1f},{y:.1f} "
f"Q{x + width:.1f},{y:.1f} {x + width:.1f},{y + r:.1f} "
f"L{x + width:.1f},{y + height:.1f} Z"
)
shape = f'<path d="{path}" fill="{color}"/>'
else:
shape = (
f'<rect x="{x:.1f}" y="{y:.1f}" width="{width:.1f}" '
f'height="{height:.1f}" fill="{color}"/>'
)
self.parts.append(f"<g><title>{escape(tip)}</title>{shape}</g>")
def legend(
self,
items: list[tuple[str, str, str]],
x: float,
y: float,
width: float,
) -> float:
"""Легенда строками: (цвет, подпись, вид — line, dot, box, dash)."""
cx, cy = x, y
for color, label, kind in items:
item_w = 34 + 7 * len(label)
if cx + item_w > x + width:
cx, cy = x, cy + 20
if kind == "box":
self.parts.append(
f'<rect x="{cx:.1f}" y="{cy - 9:.1f}" width="12" '
f'height="12" rx="2" fill="{color}"/>',
)
elif kind == "dot":
self.parts.append(
f'<circle cx="{cx + 6:.1f}" cy="{cy - 3:.1f}" r="4.5" '
f'fill="{color}"/>',
)
else:
self.line(
cx,
cy - 3,
cx + 16,
cy - 3,
color=color,
width=2,
dash={"dash": "5 4", "dots": "2 4"}.get(kind, ""),
)
self.text(cx + 22, cy + 1, label, color=INK_2)
cx += item_w
return cy
def render(self) -> str:
body = "\n".join(self.parts)
return (
'<svg xmlns="http://www.w3.org/2000/svg" '
f'viewBox="0 0 {self.width} {self.height}" '
f'width="{self.width}" height="{self.height}" '
f'font-family=\'{FONT}\'>\n{body}\n</svg>\n'
)
def nice_ticks(high: float, count: int = 5) -> list[float]:
"""Круглые деления оси от 0 до ≥ high."""
if high <= 0:
return [0, 1]
raw = high / count
power = 10 ** math.floor(math.log10(raw))
step = next(m * power for m in (1, 2, 2.5, 5, 10) if m * power >= raw)
ticks = [0.0]
while ticks[-1] < high:
ticks.append(round(ticks[-1] + step, 6))
return ticks
def log_ticks(low: float, high: float) -> list[float]:
ticks = []
exp = math.floor(math.log10(low))
while 10**exp <= high * 1.0001:
for m in (1, 2, 5):
value = m * 10**exp
if low * 0.9999 <= value <= high * 1.0001:
ticks.append(value)
exp += 1
return ticks
def compact(value: float) -> str:
if value >= 1000:
text = f"{value / 1000:.1f}".rstrip("0").rstrip(".")
return f"{text}K"
if value == int(value):
return str(int(value))
return f"{value:g}"
# ------------------------------------------------------------- данные
def load(label_dir: Path) -> dict[str, Any]:
meta = json.loads((label_dir / "meta.json").read_text(encoding="utf-8"))
runs = scaling.read_csv(label_dir / "runs.csv")
searches = scaling.read_csv(label_dir / "searches.csv")
return {
"name": label_dir.name,
"meta": meta,
"runs": runs,
"searches": searches,
"series": meta["profile"].get("series"),
"stats": scaling.config_stats(meta, runs, searches),
"chosen": scaling.chosen_steps(runs, searches),
}
def p95_points(
label: dict[str, Any],
config: str,
) -> tuple[list[tuple[float, float, int]], list[tuple[float, float, int]]]:
"""Медиана p95 по повторам на каждом уровне: ступени лестницы и
фиксированные — линия, деления — отдельные точки."""
line: dict[float, list[float]] = {}
split: dict[float, list[float]] = {}
for search in label["searches"]:
if search["config"] != config:
continue
for step in label["chosen"].get(search["cell"], []):
level = scaling.num(step.get("level"))
p95 = scaling.num(step.get("p95_ms"))
if level is None or p95 is None or p95 <= 0:
continue
target = split if step.get("step_kind") == "bisect" else line
target.setdefault(level, []).append(p95)
def medians(
data: dict[float, list[float]],
) -> list[tuple[float, float, int]]:
return [
(level, scaling.median(values) or 0, len(values))
for level, values in sorted(data.items())
]
return medians(line), medians(split)
def r_star_rows(label: dict[str, Any], config: str) -> list[dict[str, str]]:
"""Строки `runs.csv` на ступени R* завершённых ячеек конфигурации."""
rows = []
for search in label["searches"]:
if search["config"] != config or search["status"] != "complete":
continue
row = scaling.step_at(
label["chosen"].get(search["cell"], []),
scaling.num(search["r_star_rps"]),
)
if row is not None:
rows.append(row)
return rows
def config_rows(label: dict[str, Any], config: str) -> list[dict[str, str]]:
"""Все ступени выбранных попыток ячеек конфигурации."""
return [
row
for search in label["searches"]
if search["config"] == config
for row in label["chosen"].get(search["cell"], [])
]
def per_request(row: dict[str, str], cores: str) -> float | None:
"""CPU компонента на успешный запрос ступени, мкс."""
return scaling.per_request(
scaling.num(row.get(cores)),
scaling.num(row.get("rps")),
)
def wal_share(row: dict[str, str]) -> float | None:
"""Доля ожиданий WAL среди активных соединений PostgreSQL."""
active = sum(scaling.num(row.get(f"db_aas_{name}")) or 0 for name in AAS)
wal = scaling.num(row.get("db_aas_wal"))
return wal / active if active and wal is not None else None
def at_r_star(label: dict[str, Any], config: str) -> dict[str, float | None]:
"""Цена запроса и занятость ресурсов на ступени R*: медианы по
ячейкам (числа таблицы точки насыщения в `REPORT.md`)."""
rows = r_star_rows(label, config)
replicas = int(config.split("x", 1)[0])
def med(value: Any) -> float | None:
return scaling.median(value(row) for row in rows)
def col(name: str) -> Any:
return lambda row: scaling.num(row.get(name))
return {
"app_cores_replica": med(
lambda row: (
(scaling.num(row.get("app_cpu_cores")) or 0) / replicas
),
),
"app_us": med(col("app_us_per_request")),
"db_us": med(col("db_us_per_request")),
"lb_us": med(lambda row: per_request(row, "lb_cpu_cores")),
"k6_us": med(lambda row: per_request(row, "generator_cpu_cores")),
"vm_busy": med(col("vm_busy_cores")),
"vm_psi": med(col("vm_cpu_pressure")),
"db_cores": med(col("db_cpu_cores")),
"db_conn": med(col("db_conn_max")),
"db_conn_replica": med(col("db_conn_replica_max")),
"wal_share": med(wal_share),
"aas_other": med(col("db_aas_other")),
"sessions_new": med(col("db_sessions_new")),
"pool_busy": med(col("pool_busy_share")),
"lb_cores": med(col("lb_cpu_cores")),
}
def peaks(label: dict[str, Any], config: str) -> dict[str, float | None]:
"""Пики памяти за серию по всем ступеням конфигурации, МиБ."""
rows = config_rows(label, config)
def top(name: str) -> float | None:
values = [scaling.num(row.get(name)) for row in rows]
return max((v for v in values if v is not None), default=None)
return {
"rss": top("rss_max_mib"),
"app": top("app_mem_peak_mib"),
"db": top("db_mem_peak_mib"),
"k6": top("generator_mem_peak_mib"),
}
def component_values(
label: dict[str, Any],
config: str,
) -> dict[str, float | None]:
"""CPU по компонентам на ступени R*: медиана по ячейкам."""
rows = r_star_rows(label, config)
values: dict[str, float | None] = {}
for key, _ in COMPONENTS[:-1]:
values[key] = scaling.median(scaling.num(r.get(key)) for r in rows)
stand = sum(v or 0 for v in values.values())
vm = scaling.median(scaling.num(r.get("vm_busy_cores")) for r in rows)
values["vm_rest"] = None if vm is None else max(0.0, vm - stand)
values["vm_busy_cores"] = vm
return values
# ------------------------------------------------------------ графики
def plot_p95(label: dict[str, Any], title: str) -> str:
stats = label["stats"]
width, height = 860, 520
left, right, top, bottom = 76, 24, 96, 64
svg = Svg(width, height, title)
svg.text(
24,
52,
f"Метка {label['name']}: медиана p95 успешных запросов по повторам "
"на каждом уровне; полые точки — ступени деления",
color=INK_2,
)
data = {c["config"]: p95_points(label, c["config"]) for c in stats}
levels = [p[0] for line, split in data.values() for p in line + split]
values = [p[1] for line, split in data.values() for p in line + split]
if not levels:
svg.text(left, top + 40, "Нет данных", color=MUTED)
return svg.render()
x_ticks = nice_ticks(max(levels) * 1.05)
low = 10 ** math.floor(math.log10(max(min(values), 0.5)))
high = 10 ** math.ceil(math.log10(max(max(values), 250)))
y_ticks = log_ticks(low, high)
plot_w = width - left - right
plot_h = height - top - bottom
def sx(value: float) -> float:
return left + value / x_ticks[-1] * plot_w
def sy(value: float) -> float:
span = math.log10(high) - math.log10(low)
return (
top
+ plot_h
- (math.log10(value) - math.log10(low)) / span * plot_h
)
for tick in y_ticks:
svg.line(left, sy(tick), left + plot_w, sy(tick))
svg.text(
left - 8,
sy(tick) + 4,
compact(tick),
color=MUTED,
anchor="end",
)
for tick in x_ticks:
svg.text(
sx(tick),
top + plot_h + 18,
compact(tick),
color=MUTED,
anchor="middle",
)
svg.line(left, top + plot_h, left + plot_w, top + plot_h, color=AXIS)
slo = label["meta"]["profile"].get("slo_p95_ms", 200)
svg.line(
left,
sy(slo),
left + plot_w,
sy(slo),
color=INK_2,
width=1.5,
dash="6 4",
)
# подпись SLO слева: справа линии конфигураций пересекают порог
svg.text(
left + 6,
sy(slo) - 6,
f"SLO p95 {slo:g} мс",
color=INK_2,
)
svg.text(
left + plot_w / 2,
height - 18,
"Интенсивность, запросов/с",
anchor="middle",
)
svg.text(
20,
top + plot_h / 2,
"p95, мс (логарифмическая шкала)",
anchor="middle",
rotate=True,
)
legend = []
for index, c in enumerate(stats):
color = SERIES[index % len(SERIES)]
line, split = data[c["config"]]
svg.polyline([(sx(x), sy(y)) for x, y, _ in line], color)
for x, y, n in line:
svg.dot(
sx(x),
sy(y),
color,
f"{c['config']}: {x:.0f} запросов/с, p95 {y:.1f} мс (повторов {n})",
)
for x, y, n in split:
svg.dot(
sx(x),
sy(y),
color,
f"{c['config']}: деление {x:.0f} запросов/с, p95 {y:.1f} мс (повторов {n})",
hollow=True,
)
legend.append((color, c["config"], "line"))
svg.legend(legend, left, 76, plot_w)
return svg.render()
def plot_cpu(labels: list[dict[str, Any]], title: str) -> str:
bars = []
for label in labels:
prefix = f"{label['series']}: " if len(labels) > 1 else ""
for c in label["stats"]:
bars.append(
(
f"{prefix}{c['config']}",
component_values(label, c["config"]),
c,
),
)
vm_cpus = labels[0]["meta"].get("docker", {}).get("vm_cpus") or 12
perf = labels[0]["meta"].get("host", {}).get("performance_cores")
width = max(720, 120 + 78 * len(bars))
height = 560
left, right, top, bottom = 64, 24, 130, 84
svg = Svg(width, height, title)
svg.text(
24,
52,
"Медиана занятых ядер по ячейкам на ступени R*;",
color=INK_2,
)
svg.text(
24,
70,
"«остальное в ВМ» — загрузка ВМ сверх контейнеров стенда",
color=INK_2,
)
plot_w = width - left - right
plot_h = height - top - bottom
y_ticks = nice_ticks(max(vm_cpus, 1) * 1.05)
def sy(value: float) -> float:
return top + plot_h - value / y_ticks[-1] * plot_h
for tick in y_ticks:
svg.line(left, sy(tick), left + plot_w, sy(tick))
svg.text(
left - 8,
sy(tick) + 4,
compact(tick),
color=MUTED,
anchor="end",
)
svg.line(left, sy(0), left + plot_w, sy(0), color=AXIS)
svg.line(
left,
sy(vm_cpus),
left + plot_w,
sy(vm_cpus),
color=INK_2,
width=1.5,
dash="6 4",
)
svg.text(
left + plot_w - 4,
sy(vm_cpus) - 6,
f"{vm_cpus} vCPU ВМ",
color=INK_2,
anchor="end",
)
if perf:
svg.line(
left,
sy(perf),
left + plot_w,
sy(perf),
color=MUTED,
width=1.5,
dash="2 4",
)
slot = plot_w / max(1, len(bars))
bar_w = min(24.0, slot * 0.6)
for index, (name, values, c) in enumerate(bars):
x = left + slot * index + (slot - bar_w) / 2
base = 0.0
present = [(k, lbl) for k, lbl in COMPONENTS if values.get(k)]
for number, (key, label_text) in enumerate(COMPONENTS):
value = values.get(key) or 0
if value <= 0:
continue
y_top = sy(base + value)
h = sy(base) - y_top
# 2 px поверхности между сегментами
gap = 2 if base > 0 else 0
svg.bar(
x,
y_top,
bar_w,
max(0.0, h - gap),
SERIES[number],
f"{name}: {label_text} {value:.2f} ядра (R* {c['r_star'] or 0:.0f} запросов/с)",
top=key == present[-1][0],
)
base += value
svg.text(
x + bar_w / 2,
sy(base) - 6,
f"{base:.1f}",
color=INK,
anchor="middle",
)
svg.text(
x + bar_w / 2,
top + plot_h + 18,
name,
color=MUTED,
anchor="middle",
)
svg.text(
x + bar_w / 2,
top + plot_h + 34,
f"R* {c['r_star'] or 0:.0f}",
color=MUTED,
anchor="middle",
)
svg.text(
16,
top + plot_h / 2,
"Занятые ядра",
anchor="middle",
rotate=True,
)
# подпись линии ядер хоста — в легенде: на линии её закрывают столбики
items = [
(SERIES[i], text, "box") for i, (_, text) in enumerate(COMPONENTS)
]
if perf:
items.append((MUTED, f"{perf} производительных ядер хоста", "dots"))
svg.legend(items, left, 98, plot_w)
return svg.render()
def plot_rstar(labels: list[dict[str, Any]], title: str) -> str:
growth = next((x for x in labels if x["series"] == "b"), None)
budget = next((x for x in labels if x["series"] == "a"), None)
db = next((x for x in labels if x["series"] == "db"), None)
width, height = 860, 520
left, right, top, bottom = 76, 24, 96, 64
svg = Svg(width, height, title)
svg.text(
24,
52,
"Медиана R* по повторам, усы — min–max"
+ (
"; пунктир — линейный рост k × R*(1 × 1) серии b" if growth else ""
),
color=INK_2,
)
sat = scaling.saturation(growth["stats"]) if growth else None
by_k = sat["by_k"] if sat else {}
base = by_k[1]["r_star"] if 1 in by_k else None
values = [max(c["r_stars"] or [0]) for c in by_k.values()]
for extra in (budget, db):
if extra:
values += [max(c["r_stars"] or [0]) for c in extra["stats"]]
max_k = max(
[
*by_k.keys(),
4,
*(c["replicas"] for c in (budget or {}).get("stats", [])),
*(c["replicas"] for c in (db or {}).get("stats", [])),
],
)
if base:
values.append(base * max_k)
y_ticks = nice_ticks(max(values or [1]) * 1.05)
plot_w = width - left - right
plot_h = height - top - bottom
def sx(k: float) -> float:
return left + (k - 0.5) / max_k * plot_w
def sy(value: float) -> float:
return top + plot_h - value / y_ticks[-1] * plot_h
for tick in y_ticks:
svg.line(left, sy(tick), left + plot_w, sy(tick))
svg.text(
left - 8,
sy(tick) + 4,
compact(tick),
color=MUTED,
anchor="end",
)
for k in range(1, max_k + 1):
svg.text(
sx(k),
top + plot_h + 18,
str(k),
color=MUTED,
anchor="middle",
)
svg.line(left, sy(0), left + plot_w, sy(0), color=AXIS)
svg.text(
left + plot_w / 2,
height - 18,
"Число реплик API",
anchor="middle",
)
svg.text(
20,
top + plot_h / 2,
"R*, запросов/с",
anchor="middle",
rotate=True,
)
legend = []
if base:
svg.line(
sx(1),
sy(base),
sx(max_k),
sy(base * max_k),
color=MUTED,
width=1.5,
dash="6 4",
)
legend.append((MUTED, "линейный рост от 1 × 1", "dash"))
if by_k:
color = SERIES[0]
points = []
for k, c in sorted(by_k.items()):
if c["r_star"] is None:
continue
lo, hi = min(c["r_stars"]), max(c["r_stars"])
svg.line(sx(k), sy(lo), sx(k), sy(hi), color=color, width=1.5)
svg.line(
sx(k) - 5,
sy(lo),
sx(k) + 5,
sy(lo),
color=color,
width=1.5,
)
svg.line(
sx(k) - 5,
sy(hi),
sx(k) + 5,
sy(hi),
color=color,
width=1.5,
)
points.append((k, c))
svg.polyline([(sx(k), sy(c["r_star"])) for k, c in points], color)
for k, c in points:
eff = sat["efficiency"].get(k) if sat else None
svg.dot(
sx(k),
sy(c["r_star"]),
color,
f"серия b, {k} × 1: R* {scaling.spread(c['r_stars'])} запросов/с"
+ (f", эффективность {eff:.2f}" if eff is not None else ""),
)
if points:
# подпись конца линии — над точкой: справа стоят точки серии a
k, c = points[-1]
svg.text(
sx(k),
sy(max(c["r_stars"])) - 10,
f"{c['r_star']:.0f}",
color=INK,
anchor="middle",
)
legend.append((color, "серия b: k × 1 CPU", "line"))
if sat and sat["k_sat"]:
k = sat["k_sat"]
svg.line(
sx(k),
top,
sx(k),
top + plot_h,
color=INK_2,
width=1,
dash="2 4",
)
svg.text(sx(k) + 6, top + 14, f"k_sat = {k}", color=INK_2)
if budget:
color = SERIES[1]
for c in budget["stats"]:
if c["r_star"] is None:
continue
k = c["replicas"]
lo, hi = min(c["r_stars"]), max(c["r_stars"])
svg.line(
sx(k) + 14,
sy(lo),
sx(k) + 14,
sy(hi),
color=color,
width=1.5,
)
svg.dot(
sx(k) + 14,
sy(c["r_star"]),
color,
f"серия a, {k} × {c['replica_cpus']}: R* {scaling.spread(c['r_stars'])} запросов/с",
)
svg.text(
sx(k) + 24,
sy(c["r_star"]) + 4,
f"{k} × {c['replica_cpus']}",
color=INK_2,
)
legend.append((color, "серия a: бюджет 4 CPU", "dot"))
if db:
# серия db: линия по k на каждый лимит CPU PostgreSQL
groups: dict[str, list[dict[str, Any]]] = {}
for c in db["stats"]:
if c["r_star"] is not None:
groups.setdefault(c.get("db_cpus") or "", []).append(c)
for index, (limit, items) in enumerate(
sorted(groups.items(), key=lambda kv: float(kv[0] or 0)),
):
color = SERIES[2 + index]
items.sort(key=lambda c: c["replicas"])
for c in items:
k = c["replicas"]
svg.line(
sx(k),
sy(min(c["r_stars"])),
sx(k),
sy(max(c["r_stars"])),
color=color,
width=1.5,
)
if len(items) > 1:
svg.polyline(
[(sx(c["replicas"]), sy(c["r_star"])) for c in items],
color,
)
for c in items:
svg.dot(
sx(c["replicas"]),
sy(c["r_star"]),
color,
f"серия db, {c['config']}: R* "
f"{scaling.spread(c['r_stars'])} запросов/с",
)
svg.text(
sx(c["replicas"]) + 8,
sy(c["r_star"]) - 8,
f"{c['r_star']:.0f}",
color=INK,
)
legend.append(
(
color,
f"серия db: PostgreSQL {limit} CPU",
"line" if len(items) > 1 else "dot",
),
)
svg.legend(legend, left, 76, plot_w)
return svg.render()
# ------------------------------------------------------------ сводка
def render_combined_summary(labels: list[dict[str, Any]], command: str) -> str:
growth = next((x for x in labels if x["series"] == "b"), None)
sat = scaling.saturation(growth["stats"]) if growth else None
base = None
if sat and 1 in sat["by_k"]:
base = sat["by_k"][1]
# host_cost всех серий — против 1 × 1 серии b (в серии a её нет)
for label in labels:
for c in label["stats"]:
scaling.apply_host_cost(c, base["app_us_marginal"])
lines = [
"# Цена масштабирования: сводка серий",
"",
f"- Команда: `{command}`",
"- Метки: "
+ ", ".join(
f"`{x['name']}` (серия `{x['series']}`, commit "
f"`{x['meta']['git']['commit']}`, {x['meta']['status']})"
for x in labels
),
*passport_lines(labels),
(
"- R* — медиана (min–max) по повторам; эффективность — R* / "
"(k × R*(1 × 1)), опора — 1 × 1 серии `b`; предельный CPU на "
"запрос — наклон CPU реплик от успешных запросов/с по ступеням, "
"прошедшим SLO; host_cost — он же против 1 × 1 серии `b` (порог "
f"{scaling.THRESHOLDS['host_cost']})"
if base
else "- R* — медиана (min–max) по повторам; предельный CPU на "
"запрос — наклон CPU реплик от успешных запросов/с по ступеням, "
"прошедшим SLO; host_cost — он же против 1 × 1 своей метки "
f"(порог {scaling.THRESHOLDS['host_cost']}); эффективность — "
"только вместе с серией `b`"
),
"",
"## R* по конфигурациям",
"",
"| Серия | Конфигурация | R*, запросов/с | Эффективность | p95 на "
"250 / 500, мс | p99 на 250 / 500, мс | CPU реплик на запрос на 500, "
"мкс | Предельный CPU на запрос, мкс | host_cost | Признаки на R* | "
"Признаки на первой нарушившей |",
"|---|---|---|---|---|---|---|---|---|---|---|",
]
for label in labels:
for c in label["stats"]:
k = c["replicas"]
eff = (
c["r_star"] / (k * base["r_star"])
if base and base["r_star"] and c["r_star"] is not None
else None
)
f250 = c["fixed"].get(250) or {}
f500 = c["fixed"].get(500) or {}
us = f500.get("app_us")
cost = c.get("host_cost")
signs_r = list(c["signs_r"])
signs_f = list(c["signs_f"])
lines.append(
f"| {label['series']} | {c['config']} | "
f"{scaling.spread(c['r_stars'])} | {scaling.fmt(eff, 2)} | "
f"{scaling.fmt(f250.get('p95'), 1)} / "
f"{scaling.fmt(f500.get('p95'), 1)} | "
f"{scaling.fmt(f250.get('p99'), 1)} / "
f"{scaling.fmt(f500.get('p99'), 1)} | {scaling.fmt(us, 0)} | "
f"{scaling.fmt(c.get('app_us_marginal'), 0)} | "
f"{scaling.fmt(cost, 2)} | {' '.join(signs_r) or '—'} | "
f"{' '.join(signs_f) or '—'} |",
)
lines.append("")
budget = next((x for x in labels if x["series"] == "a"), None)
if budget and base and base["r_star"]:
one = next((c for c in budget["stats"] if c["replicas"] == 1), None)
if one and one["r_star"] is not None:
lines += [
"## Фиксированный бюджет",
"",
f"R*(1 × {one['replica_cpus']}) / R*(1 × 1) = "
f"{one['r_star']:.0f} / {base['r_star']:.0f} = "
f"**{one['r_star'] / base['r_star']:.2f}** (H1: 0,9–1,15). "
"Посекундный максимум CPU реплики 1 × "
f"{one['replica_cpus']} за серию: "
f"{scaling.fmt(one['app_cpu_second_max'], 2)} ядра.",
"",
*throttling_lines(
[(growth, base), (budget, one)] if growth else [],
),
]
if sat:
lines += [
"## Точка насыщения (серия `b`)",
"",
f"Правило: наименьшее k ≤ 5, при котором медиана R*(k + 1) < "
f"{scaling.THRESHOLDS['k_sat_gain']} × R*(k).",
"",
"| k | R*, запросов/с | Прирост к k − 1 | Эффективность | "
"CPU реплики на R*, ядер на реплику | CPU PostgreSQL на R*, "
"ядер | Признаки на R* | Признаки на первой нарушившей |",
"|---|---|---|---|---|---|---|---|",
]
previous = None
for k, c in sorted(sat["by_k"].items()):
gain = (
c["r_star"] / previous - 1
if previous and c["r_star"] is not None
else None
)
per = c["app_cpu_r"] / k if c["app_cpu_r"] is not None else None
lines.append(
f"| {k} | {scaling.spread(c['r_stars'])} | "
+ (f"{gain * 100:+.0f} %" if gain is not None else "—")
+ f" | {scaling.fmt(sat['efficiency'].get(k), 2)} | "
f"{scaling.fmt(per, 2)} | {scaling.fmt(c['db_cpu_r'], 2)} | "
f"{' '.join(c['signs_r']) or '—'} | "
f"{' '.join(c['signs_f']) or '—'} |",
)
previous = c["r_star"]
k_sat = sat["k_sat"]
lines += [
"",
"k_sat: "
+ (
f"**{k_sat}** — признаки на k_sat − 1, k_sat и k_sat + 1 — "
"строки таблицы выше"
if k_sat
else "нет: в пределах серии каждая реплика добавляет не "
"меньше 10 %"
),
"",
*cost_lines(growth, sat["by_k"]),
]
db = next((x for x in labels if x["series"] == "db"), None)
if db:
lines += db_lines(db)
lines += memory_lines(labels)
return "\n".join(lines)
def db_lines(label: dict[str, Any]) -> list[str]:
"""Серия db: PostgreSQL на ступени R* и на первой нарушившей —
медианы по повторам (насыщение БД управляемым опытом)."""
lines = [
"## Насыщение PostgreSQL (серия `db`)",
"",
"Медианы по повторам. Доля лимита — CPU PostgreSQL / его лимит; "
"троттлинг — доля периодов CFS контейнера PostgreSQL; CPU "
"PostgreSQL на запрос — `db_us_per_request`; доля WAL — от всех "
"активных соединений; признак `db_cpu` — CPU ≥ "
f"{scaling.THRESHOLDS['db_cpu_share'] * 100:.0f} % лимита или "
f"троттлинг ≥ {scaling.THRESHOLDS['throttled_ratio'] * 100:.0f} %.",
"",
"| Конфигурация | Лимит CPU PostgreSQL | Ступень | Уровень, "
"запросов/с | CPU PostgreSQL, ядер | Доля лимита | Троттлинг "
"PostgreSQL | CPU PostgreSQL на запрос, мкс | Ожидания WAL, доля "
"активных | Соединений | p95, мс | Признаки |",
"|---|---|---|---|---|---|---|---|---|---|---|---|",
]
for c in label["stats"]:
limit = scaling.num(c.get("db_cpus"))
steps = {"R*": [], "первая нарушившая": []}
for search in label["searches"]:
if (
search["config"] != c["config"]
or search["status"] != "complete"
):
continue
chosen = label["chosen"].get(search["cell"], [])
r_star = scaling.num(search["r_star_rps"]) or 0
for name, row in (
("R*", scaling.step_at(chosen, r_star)),
("первая нарушившая", scaling.fail_step(chosen, r_star)),
):
if row is not None:
steps[name].append(row)
for name, rows in steps.items():
if not rows:
continue
def med(column: str, rows: list[dict[str, str]] = rows) -> Any:
return scaling.median(scaling.num(r.get(column)) for r in rows)
cpu = med("db_cpu_cores")
share = cpu / limit if cpu is not None and limit else None
wal = scaling.median(wal_share(r) for r in rows)
signs = scaling.majority(
[(r.get("signs") or "").split() for r in rows],
)
lines.append(
f"| {c['config']} | {c.get('db_cpus') or '—'} | {name} | "
f"{scaling.spread([scaling.num(r.get('level')) for r in rows])}"
f" | {scaling.fmt(cpu, 2)} | {scaling.fmt(share, 2)} | "
f"{scaling.fmt(med('db_throttled_ratio'), 3)} | "
f"{scaling.fmt(med('db_us_per_request'), 0)} | "
f"{scaling.fmt(None if wal is None else wal * 100, 0)} % | "
f"{scaling.fmt(med('db_conn_max'), 0)} | "
f"{scaling.fmt(med('p95_ms'), 1)} | "
f"{' '.join(signs) or '—'} |",
)
return [*lines, ""]
def passport_lines(labels: list[dict[str, Any]]) -> list[str]:
"""Шапка evidence: спек, change, даты меток и хост из паспортов."""
hosts = []
for x in labels:
host, docker = x["meta"].get("host", {}), x["meta"].get("docker", {})
text = (
f"{host.get('machine')} ({host.get('performance_cores')} P + "
f"{host.get('efficiency_cores')} E ядер, "
f"{host.get('memory_gib')} ГиБ), {host.get('os')}; ВМ Docker "
f"{docker.get('vm_cpus')} vCPU / {docker.get('vm_memory_gib')} "
f"GiB, Docker {docker.get('server')}"
)
if text not in hosts:
hosts.append(text)
return [
"- Спек: `load-benchmark`, change `scaling-bench`",
"- Даты меток (UTC): "
+ ", ".join(
f"`{x['name']}` {x['meta'].get('started_at')} — "
f"{x['meta'].get('finished_at')}"
for x in labels
),
"- Хост (паспорт `meta.json`): " + "; ".join(hosts),
]
def percent(values: list[float | None]) -> str:
"""Доли в процентах: медиана (min–max)."""
return scaling.spread(
[None if v is None else v * 100 for v in values],
1,
)
def throttling_lines(
pairs: list[tuple[dict[str, Any] | None, dict[str, Any]]],
) -> list[str]:
"""H1б: троттлинг CFS и p99 одной реплики 1 × 1 и 1 × 4 на 500/с, на
R* и на нарушивших ступенях."""
lines = [
"Троттлинг CFS одной реплики (H1б; доля периодов, медиана (min–max) "
"по повторам; нарушившие — наибольшее по ступеням выше R*):",
"",
"| Конфигурация | Троттлинг на 500/с, % | p99 на 500/с, мс | "
"Троттлинг на R*, % | p99 на R*, мс | Троттлинг на нарушивших, "
"max % |",
"|---|---|---|---|---|---|",
]
for label, c in pairs:
if label is None:
continue
f500 = c["fixed"].get(500) or {}
rows = r_star_rows(label, c["config"])
failed = [
scaling.num(row.get("app_throttled_ratio"))
for row in config_rows(label, c["config"])
if row.get("slo_pass") == "False"
]
top = max((v for v in failed if v is not None), default=None)
at_r = [scaling.num(r.get("app_throttled_ratio")) for r in rows]
p99_r = [scaling.num(r.get("p99_ms")) for r in rows]
lines.append(
f"| {label['series']} {c['config']} | "
f"{percent(f500.get('throttled_all') or [])} | "
f"{scaling.spread(f500.get('p99_all') or [], 1)} | "
f"{percent(at_r)} | {scaling.spread(p99_r, 1)} | "
f"{scaling.fmt(None if top is None else top * 100, 1)} |",
)
return [*lines, ""]
def cost_lines(
label: dict[str, Any] | None,
by_k: dict[int, dict[str, Any]],
) -> list[str]:
"""Цена успешного запроса в CPU по компонентам и занятость общих
ресурсов на ступени R* серии роста; рост — к k = 1."""
if label is None or not by_k:
return []
values = {k: at_r_star(label, c["config"]) for k, c in by_k.items()}
base = values.get(1, {})
keys = ("app_us", "db_us", "lb_us", "k6_us")
def growth(v: dict[str, float | None], key: str) -> str:
own, ref = v.get(key), base.get(key)
return f"×{own / ref:.2f}" if own and ref else "—"
lines = [
"## Цена запроса и общие ресурсы на R* (серия `b`)",
"",
"Медианы по повторам на ступени R*. CPU на успешный запрос — CPU "
"компонента × 10⁶ / успешные в секунду (у HAProxy и k6 — по "
"`lb_cpu_cores` и `generator_cpu_cores`); рост — к k = 1. PSI — доля "
"времени, когда задачи ВМ ждали CPU.",
"",
"| k | CPU реплики, ядер на реплику | CPU на запрос, мкс: реплики / "
"PostgreSQL / HAProxy / k6 | Рост к k = 1 | Занято ядер ВМ | PSI CPU "
"ВМ, % | CPU PostgreSQL, ядер | CPU HAProxy, ядер |",
"|---|---|---|---|---|---|---|---|",
]
for k, v in sorted(values.items()):
psi = v["vm_psi"]
lines.append(
f"| {k} | {scaling.fmt(v['app_cores_replica'], 2)} | "
+ " / ".join(scaling.fmt(v[key], 0) for key in keys)
+ " | "
+ " / ".join(growth(v, key) for key in keys)
+ f" | {scaling.fmt(v['vm_busy'], 1)} | "
f"{scaling.fmt(None if psi is None else psi * 100, 1)} | "
f"{scaling.fmt(v['db_cores'], 2)} | "
f"{scaling.fmt(v['lb_cores'], 2)} |",
)
lines += [
"",
"PostgreSQL и пул на R* (`db_aas_*` — среднее число активных "
"соединений по классу ожидания; доля WAL — от всех активных, "
"порог признака `db_wal` — "
f"{scaling.THRESHOLDS['wait_share'] * 100:.0f} % при среднем не "
f"меньше {scaling.THRESHOLDS['db_active_min']:.0f} активного; "
"`pool_busy_share` — доля отсчётов, когда у реплики не было "
"свободного соединения):",
"",
"| k | Соединений, всего | Соединений реплики, max | Ожидания "
"WAL, доля активных | Прочие ожидания, соединений | Новых сессий за "
"окно | pool_busy_share |",
"|---|---|---|---|---|---|---|",
]
for k, v in sorted(values.items()):
share = v["wal_share"]
lines.append(
f"| {k} | {scaling.fmt(v['db_conn'], 0)} | "
f"{scaling.fmt(v['db_conn_replica'], 0)} | "
f"{scaling.fmt(None if share is None else share * 100, 0)} % | "
f"{scaling.fmt(v['aas_other'], 2)} | "
f"{scaling.fmt(v['sessions_new'], 0)} | "
f"{scaling.fmt(v['pool_busy'], 2)} |",
)
return [*lines, ""]
def memory_lines(labels: list[dict[str, Any]]) -> list[str]:
"""Пики памяти за серию: память нигде не была узким местом."""
lines = [
"## Память: пики за серию",
"",
"Наибольшее по всем ступеням конфигурации, МиБ. RSS — сумма RSS "
"процессов реплик (`rss_max_mib`) и она же на реплику; cgroup — "
"сумма пиков `memory.current` реплик (`app_mem_peak_mib`). Лимит "
"PostgreSQL и k6 — 1 ГиБ.",
"",
"| Серия | Конфигурация | Лимит реплики | RSS реплик, сумма | RSS "
"на реплику | cgroup реплик, сумма | PostgreSQL | k6 |",
"|---|---|---|---|---|---|---|---|",
]
for label in labels:
for c in label["stats"]:
p = peaks(label, c["config"])
k = c["replicas"]
limit = next(
(
s["replica_memory"]
for s in label["searches"]
if s["config"] == c["config"]
),
"",
)
lines.append(
f"| {label['series']} | {c['config']} | {limit or '—'} | "
f"{scaling.fmt(p['rss'], 0)} | "
f"{scaling.fmt(None if p['rss'] is None else p['rss'] / k)} | "
f"{scaling.fmt(p['app'], 0)} | "
f"{scaling.fmt(p['db'], 0)} | {scaling.fmt(p['k6'], 0)} |",
)
return [*lines, ""]
def render_series(label_dir: Path) -> None:
"""Графики серии в каталоге метки: p95 от интенсивности и CPU по
компонентам на R*."""
try:
label = load(label_dir)
except (OSError, ValueError, KeyError):
return
if not label["searches"]:
return
series = label["series"]
(label_dir / "p95-by-rate.svg").write_text(
plot_p95(label, f"Серия {series}: p95 от интенсивности"),
encoding="utf-8",
)
(label_dir / "cpu-by-component.svg").write_text(
plot_cpu([label], f"Серия {series}: CPU по компонентам на R*"),
encoding="utf-8",
)
def render_combined(names: list[str], out_name: str, command: str) -> Path:
labels = [load(BENCH_DIR / name) for name in names]
out = OUT_DIR / out_name
if out.exists():
shutil.rmtree(out)
out.mkdir(parents=True)
(out / "rstar-by-replicas.svg").write_text(
plot_rstar(labels, "R* от числа реплик API"),
encoding="utf-8",
)
for label in labels:
(out / f"p95-by-rate-{label['series']}.svg").write_text(
plot_p95(label, f"Серия {label['series']}: p95 от интенсивности"),
encoding="utf-8",
)
(out / "cpu-by-component.svg").write_text(
plot_cpu(labels, "CPU по компонентам на R*"),
encoding="utf-8",
)
(out / "summary.md").write_text(
render_combined_summary(labels, command),
encoding="utf-8",
)
return out
def main(argv: list[str]) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--labels", default="")
parser.add_argument("--out", default="scaling")
args = parser.parse_args(argv)
names = args.labels.split()
if not names:
print(
'labels are required: make bench_plots LABELS="<label> …" '
"[PLOTS=<dir>]",
flush=True,
)
return 2
missing = [n for n in names if not (BENCH_DIR / n / "meta.json").is_file()]
if missing:
print(f"labels not found: {', '.join(missing)}", flush=True)
return 1
not_series = [
n
for n in names
if not json.loads(
(BENCH_DIR / n / "meta.json").read_text(encoding="utf-8"),
)["profile"].get("series")
]
if not_series:
print(
f"not track 1 series labels: {', '.join(not_series)}",
flush=True,
)
return 1
command = f'make bench_plots LABELS="{" ".join(names)}" PLOTS={args.out}'
out = render_combined(names, args.out, command)
print((out / "summary.md").read_text(encoding="utf-8"), flush=True)
print(f"saved to {out.relative_to(ROOT)}", flush=True)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))