Forward the lab 4 foundation and migrate the lab 5 pipeline to SQLAlchemy

This commit is contained in:
Artyom
2026-11-06 09:12:25 +03:00
parent ae9f60fe1f
commit 1217da5f5e
134 changed files with 8243 additions and 1409 deletions
+414
View File
@@ -0,0 +1,414 @@
import argparse
import collections
import csv
import statistics
import subprocess
import time
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
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_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",
]
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[1], "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(line for line in handle if not line.startswith("#")):
if row["disturbed"] == "True":
raise ValueError(f"disturbed run in accepted sample: {path}")
key = (Path(path).parent.name, row["variant"], row["config"])
groups[key][int(row["rate"])].append(row)
for key, rates in groups.items():
for rate, rows in rates.items():
if len(rows) < MIN_REPEATS or len({row["repeat"] for row in rows}) != len(rows):
raise ValueError(f"need at least three distinct repeats: {key}, {rate}")
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: 2 * sum(r["saturated"] == "True" for r in rows) > len(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",
]:
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
row["capacity_label"] = (
f"≥{capacity}" if capacity in (min(rates), max(rates)) else str(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 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(
[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,
label=label(key),
markeredgecolor=SURFACE,
markeredgewidth=1.5,
**look,
)
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])
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, categorical)
if not categorical:
axis.set_yscale("log")
axis.set_title(title)
for axis in axes:
axis.set_xlabel(
f"Вариант при {top} запросах/с" if categorical else "Предложенная нагрузка, запросов/с"
)
if not categorical:
axis.legend(loc="best", fontsize=9)
figure.suptitle(f"{series}: медиана повторов, усы — min–max", color=INK)
save(figure, out / f"{series}-throughput.png")
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}") for rate in rates]
axis.errorbar(
rates,
[v[0] for v in values],
yerr=[[v[1] for v in values], [v[2] for v in values]],
capsize=3,
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="best")
figure.suptitle("Медиана CPU по повторам, усы — min–max", color=INK)
save(figure, out / f"{series}-cpu.png")
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.errorbar(
4,
r["peak_ok_rps"],
yerr=[[r["peak_ok_rps"] - r["peak_ok_min"]], [r["peak_ok_max"] - r["peak_ok_rps"]]],
capsize=3,
linestyle="none",
label=f"серия A: {config}",
**look,
)
after = [r for r in rows if r["variant"] == "after"]
for r in after:
width = r["cpu_api_limit"] / 100
axis.errorbar(
width,
r["peak_ok_rps"],
yerr=[[r["peak_ok_rps"] - r["peak_ok_min"]], [r["peak_ok_max"] - r["peak_ok_rps"]]],
capsize=3,
linestyle="none",
label=f"{r['config']} после улучшения",
**VARIANTS["after"],
)
axis.set_xlabel("Суммарный CPU реплик API")
axis.set_ylabel("Пик успешных RPS")
axis.set_title("Пропускная способность от ресурсов API")
axis.set_xticks(cpus)
axis.legend(loc="best", fontsize=9)
save(figure, out / "scaling.png")
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))
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)
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_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} | "
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"\nОписательная USL-аппроксимация серии B: σ = {sigma:.4f}, κ = {kappa:.5f}, "
f"модельный N* = {best} CPU. Это не измеренный оптимум и не доказательство причины."
)
(out / "capacity.md").write_text(STAMP + "\n\n" + "\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()
+111
View File
@@ -0,0 +1,111 @@
import argparse
import json
import math
from pathlib import Path
from statistics import median
SIZES = (1000, 10000, 30000, 100000)
REPEATS = 3
LINEAR_BOUNDS = (7, 13)
RSS_BOUND = 1.1
DEEP_ORDER = 10
def main() -> None:
parser = argparse.ArgumentParser(description="Validate and compare complete reconciler series")
parser.add_argument("current", type=Path)
parser.add_argument("baseline", type=Path)
parser.add_argument("--out", required=True, type=Path)
args = parser.parse_args()
current = json.loads((args.current / "summary.json").read_text())
baseline = json.loads(args.baseline.read_text())
rows = {(r["objects"], r["mode"]): r for r in current["results"]}
old = {(r["objects"], r["mode"]): r for r in baseline["results"]}
expected = {(n, mode) for n in SIZES for mode in ("shallow", "deep")} | {(10000, "page100")}
if set(rows) != expected or any(r["runs"] != REPEATS for r in rows.values()):
parser.error("expected all nine points with three repeats")
comparisons = []
for (n, mode), row in sorted(rows.items()):
runs = []
for repeat in range(1, REPEATS + 1):
raw = json.loads((args.current / f"n{n}-{mode}-{repeat}.json").read_text())
runs.append(raw)
pages = math.ceil(n / (100 if mode == "page100" else 1000))
requests = {
"s3_list": pages,
"sql": pages,
"s3_head": 0,
"s3_delete": 0,
"s3_get": n if mode == "deep" else 0,
}
if (
raw["cost"]["requests"] != requests
or raw["screen"]["disturbed"]
or raw["scanned"] != {"objects": n, "rows": n}
or raw["errors"]
or raw["exit_code"]
or raw["skipped"]
or any(raw["found"].values())
or raw["commit"] != current["commit"]
):
parser.error(f"invalid accepted measurement: {n}/{mode}/{repeat}")
times = row["seconds_all"]
if (
times != [r["cost"]["seconds"] for r in runs]
or row["seconds_median"] != median(times)
or row["max_rss_mib_median"] != median(r["cost"]["max_rss_mib"] for r in runs)
):
parser.error(f"summary differs from raw measurements: {n}/{mode}")
previous = old[(n, mode)]
comparisons.append(
{
**row,
"seconds_range": [min(times), max(times)],
"spread_percent": round((max(times) - min(times)) / row["seconds_median"] * 100, 2),
"historical_seconds": previous["seconds_median"],
"historical_rss_mib": previous["max_rss_mib_median"],
"time_ratio_to_historical": row["seconds_median"] / previous["seconds_median"],
"rss_ratio_to_historical": row["max_rss_mib_median"]
/ previous["max_rss_mib_median"],
}
)
ratio = rows[(100000, "shallow")]["seconds_median"] / rows[(10000, "shallow")]["seconds_median"]
rss = (
rows[(100000, "shallow")]["max_rss_mib_median"]
/ rows[(1000, "shallow")]["max_rss_mib_median"]
)
deep = {
str(n): rows[(n, "deep")]["seconds_median"] / rows[(n, "shallow")]["seconds_median"]
for n in SIZES
}
result = {
"current_commit": current["commit"],
"baseline_commit": baseline["commit"],
"accepted": len(rows) * REPEATS,
"rejected_files": len(list((args.current / "disturbed").glob("*.json"))),
"requests_and_consistency_verified_for_every_raw_run": True,
"h2": {
"t100k_over_t10k": ratio,
"within_7_to_13": LINEAR_BOUNDS[0] <= ratio <= LINEAR_BOUNDS[1],
"rss100k_over_rss1k": rss,
"rss_growth_below_10_percent": rss < RSS_BOUND,
"deep_over_shallow": deep,
"deep_at_least_tenfold": all(v >= DEEP_ORDER for v in deep.values()),
},
"comparison": comparisons,
"limitation": (
"Historical comparison is not an isolated causal A/B experiment; "
"warm seeded data in Docker Desktop."
),
}
with args.out.open("x") as output:
output.write(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
print(
json.dumps(
{k: v for k, v in result.items() if k != "comparison"}, ensure_ascii=False, indent=2
)
)
if __name__ == "__main__":
main()
+50
View File
@@ -0,0 +1,50 @@
import argparse
import json
import statistics
from collections import defaultdict
from pathlib import Path
REPEATS = 3
def spread(values: list[float]) -> dict[str, float]:
return {"median": statistics.median(values), "min": min(values), "max": max(values)}
def main() -> None:
parser = argparse.ArgumentParser(description="Summarize three independent upload memory runs")
parser.add_argument("root", type=Path)
args = parser.parse_args()
groups = defaultdict(list)
for path in sorted(args.root.glob("run*/slots*.json")):
run = json.loads(path.read_text())
key = (run["upload_concurrency"], round(run["body_bytes"] / 1048576), run["concurrency"])
groups[key].append((path, run))
expected = {(s, m, c) for s in (3, 64) for m in (1, 10) for c in (10, 20, 40)}
if set(groups) != expected or any(len(runs) != REPEATS for runs in groups.values()):
parser.error("expected 12 points with three repeats each")
summary = []
for (slots, size, concurrency), rows in sorted(groups.items()):
runs = [r for _, r in rows]
summary.append(
{
"slots": slots,
"mib": size,
"concurrency": concurrency,
"files": [str(p.relative_to(args.root)) for p, _ in rows],
"commits": sorted({r["commit"] for r in runs}),
"status_codes": [r["status_codes"] for r in runs],
"api_peak_mib": spread(
[max(m["cgroup_memory_peak_mib"] for m in r["memory"].values()) for r in runs]
),
"lb_peak_mib": spread([r["lb"]["cgroup_memory_peak_mib"] for r in runs]),
"p95_ms": spread([r["latency_ms"]["p95"] for r in runs]),
"api_oom": sum(m["oom_kill"] for r in runs for m in r["memory"].values()),
"lb_oom": sum(r["lb"]["oom_kill"] for r in runs),
}
)
print(json.dumps(summary, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()