196 lines
8.4 KiB
Python
196 lines
8.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Summarise track-1 runs and render dependency-free SVG charts."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import statistics
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SUMMARY = ROOT / "evidence" / "track1_summary.json"
|
|
OUT_JSON = ROOT / "evidence" / "track1_report.json"
|
|
OUT_TXT = ROOT / "evidence" / "track1_report.txt"
|
|
FIXED_SVG = ROOT / "evidence" / "track1_fixed_total.svg"
|
|
GROWTH_SVG = ROOT / "evidence" / "track1_growth.svg"
|
|
|
|
|
|
def median(values):
|
|
return round(statistics.median(values), 3) if values else None
|
|
|
|
|
|
def profile_summary(items):
|
|
grouped = {}
|
|
for item in items:
|
|
grouped.setdefault(item["profile"]["name"], []).append(item)
|
|
result = []
|
|
for name, runs in grouped.items():
|
|
rps = [run["successful_rps"] for run in runs]
|
|
p95 = [run["successful_latency_ms"]["p95"] for run in runs]
|
|
errors = sum(run["failed_requests"] for run in runs)
|
|
result.append(
|
|
{
|
|
"profile": name,
|
|
"replicas": runs[0]["profile"]["replicas"],
|
|
"cpu_per_replica": runs[0]["profile"]["cpu"],
|
|
"memory_per_replica": runs[0]["profile"]["mem"],
|
|
"repetitions": len(runs),
|
|
"rps_median": median(rps),
|
|
"rps_min": min(rps),
|
|
"rps_max": max(rps),
|
|
"p95_ms_median": median(p95),
|
|
"errors_total": errors,
|
|
}
|
|
)
|
|
order = {
|
|
"1x2.0cpu_1g": 0,
|
|
"2x1.0cpu_512m": 1,
|
|
"4x0.5cpu_256m": 2,
|
|
"4x1.0cpu_512m": 3,
|
|
"4x2.0cpu_1g": 4,
|
|
}
|
|
return sorted(result, key=lambda item: order.get(item["profile"], 99))
|
|
|
|
|
|
def metrics_summary(run):
|
|
path = ROOT / run["metrics_file"]
|
|
samples = json.loads(path.read_text(encoding="utf-8"))
|
|
api_cpu, pg_cpu, lb_cpu, connections, active, waiting = [], [], [], [], [], []
|
|
for sample in samples:
|
|
api = [c for c in sample["containers"] if "-api-" in c["name"]]
|
|
pg = [c for c in sample["containers"] if "-postgres-" in c["name"]]
|
|
lb = [c for c in sample["containers"] if "-lb-" in c["name"]]
|
|
api_cpu.append(sum(float(c["cpu_percent"].rstrip("%")) for c in api))
|
|
pg_cpu.append(float(pg[0]["cpu_percent"].rstrip("%")) if pg else 0.0)
|
|
lb_cpu.append(float(lb[0]["cpu_percent"].rstrip("%")) if lb else 0.0)
|
|
connections.append(sample["postgres"]["connections"])
|
|
active.append(sample["postgres"]["active"])
|
|
waiting.append(sample["postgres"]["waiting"])
|
|
return {
|
|
"samples": len(samples),
|
|
"api_cpu_percent_avg": round(statistics.mean(api_cpu), 2) if api_cpu else None,
|
|
"api_cpu_percent_max": round(max(api_cpu), 2) if api_cpu else None,
|
|
"postgres_cpu_percent_max": round(max(pg_cpu), 2) if pg_cpu else None,
|
|
"lb_cpu_percent_max": round(max(lb_cpu), 2) if lb_cpu else None,
|
|
"postgres_connections_max": max(connections) if connections else None,
|
|
"postgres_active_max": max(active) if active else None,
|
|
"postgres_waiting_max": max(waiting) if waiting else None,
|
|
}
|
|
|
|
|
|
def bar_svg(title: str, labels: list[str], values: list[float], errors: list[float], path: Path, unit: str):
|
|
width, height = 760, 420
|
|
left, bottom, top, right = 80, 70, 50, 30
|
|
plot_w, plot_h = width - left - right, height - top - bottom
|
|
max_value = max(values) * 1.2 if values else 1
|
|
bars = []
|
|
for index, (label, value, error) in enumerate(zip(labels, values, errors)):
|
|
bar_w = plot_w / (len(values) * 1.8)
|
|
x = left + plot_w * (index + 0.5) / len(values) - bar_w / 2
|
|
h = plot_h * value / max_value
|
|
y = top + plot_h - h
|
|
bars.append(
|
|
f'<rect x="{x:.1f}" y="{y:.1f}" width="{bar_w:.1f}" height="{h:.1f}" fill="#2f6f9f"/>'
|
|
f'<text x="{x + bar_w/2:.1f}" y="{y - 8:.1f}" text-anchor="middle" font-size="12">{value:.1f}</text>'
|
|
f'<text x="{x + bar_w/2:.1f}" y="{top + plot_h + 20:.1f}" text-anchor="middle" font-size="12">{label}</text>'
|
|
f'<line x1="{x:.1f}" y1="{top + plot_h:.1f}" x2="{x:.1f}" y2="{top:.1f}" stroke="#ddd"/>'
|
|
)
|
|
svg = f'''<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}">
|
|
<rect width="100%" height="100%" fill="white"/>
|
|
<text x="{width/2}" y="28" text-anchor="middle" font-size="18" font-family="sans-serif">{title}</text>
|
|
<text x="20" y="{top + plot_h/2}" transform="rotate(-90 20 {top + plot_h/2})" text-anchor="middle" font-size="12" font-family="sans-serif">{unit}</text>
|
|
<line x1="{left}" y1="{top + plot_h}" x2="{left + plot_w}" y2="{top + plot_h}" stroke="#333"/>
|
|
<line x1="{left}" y1="{top}" x2="{left}" y2="{top + plot_h}" stroke="#333"/>
|
|
{''.join(bars)}
|
|
</svg>'''
|
|
path.write_text(svg, encoding="utf-8")
|
|
|
|
|
|
def main() -> None:
|
|
summary = json.loads(SUMMARY.read_text(encoding="utf-8"))
|
|
fixed = profile_summary(summary["scaling_fixed_total"])
|
|
growth = profile_summary(summary["growth_four_replicas"])
|
|
details = []
|
|
for item in summary["scaling_fixed_total"] + summary["growth_four_replicas"]:
|
|
row = dict(item)
|
|
row["metrics"] = metrics_summary(item)
|
|
details.append(row)
|
|
|
|
report = {
|
|
"fixed_total": fixed,
|
|
"growth": growth,
|
|
"runs": details,
|
|
"note": (
|
|
"Closed-loop client on the same Docker host. RPS is HTTP 201 throughput, "
|
|
"not a COMMIT counter; p95 covers successful requests only. Two repetitions "
|
|
"per profile; medians are reported."
|
|
),
|
|
}
|
|
OUT_JSON.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
|
|
lines = [
|
|
"Track 1. Cost of scaling (closed-loop POST /messages)",
|
|
"Fixed total API budget: 1x2.0/1G, 2x1.0/512M, 4x0.5/256M",
|
|
"Profile reps RPS median (min..max) p95 ms median errors",
|
|
]
|
|
for item in fixed:
|
|
lines.append(
|
|
f"{item['profile']:<18} {item['repetitions']:>4} "
|
|
f"{item['rps_median']:>6.1f} ({item['rps_min']:.1f}..{item['rps_max']:.1f}) "
|
|
f"{item['p95_ms_median']:>8.1f} {item['errors_total']}"
|
|
)
|
|
lines += [
|
|
"",
|
|
"Growth series: 4 replicas, per-replica CPU/RAM",
|
|
"Profile reps RPS median (min..max) p95 ms median errors",
|
|
]
|
|
for item in growth:
|
|
lines.append(
|
|
f"{item['profile']:<18} {item['repetitions']:>4} "
|
|
f"{item['rps_median']:>6.1f} ({item['rps_min']:.1f}..{item['rps_max']:.1f}) "
|
|
f"{item['p95_ms_median']:>8.1f} {item['errors_total']}"
|
|
)
|
|
lines += ["", "Resource samples (max over sampled runs):"]
|
|
for item in details:
|
|
metrics = item["metrics"]
|
|
lines.append(
|
|
f"{item['profile']['name']:<18} api_cpu={metrics['api_cpu_percent_max']}% "
|
|
f"pg_cpu={metrics['postgres_cpu_percent_max']}% "
|
|
f"lb_cpu={metrics['lb_cpu_percent_max']}% "
|
|
f"pg_conn={metrics['postgres_connections_max']} "
|
|
f"pg_active={metrics['postgres_active_max']} "
|
|
f"pg_wait={metrics['postgres_waiting_max']}"
|
|
)
|
|
lines += [
|
|
"",
|
|
"Interpretation:",
|
|
"- At a fixed total 2 CPU budget, splitting the budget across replicas does not",
|
|
" increase throughput; the per-request CPU cost dominates and p95 grows.",
|
|
"- In the growth series, 4 replicas improve from ~90 to ~280 RPS as total API CPU",
|
|
" grows from 2 to 8 cores, then the curve flattens: PostgreSQL (1 CPU) and the",
|
|
" write/idempotency transaction become the shared bottleneck.",
|
|
"- This is a local comparative experiment, not a production capacity guarantee.",
|
|
]
|
|
OUT_TXT.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
print("\n".join(lines))
|
|
|
|
bar_svg(
|
|
"Fixed total API budget (2 CPU / 1G): 1 vs 2 vs 4 replicas",
|
|
[f"{item['replicas']} replica" for item in fixed],
|
|
[item["rps_median"] for item in fixed],
|
|
[item["rps_max"] - item["rps_median"] for item in fixed],
|
|
FIXED_SVG,
|
|
"successful RPS",
|
|
)
|
|
bar_svg(
|
|
"4 replicas: effect of per-replica resources",
|
|
[f"{item['cpu_per_replica']} CPU" for item in growth],
|
|
[item["rps_median"] for item in growth],
|
|
[item["rps_max"] - item["rps_median"] for item in growth],
|
|
GROWTH_SVG,
|
|
"successful RPS",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|