Cache the immutable message statement and validate scaling evidence

This commit is contained in:
Artyom
2026-11-05 23:59:54 +03:00
parent 05d69ba6ac
commit 57744c532c
7 changed files with 87 additions and 39 deletions
+48 -28
View File
@@ -2,6 +2,8 @@ import argparse
import collections
import csv
import statistics
import subprocess
import time
from pathlib import Path
import matplotlib.pyplot as plt
@@ -13,8 +15,13 @@ USL_POINTS = 3
MIN_REPEATS = 3
COMPONENTS = (("api", "API"), ("postgres", "PostgreSQL"), ("lb", "HAProxy"), ("k6", "k6"))
INK, MUTED, GRID, SURFACE = "#0b0b0b", "#52514e", "#e4e3df", "#fcfcfb"
STAMP = (
f"date: {time.strftime('%FT%TZ', time.gmtime())} commit: "
+ subprocess.check_output(["/usr/bin/env", "git", "rev-parse", "HEAD"], text=True).strip()
)
SUMMARY = (
"series variant config rates capacity_rate capacity_ok_rps capacity_p95_ms capacity_p99_ms "
"series variant config rates capacity_rate capacity_label "
"capacity_ok_rps capacity_p95_ms capacity_p99_ms "
"peak_rate peak_ok_rps peak_ok_min peak_ok_max peak_p95_ms peak_failed_ratio peak_dropped "
"cpu_api cpu_api_limit cpu_postgres cpu_lb cpu_k6 pg_active_avg pg_lock_avg pg_idle_tx_avg "
"pg_idle_avg pool_total pg_backends_max lb_qcur_max"
@@ -114,6 +121,9 @@ def summarize(key, rates):
for field, name in (("ok_rps", "ok_rps"), ("p95_ms", "p95_ms"), ("p99_ms", "p99_ms")):
row[f"capacity_{name}"] = med(rates[capacity], field)[0]
row["capacity_rate"] = capacity
row["capacity_label"] = (
f"≥{capacity}" if capacity in (min(rates), max(rates)) else str(capacity)
)
return row
@@ -123,13 +133,21 @@ def direct_label(axis, x, y, text):
)
def lines(axis, keys, groups, field):
for key in keys:
def save(figure, path):
figure.tight_layout()
figure.savefig(path, dpi=150, metadata={"Description": STAMP})
plt.close(figure)
def lines(axis, keys, groups, field, categorical=False):
for index, key in enumerate(keys):
rates = sorted(groups[key])
values = [med(groups[key][rate], field) for rate in rates]
look = VARIANTS.get(key[1]) or style(key[2])
if categorical:
look = {"color": PALETTE[index], "marker": MARKERS[index]}
axis.errorbar(
rates,
[index] if categorical else rates,
[v[0] for v in values],
yerr=[[v[1] for v in values], [v[2] for v in values]],
capsize=3,
@@ -138,28 +156,34 @@ def lines(axis, keys, groups, field):
markeredgewidth=1.5,
**look,
)
direct_label(axis, rates[-1], values[-1][0], label(key))
if not categorical:
direct_label(axis, rates[-1], values[-1][0], label(key))
if categorical:
axis.set_xticks(range(len(keys)), [label(k) for k in keys], rotation=25, ha="right")
def throughput(series, keys, groups, out):
figure, axes = plt.subplots(1, 3, figsize=(17, 4.8))
top = max(rate for key in keys for rate in groups[key])
axes[0].plot(
[0, top], [0, top], color=MUTED, linestyle="--", linewidth=1, label="RPS = нагрузка"
)
lines(axes[0], keys, groups, "ok_rps")
categorical = len({rate for key in keys for rate in groups[key]}) == 1
if not categorical:
axes[0].plot(
[0, top], [0, top], color=MUTED, linestyle="--", linewidth=1, label="RPS = нагрузка"
)
lines(axes[0], keys, groups, "ok_rps", categorical)
axes[0].set_title("Успешные RPS")
for axis, field, title in ((axes[1], "p95_ms", "p95, мс"), (axes[2], "p99_ms", "p99, мс")):
lines(axis, keys, groups, field)
lines(axis, keys, groups, field, categorical)
axis.set_yscale("log")
axis.set_title(title)
for axis in axes:
axis.set_xlabel("Предложенная нагрузка, запросов/с")
axis.legend(loc="upper left", fontsize=9)
figure.suptitle(f"{series}: медиана 3 повторов, усы — min–max", color=INK)
figure.tight_layout()
figure.savefig(out / f"{series}-throughput.png", dpi=150)
plt.close(figure)
axis.set_xlabel(
f"Вариант при {top} запросах/с" if categorical else "Предложенная нагрузка, запросов/с"
)
if not categorical:
axis.legend(loc="upper left", fontsize=9)
figure.suptitle(f"{series}: медиана повторов, усы — min–max", color=INK)
save(figure, out / f"{series}-throughput.png")
def cpu(series, keys, groups, out):
@@ -176,9 +200,7 @@ def cpu(series, keys, groups, out):
axis.set_title(f"CPU, % ядра — {label(key)}")
axis.set_xlabel("Предложенная нагрузка, запросов/с")
axis.legend(fontsize=8, loc="upper left")
figure.tight_layout()
figure.savefig(out / f"{series}-cpu.png", dpi=150)
plt.close(figure)
save(figure, out / f"{series}-cpu.png")
def usl(points):
@@ -248,9 +270,7 @@ def scaling(rows, out):
axis.set_title("Пропускная способность от ресурсов API")
axis.set_xticks(cpus)
axis.legend(loc="upper left", fontsize=9)
figure.tight_layout()
figure.savefig(out / "scaling.png", dpi=150)
plt.close(figure)
save(figure, out / "scaling.png")
return fit
@@ -288,13 +308,12 @@ def utilisation(rows, out):
axis.set_ylim(0, 115)
axis.set_title("Загрузка звеньев в точке пика успешных RPS", pad=34)
axis.legend(fontsize=9, ncols=4, loc="lower left", bbox_to_anchor=(0, 1.0))
figure.tight_layout()
figure.savefig(out / "utilisation.png", dpi=150)
plt.close(figure)
save(figure, out / "utilisation.png")
def write(rows, out, fit):
with (out / "capacity.csv").open("w", newline="") as handle:
handle.write(f"# {STAMP}\n")
writer = csv.DictWriter(handle, fieldnames=SUMMARY, extrasaction="ignore")
writer.writeheader()
writer.writerows(rows)
@@ -305,7 +324,7 @@ def write(rows, out, fit):
lines_ = [head, "|" + " --- |" * 11]
for r in rows:
lines_.append(
f"| {r['series']} | {r['variant']} | {r['config']} | {r.get('capacity_rate', '—')} | "
f"| {r['series']} | {r['variant']} | {r['config']} | {r.get('capacity_label', '—')} | "
f"{r.get('capacity_p95_ms', '—')} | {r['peak_ok_rps']:.0f} "
f"[{r['peak_ok_min']:.0f}–{r['peak_ok_max']:.0f}] | {r['peak_rate']} | "
f"{r['cpu_api']:.0f} / {r['cpu_api_limit']} | {r['cpu_postgres']:.0f} | "
@@ -315,9 +334,10 @@ def write(rows, out, fit):
_, sigma, kappa, peak = fit
best = f"{peak:.1f}" if peak else "—"
lines_.append(
f"\nUSL по серии B: σ = {sigma:.4f}, κ = {kappa:.5f}, пик при N* = {best} CPU"
f"\nОписательная USL-аппроксимация серии B: σ = {sigma:.4f}, κ = {kappa:.5f}, "
f"модельный N* = {best} CPU. Это не измеренный оптимум и не доказательство причины."
)
(out / "capacity.md").write_text("\n".join(lines_) + "\n")
(out / "capacity.md").write_text(STAMP + "\n\n" + "\n".join(lines_) + "\n")
print("\n".join(lines_))