forked from mtusi2026/mtusi2026lab3
The first run of series A was spoiled by other stands on the same Docker VM: an image build of another lab is invisible to docker stats, yet it took CPU from the pinned replicas, doubled their CPU per request and stalled the Docker API for fifteen minutes in the middle of two steps. docker-stats.sh now also samples the VM-wide /proc/stat counters, so a step records the CPU of the whole VM, the part not used by this project, the largest gap between samples and its wall time. A step with more than one foreign core, a gap over 10 s or a k6 run 90 s longer than its load is written to disturbed.csv and repeated after the VM has been quiet for 15 s, up to three times. On a quiet VM the foreign part is 2 % of a core at 600 rps and 25 % at 4 560 rps, which is Docker networking and the samplers themselves. The growth series starts at 300 rps per CPU and searches up to 9 000, so its knees are found with fewer idle steps. The plots fit the universal scalability law to the growth series. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
326 lines
13 KiB
Python
326 lines
13 KiB
Python
import argparse
|
||
import collections
|
||
import csv
|
||
import statistics
|
||
from pathlib import Path
|
||
|
||
import matplotlib.pyplot as plt
|
||
|
||
ORDER = ("1x1", "2x1", "4x1", "8x1", "2x2", "1x4")
|
||
PALETTE = ("#2a78d6", "#eb6834", "#1baf7a", "#eda100", "#e87ba4", "#008300", "#6250d6", "#e34948")
|
||
MARKERS = ("o", "s", "^", "D", "v", "P", "X", "*")
|
||
USL_POINTS = 3
|
||
COMPONENTS = (("api", "API"), ("postgres", "PostgreSQL"), ("lb", "HAProxy"), ("k6", "k6"))
|
||
INK, MUTED, GRID, SURFACE = "#0b0b0b", "#52514e", "#e4e3df", "#fcfcfb"
|
||
SUMMARY = (
|
||
"series variant config rates capacity_rate 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"
|
||
).split()
|
||
|
||
plt.rcParams.update(
|
||
{
|
||
"figure.facecolor": SURFACE,
|
||
"axes.facecolor": SURFACE,
|
||
"axes.edgecolor": MUTED,
|
||
"axes.labelcolor": MUTED,
|
||
"axes.titlecolor": INK,
|
||
"axes.titleweight": "bold",
|
||
"axes.grid": True,
|
||
"grid.color": GRID,
|
||
"grid.linewidth": 0.8,
|
||
"axes.spines.top": False,
|
||
"axes.spines.right": False,
|
||
"xtick.color": MUTED,
|
||
"ytick.color": MUTED,
|
||
"legend.frameon": False,
|
||
"lines.linewidth": 2,
|
||
"lines.markersize": 7,
|
||
"font.size": 10,
|
||
}
|
||
)
|
||
|
||
|
||
VARIANTS = {
|
||
"before": {"color": MUTED, "marker": "o"},
|
||
"after": {"color": PALETTE[0], "marker": "s"},
|
||
}
|
||
|
||
|
||
def style(name):
|
||
index = ORDER.index(name) if name in ORDER else len(ORDER)
|
||
return {"color": PALETTE[index], "marker": MARKERS[index]}
|
||
|
||
|
||
def load(paths):
|
||
groups = collections.defaultdict(lambda: collections.defaultdict(list))
|
||
for path in paths:
|
||
with Path(path).open() as handle:
|
||
for row in csv.DictReader(handle):
|
||
key = (Path(path).parent.name, row["variant"], row["config"])
|
||
groups[key][int(row["rate"])].append(row)
|
||
return groups
|
||
|
||
|
||
def med(rows, field):
|
||
values = [float(r[field]) for r in rows]
|
||
middle = statistics.median(values)
|
||
return middle, middle - min(values), max(values) - middle
|
||
|
||
|
||
def label(key):
|
||
return key[2] if key[1] == "base" else f"{key[2]} {key[1]}"
|
||
|
||
|
||
def summarize(key, rates):
|
||
series, variant, config = key
|
||
sat = {rate: any(r["saturated"] == "True" for r in rows) for rate, rows in rates.items()}
|
||
capacity = max((rate for rate, bad in sat.items() if not bad), default=None)
|
||
peak = max(rates, key=lambda rate: med(rates[rate], "ok_rps")[0])
|
||
top, first = rates[peak], rates[peak][0]
|
||
replicas, workers, pool = (int(first[f]) for f in ("replicas", "workers", "pool"))
|
||
row = {
|
||
"series": series,
|
||
"variant": variant,
|
||
"config": config,
|
||
"rates": " ".join(str(r) for r in sorted(rates)),
|
||
"peak_rate": peak,
|
||
"peak_ok_rps": med(top, "ok_rps")[0],
|
||
"peak_ok_min": min(float(r["ok_rps"]) for r in top),
|
||
"peak_ok_max": max(float(r["ok_rps"]) for r in top),
|
||
"peak_p95_ms": med(top, "p95_ms")[0],
|
||
"peak_failed_ratio": med(top, "failed_ratio")[0],
|
||
"peak_dropped": med(top, "dropped_iterations")[0],
|
||
"cpu_api_limit": round(replicas * float(first["cpus"]) * 100),
|
||
"pool_total": replicas * workers * pool,
|
||
}
|
||
for field in (
|
||
"cpu_api cpu_postgres cpu_lb cpu_k6 pg_active_avg pg_lock_avg pg_idle_tx_avg "
|
||
"pg_idle_avg pg_backends_max lb_qcur_max"
|
||
).split():
|
||
row[field] = round(med(top, field)[0], 2)
|
||
if capacity is not None:
|
||
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
|
||
return row
|
||
|
||
|
||
def direct_label(axis, x, y, text):
|
||
axis.annotate(
|
||
text, (x, y), xytext=(6, 0), textcoords="offset points", va="center", color=INK, fontsize=9
|
||
)
|
||
|
||
|
||
def lines(axis, keys, groups, field):
|
||
for key in keys:
|
||
rates = sorted(groups[key])
|
||
values = [med(groups[key][rate], field) for rate in rates]
|
||
look = VARIANTS.get(key[1]) or style(key[2])
|
||
axis.errorbar(
|
||
rates,
|
||
[v[0] for v in values],
|
||
yerr=[[v[1] for v in values], [v[2] for v in values]],
|
||
capsize=3,
|
||
label=label(key),
|
||
markeredgecolor=SURFACE,
|
||
markeredgewidth=1.5,
|
||
**look,
|
||
)
|
||
direct_label(axis, rates[-1], values[-1][0], label(key))
|
||
|
||
|
||
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")
|
||
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)
|
||
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)
|
||
|
||
|
||
def cpu(series, keys, groups, out):
|
||
figure, axes = plt.subplots(1, len(keys), figsize=(5 * len(keys), 4.2), squeeze=False)
|
||
for axis, key in zip(axes[0], keys, strict=True):
|
||
rates = sorted(groups[key])
|
||
first = groups[key][rates[0]][0]
|
||
limit = int(first["replicas"]) * float(first["cpus"]) * 100
|
||
for index, (field, name) in enumerate(COMPONENTS):
|
||
values = [med(groups[key][rate], f"cpu_{field}")[0] for rate in rates]
|
||
axis.plot(rates, values, color=PALETTE[index], marker=MARKERS[index], label=name)
|
||
axis.axhline(limit, color=PALETTE[0], linestyle=":", linewidth=1.2, label="лимит API")
|
||
axis.axhline(200, color=PALETTE[1], linestyle=":", linewidth=1.2, label="лимит PostgreSQL")
|
||
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)
|
||
|
||
|
||
def usl(points):
|
||
base = points[0][1] / points[0][0]
|
||
pairs = [(n - 1, n * (n - 1), n * base / x - 1) for n, x in points]
|
||
a, b, c = (sum(p[i] * p[j] for p in pairs) for i, j in ((0, 0), (0, 1), (1, 1)))
|
||
d, e = (sum(p[i] * p[2] for p in pairs) for i in (0, 1))
|
||
det = a * c - b * b
|
||
sigma, kappa = (d * c - b * e) / det, (a * e - b * d) / det
|
||
peak = ((1 - sigma) / kappa) ** 0.5 if kappa > 0 else None
|
||
return base, sigma, kappa, peak
|
||
|
||
|
||
def scaling(rows, out):
|
||
by = {(r["series"], r["config"]): r for r in rows if r["variant"] == "base"}
|
||
growth = [
|
||
by.get(("series-b", c)) or by.get(("series-a", c)) for c in ("1x1", "2x1", "4x1", "8x1")
|
||
]
|
||
growth = [r for r in growth if r]
|
||
if not growth:
|
||
return
|
||
figure, axis = plt.subplots(figsize=(7.5, 4.8))
|
||
cpus = [r["cpu_api_limit"] / 100 for r in growth]
|
||
unit = growth[0]["peak_ok_rps"] / cpus[0]
|
||
axis.plot(cpus, [unit * c for c in cpus], color=MUTED, linestyle="--", linewidth=1)
|
||
axis.annotate("линейный рост", (cpus[-1], unit * cpus[-1]), color=MUTED, fontsize=9)
|
||
fit = None
|
||
if len(growth) >= USL_POINTS:
|
||
fit = usl([(c, r["peak_ok_rps"]) for c, r in zip(cpus, growth, strict=True)])
|
||
base, sigma, kappa, _ = fit
|
||
grid = [1 + i * (cpus[-1] - 1) / 60 for i in range(61)]
|
||
model = [base * n / (1 + sigma * (n - 1) + kappa * n * (n - 1)) for n in grid]
|
||
axis.plot(
|
||
grid, model, color=PALETTE[6], linewidth=1.5, label=f"USL: σ={sigma:.3f}, κ={kappa:.4f}"
|
||
)
|
||
axis.errorbar(
|
||
cpus,
|
||
[r["peak_ok_rps"] for r in growth],
|
||
yerr=[
|
||
[r["peak_ok_rps"] - r["peak_ok_min"] for r in growth],
|
||
[r["peak_ok_max"] - r["peak_ok_rps"] for r in growth],
|
||
],
|
||
color=PALETTE[0],
|
||
marker="o",
|
||
capsize=3,
|
||
label="серия B: N × 1 CPU",
|
||
)
|
||
for r, c in zip(growth, cpus, strict=True):
|
||
direct_label(axis, c, r["peak_ok_rps"], r["config"])
|
||
for config in ("2x2", "1x4"):
|
||
r = by.get(("series-a", config))
|
||
if r:
|
||
look = style(config)
|
||
axis.plot(4, r["peak_ok_rps"], linestyle="none", label=f"серия A: {config}", **look)
|
||
axis.set_xlabel("Суммарный CPU реплик API")
|
||
axis.set_ylabel("Пик успешных RPS")
|
||
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)
|
||
return fit
|
||
|
||
|
||
def utilisation(rows, out):
|
||
picked = [r for r in rows if r["variant"] == "base" and r["series"] != "compare"]
|
||
seen, ordered = set(), []
|
||
for config in ORDER:
|
||
for r in picked:
|
||
if r["config"] == config and config not in seen:
|
||
seen.add(config)
|
||
ordered.append(r)
|
||
if not ordered:
|
||
return
|
||
metrics = (
|
||
("CPU API, % лимита", lambda r: 100 * r["cpu_api"] / r["cpu_api_limit"]),
|
||
("CPU PostgreSQL, % лимита", lambda r: r["cpu_postgres"] / 2),
|
||
("CPU HAProxy, % лимита", lambda r: r["cpu_lb"]),
|
||
("ждут блокировку, % пула", lambda r: 100 * r["pg_lock_avg"] / r["pool_total"]),
|
||
)
|
||
figure, axis = plt.subplots(figsize=(10, 4.8))
|
||
width = 0.8 / len(metrics)
|
||
for index, (name, value) in enumerate(metrics):
|
||
xs = [i + (index - 1.5) * width for i in range(len(ordered))]
|
||
axis.bar(
|
||
xs,
|
||
[value(r) for r in ordered],
|
||
width=width - 0.02,
|
||
color=PALETTE[index],
|
||
label=name,
|
||
edgecolor=SURFACE,
|
||
)
|
||
axis.set_xticks(range(len(ordered)), [r["config"] for r in ordered])
|
||
axis.axhline(100, color=MUTED, linestyle=":", linewidth=1)
|
||
axis.set_ylabel("%")
|
||
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)
|
||
|
||
|
||
def write(rows, out, fit):
|
||
with (out / "capacity.csv").open("w", newline="") as handle:
|
||
writer = csv.DictWriter(handle, fieldnames=SUMMARY, extrasaction="ignore")
|
||
writer.writeheader()
|
||
writer.writerows(rows)
|
||
head = (
|
||
"| серия | вариант | конфигурация | потолок по SLO, rps | p95 там, мс | пик успешных RPS "
|
||
"[min–max] | при нагрузке | CPU API / лимит | CPU PG | CPU LB | ждут блокировку / пул |"
|
||
)
|
||
lines_ = [head, "|" + " --- |" * 11]
|
||
for r in rows:
|
||
lines_.append(
|
||
f"| {r['series']} | {r['variant']} | {r['config']} | {r.get('capacity_rate', '—')} | "
|
||
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} | "
|
||
f"{r['cpu_lb']:.0f} | {r['pg_lock_avg']:.1f} / {r['pool_total']} |"
|
||
)
|
||
if fit:
|
||
_, sigma, kappa, peak = fit
|
||
best = f"{peak:.1f}" if peak else "—"
|
||
lines_.append(
|
||
f"\nUSL по серии B: σ = {sigma:.4f}, κ = {kappa:.5f}, пик при N* = {best} CPU"
|
||
)
|
||
(out / "capacity.md").write_text("\n".join(lines_) + "\n")
|
||
print("\n".join(lines_))
|
||
|
||
|
||
def main():
|
||
parser = argparse.ArgumentParser(description="Графики и сводка трека 1 из summary.csv")
|
||
parser.add_argument("summaries", nargs="+")
|
||
parser.add_argument("--out", required=True)
|
||
args = parser.parse_args()
|
||
out = Path(args.out)
|
||
out.mkdir(parents=True, exist_ok=True)
|
||
groups = load(args.summaries)
|
||
rank = {name: index for index, name in enumerate(ORDER)}
|
||
keys = sorted(groups, key=lambda k: (k[0], k[1] != "before", rank.get(k[2], 99)))
|
||
rows = [summarize(key, groups[key]) for key in keys]
|
||
for series in dict.fromkeys(k[0] for k in keys):
|
||
chosen = [k for k in keys if k[0] == series]
|
||
throughput(series, chosen, groups, out)
|
||
cpu(series, chosen, groups, out)
|
||
fit = scaling(rows, out)
|
||
utilisation(rows, out)
|
||
write(rows, out, fit)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|