Files
mtusi2026lab3/bench/plots.py
T
ricoromaandClaude Opus 5.5 02fd3d8a36 feat: add track 1 scaling series to the bench stand
Series a/b/pool/ab/quick in bench/run.py: per-cell fresh stand with the
configuration's replicas and max_connections, open-model mixed profile
(k6/mixed.js), fixed levels and R* search by SLO, a sampler container
for cgroup, VM, HAProxy and PostgreSQL metrics, resume and a stand
lock. Pure helpers in bench/scaling.py, SVG plots in bench/plots.py,
series mode for bench_compare and bench_ab. Hypotheses H1-H3 go to
REPORT.md before the first series.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 03:28:36 +03:00

954 lines
32 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", "остальное в ВМ"),
)
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="5 4" if kind == "dash" else "",
)
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 component_values(
label: dict[str, Any],
config: str,
) -> dict[str, float | None]:
"""CPU по компонентам на ступени 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)
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",
)
svg.text(
left + plot_w - 4,
sy(slo) - 6,
f"SLO p95 {slo:g} мс",
color=INK_2,
anchor="end",
)
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",
)
svg.text(
left + plot_w - 4,
sy(perf) - 6,
f"{perf} производительных ядер хоста",
color=INK_2,
anchor="end",
)
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,
)
svg.legend(
[(SERIES[i], text, "box") for i, (_, text) in enumerate(COMPONENTS)],
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)
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",
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()]
if budget:
values += [max(c["r_stars"] or [0]) for c in budget["stats"]]
max_k = max(
[
*by_k.keys(),
4,
*(c["replicas"] for c in (budget 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:
k, c = points[-1]
svg.text(
sx(k) + 10,
sy(c["r_star"]) + 4,
f"{c['r_star']:.0f}",
color=INK,
)
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"))
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
),
"- R* — медиана (min–max) по повторам; эффективность — R* / (k × "
"R*(1 × 1)), опора — 1 × 1 серии `b`; предельный CPU на запрос — "
"наклон CPU реплик от успешных запросов/с по ступеням, прошедшим "
"SLO; host_cost — он же против 1 × 1 серии `b` (порог "
f"{scaling.THRESHOLDS['host_cost']})",
"",
"## 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)} ядра.",
"",
]
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 %"
),
"",
]
return "\n".join(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:]))