"""Сводка и графики трека 1 из evidence/track1/*.json (stdlib; SVG без внешних библиотек). Графики: одна ось Y на график, линии 2 px, маркеры ≥ 8 px разной формы (вторичное кодирование), подписи серий у конца линии + легенда, всплывающие подсказки через , таблица в summary.md. """ import json import statistics from pathlib import Path # Категориальная палитра (validated: dataviz reference palette, slots 1–4, light surface). SERIES = ["#2a78d6", "#eb6834", "#1baf7a", "#eda100"] SURFACE, INK, INK2, GRID = "#fcfcfb", "#0b0b0b", "#52514e", "#e4e3df" SHAPES = ["circle", "square", "triangle", "diamond"] def load(out): runs = {} for f in sorted(Path(out).glob("*.json")): if f.name.startswith("summary"): continue d = json.loads(f.read_text()) base = d["config"].rsplit("-r", 1)[0] if d.get("repeat", 1) > 1 else d["config"] runs.setdefault(base, []).append(d) return runs def metrics(d): steps = d["steps"] err_ok = [s for s in steps if s["error_ratio"] <= 0.01] return { "capacity_slo": d["capacity_at_slo_rps"], "capacity_err1": max((s["throughput_ok_rps"] for s in err_ok), default=0), "peak": d["peak_ok_rps"], } def marker(shape, x, y, color, tip): t = "<title>%s" % tip if shape == "circle": return '%s' % (x, y, color, SURFACE, t) if shape == "square": return '%s' % (x - 4.5, y - 4.5, color, SURFACE, t) if shape == "triangle": return '%s' % ( x, y - 5.5, x + 5.5, y + 4.5, x - 5.5, y + 4.5, color, SURFACE, t) return '%s' % ( x, y - 6, x + 6, y, x, y + 6, x - 6, y, color, SURFACE, t) def line_chart(path, title, subtitle, series, xlabel, ylabel, ymax=None, diagonal=False, refline=None): """series: [(label, [(x, y, tip)])].""" W, H, L, R, T, B = 760, 400, 64, 190, 64, 52 xs = [p[0] for _, pts in series for p in pts] ys = [p[1] for _, pts in series for p in pts] xmax = max(xs) * 1.05 ymax = ymax or max(ys) * 1.12 sx = lambda v: L + (W - L - R) * v / xmax # noqa: E731 sy = lambda v: H - B - (H - T - B) * v / ymax # noqa: E731 o = ['' % (W, H), '' % SURFACE, '%s' % (L, INK, title), '%s' % (L, INK2, subtitle)] step = 10 ** len(str(int(ymax / 5))) / 10 step = next(s for s in (step, step * 2, step * 2.5, step * 5, step * 10) if ymax / s <= 6) v = 0 while v <= ymax: o.append('' % (L, W - R, sy(v), sy(v), GRID)) o.append('%g' % (L - 8, sy(v) + 4, INK2, v)) v += step xstep = next(s for s in (50, 100, 200, 250, 500) if xmax / s <= 9) v = 0 while v <= xmax: o.append('%g' % (sx(v), H - B + 18, INK2, v)) v += xstep o.append('' % (L, W - R, H - B, H - B, INK2)) o.append('%s' % ((L + W - R) / 2, H - 12, INK2, xlabel)) o.append('%s' % ( (T + H - B) / 2, INK2, (T + H - B) / 2, ylabel)) if refline: val, text = refline o.append('' % (L, W - R, sy(val), sy(val), INK2)) o.append('%s' % (W - R - 4, sy(val) - 5, INK2, text)) if diagonal: m = min(xmax, ymax) o.append('' % ( sx(0), sy(0), sx(m), sy(m), INK2)) o.append('пунктир: полезная = поданная' % (sx(m * 0.92) - 8, sy(m * 0.92), INK2)) for i, (label, pts) in enumerate(series): c = SERIES[i % len(SERIES)] pts = sorted(pts) o.append('' % ( c, " ".join("%.1f,%.1f" % (sx(x), sy(y)) for x, y, _ in pts))) for x, y, tip in pts: o.append(marker(SHAPES[i % 4], sx(x), sy(y), c, "%s — %s" % (label, tip))) # легенда сверху справа lx = W - R + 10 for i, (label, _) in enumerate(series): y = T + 4 + i * 18 o.append(marker(SHAPES[i % 4], lx + 6, y, SERIES[i % len(SERIES)], label)) o.append('%s' % (lx + 16, y + 4, INK2, label)) o.append("") Path(path).write_text("\n".join(o) + "\n") def bar_chart(path, title, subtitle, groups, ylabel): """groups: [(label, value, tip)] — одна серия, одна ось.""" W, H, L, R, T, B = 760, 360, 64, 24, 64, 70 ymax = max(v for _, v, _ in groups) * 1.18 n = len(groups) bw = (W - L - R) / n sy = lambda v: H - B - (H - T - B) * v / ymax # noqa: E731 o = ['' % (W, H), '' % SURFACE, '%s' % (L, INK, title), '%s' % (L, INK2, subtitle)] step = next(s for s in (25, 50, 100, 200) if ymax / s <= 6) v = 0 while v <= ymax: o.append('' % (L, W - R, sy(v), sy(v), GRID)) o.append('%g' % (L - 8, sy(v) + 4, INK2, v)) v += step for i, (label, val, tip) in enumerate(groups): x = L + i * bw + bw * 0.22 w = bw * 0.56 y = sy(val) h = H - B - y r = min(4, w / 2) o.append('%s' % ( x, H - B, y + r, x, y, x + r, y, x + w - r, x + w, y, x + w, y + r, H - B, SERIES[0], tip)) o.append('%g' % (x + w / 2, y - 6, INK, val)) for k, part in enumerate(label.split("\n")): o.append('%s' % ( x + w / 2, H - B + 16 + 13 * k, INK2, part)) o.append('' % (L, W - R, H - B, H - B, INK2)) o.append('%s' % ( (T + H - B) / 2, INK2, (T + H - B) / 2, ylabel)) o.append("") Path(path).write_text("\n".join(o) + "\n") def build(out): out = Path(out) runs = load(out) table, summary = [], {} for name, ds in sorted(runs.items()): ms = [metrics(d) for d in ds] agg = {k: statistics.median(m[k] for m in ms) for k in ms[0]} spread = {k: [m[k] for m in ms] for k in ms[0]} p = ds[0]["params"] sat = max(ds[0]["steps"], key=lambda s: s["throughput_ok_rps"]) res = sat["resources"] summary[name] = {"series": ds[0]["series"], "repeats": len(ds), "median": agg, "values": spread, "params": {k: p[k] for k in ("API_REPLICAS", "API_CPUS", "API_MEMORY", "API_CPUSET", "DB_POOL_MAX", "LB_SERVER_MAXCONN", "LB_QUEUE_TIMEOUT") if k in p}, "at_peak": {"rate": sat["rate"], "cpu_percent": res["cpu_percent_mean"], "cfs_throttled": res.get("api_cfs_throttled_period_ratio"), "lb_queue_max": res["lb_queue_max"], "pg_connections": res["pg_connections_mean"]}} table.append("| %s | %s | %s×%s CPU, %s | pool %s, maxconn %s, queue %s | %s | %s | %s | %s |" % ( name, ds[0]["series"], p["API_REPLICAS"], p["API_CPUS"], p["API_MEMORY"], p.get("DB_POOL_MAX"), p.get("LB_SERVER_MAXCONN"), p.get("LB_QUEUE_TIMEOUT"), "/".join("%g" % v for v in spread["capacity_slo"]), "/".join("%g" % v for v in spread["capacity_err1"]), "/".join("%g" % v for v in spread["peak"]), len(ds))) (out / "summary.json").write_text(json.dumps(summary, ensure_ascii=False, indent=1) + "\n") head = ("| Конфигурация | Серия | Реплики × CPU, RAM | Пул / LB | Ёмкость при SLO, rps | Макс. при ошибках ≤1 %, rps | " "Пик полезной, rps | Повторов |\n|---|---|---|---|---|---|---|---|") (out / "summary.md").write_text(head + "\n" + "\n".join(table) + "\n") def curve(name, field): """Медиана по повторам для каждой ступени rate (повторы могут остановиться на разных ступенях).""" by_rate = {} for d in runs[name]: for s in d["steps"]: by_rate.setdefault(s["rate"], []).append(s) pts = [] for rate, ss in sorted(by_rate.items()): ok = statistics.median(s["throughput_ok_rps"] for s in ss) p99 = statistics.median(s["latency_ms"]["p99"] for s in ss) err = statistics.median(s["error_ratio"] for s in ss) pts.append((rate, ok if field == "ok" else p99, "%g req/s: полезная %.0f rps, ошибок %.1f %%, p99 %.0f мс (медиана %d прогонов)" % ( rate, ok, 100 * err, p99, len(ss)))) return pts fixed = [("1 × 1.0 CPU", "fixed-1x1.00"), ("2 × 0.5 CPU", "fixed-2x0.50"), ("4 × 0.25 CPU", "fixed-4x0.25")] fixed = [f for f in fixed if f[1] in runs] if fixed: line_chart(out / "fixed-cpu-goodput.svg", "Фиксированный бюджет API (1 CPU, 512 MiB): дробление на реплики", "Полезная пропускная способность (2xx), медиана 3 прогонов; open-loop, смесь 70/20/10", [(l, curve(n, "ok")) for l, n in fixed], "Поданная нагрузка, req/s", "Полезная, rps", diagonal=True) line_chart(out / "fixed-cpu-p99.svg", "Фиксированный бюджет API: хвост задержки", "p99 успешных ответов от запланированного момента, медиана 3 прогонов", [(l, [(x, min(y, 2500), t) for x, y, t in curve(n, "p99")]) for l, n in fixed], "Поданная нагрузка, req/s", "p99, мс (обрезано на 2500)", ymax=2600, refline=(250, "SLO 250 мс")) adm = [("без лимита (maxconn 0)", "fixed-1x1.00"), ("maxconn 10", "admission-1x1.00-mc10"), ("maxconn 20", "admission-1x1.00-mc20"), ("maxconn 10, queue 250 мс", "admission-1x1.00-mc10-q250")] adm = [a for a in adm if a[1] in runs] if len(adm) > 1: line_chart(out / "admission-goodput.svg", "maxconn на LB против перегрузки (1 × 1.0 CPU)", "Полезная пропускная способность; пул 10; без лимита и maxconn 10 — медиана 3 прогонов", [(l, curve(n, "ok")) for l, n in adm], "Поданная нагрузка, req/s", "Полезная, rps", diagonal=True) bars = [("growth-1x0.25", "1×0.25\n(0.25 CPU)"), ("growth-2x0.25", "2×0.25\n(0.5 CPU)"), ("growth-3x0.25", "3×0.25\n(0.75 CPU)"), ("fixed-4x0.25", "4×0.25\n(1 CPU)"), ("fixed-1x1.00", "1×1.0\n(1 CPU)"), ("machine-2x1.00", "2×1.0\n(2 vCPU хоста)"), ("machine-4x1.00", "4×1.0\n(2 vCPU хоста)")] bars = [(summary[n]["median"]["peak"], l, n) for n, l in bars if n in summary] if bars: bar_chart(out / "growth-peak.svg", "Рост ресурсов: пик полезной пропускной способности", "Ступени 0.25 CPU на изолированном ядре, затем вся машина (2 vCPU, общие с БД/LB/генератором)", [(l, round(v), "%s: пик %.0f rps" % (n, v)) for v, l, n in bars], "Пик полезной, rps") print((out / "summary.md").read_text())