forked from mtusi2026/mtusi2026lab3
120 lines
5.9 KiB
Python
120 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Summarize measured k6 counters and plot reproducible research figures."""
|
|
import csv
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import matplotlib
|
|
matplotlib.use("Agg")
|
|
import matplotlib.pyplot as plt
|
|
|
|
roots = [Path(arg) for arg in sys.argv[1:]]
|
|
def resource_peaks(root, run):
|
|
"""resource_peaks aggregates five-second samples, including setup and warmup."""
|
|
path = root / ("resources-" + run + ".txt")
|
|
lines = path.read_text().splitlines()
|
|
pod_snapshot = root / ("pods-" + run + ".txt")
|
|
if pod_snapshot.exists():
|
|
api_pods = {line.split()[0] for line in pod_snapshot.read_text().splitlines() if line.startswith("api-") and "Running" in line}
|
|
else:
|
|
api_pods = set()
|
|
block = set()
|
|
for line in lines + ["UTC END"]:
|
|
if line.startswith("UTC "):
|
|
if block:
|
|
api_pods = block
|
|
block = set()
|
|
elif line.startswith("api-"):
|
|
block.add(line.split()[0])
|
|
peaks = dict(api_cpu_m=0, postgres_cpu_m=0, valkey_cpu_m=0, load_cpu_m=0, api_memory_mib=0, postgres_memory_mib=0, pg_active=0, pg_idle_transaction=0, pg_lock_waiters=0)
|
|
sample = dict.fromkeys(peaks, 0)
|
|
for line in lines + ["UTC END"]:
|
|
if line.startswith("UTC "):
|
|
for key in peaks:
|
|
peaks[key] = max(peaks[key], sample[key])
|
|
sample = dict.fromkeys(peaks, 0)
|
|
continue
|
|
fields = line.split()
|
|
if len(fields) == 3 and fields[0].startswith(("api-", "postgres-", "valkey-", "load-")):
|
|
role = fields[0].split("-")[0]
|
|
if role == "api" and fields[0] not in api_pods:
|
|
continue
|
|
if role == "load" and not fields[0].startswith("load-" + run + "-"):
|
|
continue
|
|
cpu = float(fields[1][:-1]) if fields[1].endswith("m") else float(fields[1]) * 1000
|
|
memory = float(fields[2][:-2]) * (1024 if fields[2].endswith("Gi") else 1)
|
|
sample[role + "_cpu_m"] += cpu
|
|
if role in ("api", "postgres"):
|
|
sample[role + "_memory_mib"] += memory
|
|
if line.startswith("active|"):
|
|
sample["pg_active"] += int(line.split("|")[-1])
|
|
if line.startswith("idle in transaction|"):
|
|
sample["pg_idle_transaction"] += int(line.split("|")[-1])
|
|
if "|Lock|" in line:
|
|
sample["pg_lock_waiters"] += int(line.split("|")[-1])
|
|
return peaks
|
|
|
|
rows = []
|
|
|
|
excluded = []
|
|
for root in roots:
|
|
for profile in csv.DictReader((root / "profiles.csv").open()):
|
|
deployment = root / ("deployment-" + profile["run"] + ".json")
|
|
if deployment.exists() and json.loads(deployment.read_text())["spec"]["replicas"] != int(profile["replicas"]):
|
|
excluded.append(str(deployment) + " (replica mismatch)")
|
|
continue
|
|
source = root / ("load-" + profile["run"] + ".json")
|
|
if not source.exists():
|
|
excluded.append(str(source))
|
|
continue
|
|
result = json.loads(source.read_text())
|
|
metrics = result["summary"]["metrics"]
|
|
if "message_latency_ms" not in metrics:
|
|
excluded.append(str(source))
|
|
continue
|
|
def value(name, key="count"):
|
|
"""value reads a measured metric, using zero for an absent error counter."""
|
|
return metrics.get(name, {}).get("values", {}).get(key, 0)
|
|
latency = metrics["message_latency_ms"]["values"]
|
|
seconds = int(profile["duration"])
|
|
operations = value("measured_operations")
|
|
rows.append(dict(profile, source=str(root), confirmed=value("confirmed_messages"),
|
|
rps=value("confirmed_messages") / seconds,
|
|
errors=value("application_errors"),
|
|
error_percent=100 * value("application_errors") / max(operations, 1),
|
|
p50_ms=latency["p(50)"], p95_ms=latency["p(95)"],
|
|
p99_ms=latency["p(99)"],
|
|
rejected_429=value("application_errors{status:429}"),
|
|
unavailable_503=value("application_errors{status:503}"),
|
|
network_errors=value("application_errors{status:0}"), **resource_peaks(root, profile["run"])))
|
|
output = roots[0]
|
|
with (output / "summary.csv").open("w") as stream:
|
|
writer = csv.DictWriter(stream, fieldnames=list(rows[0]))
|
|
writer.writeheader()
|
|
writer.writerows(rows)
|
|
(output / "excluded.json").write_text(json.dumps(excluded, indent=2)+"\n")
|
|
fig, axes = plt.subplots(3, 1, figsize=(13, 11), constrained_layout=True)
|
|
names = [row["profile"] for row in rows]
|
|
for axis, field, label in zip(axes, ["rps", "p95_ms", "error_percent"],
|
|
["Confirmed messages / second", "Write attempt p95, ms", "Failed operations, %"]):
|
|
axis.bar(names, [row[field] for row in rows], color="#267c75")
|
|
axis.set_ylabel(label)
|
|
axis.grid(axis="y", alpha=0.25)
|
|
axis.tick_params(axis="x", rotation=35)
|
|
fig.suptitle("Lab 3: measured replica budgets and pool-size experiment")
|
|
fig.savefig(output / "results.png", dpi=160)
|
|
fig.savefig(output / "results.svg")
|
|
plt.close(fig)
|
|
by_name = {row["profile"]: row for row in rows}
|
|
if all(f"{series}{n}" in by_name for series in ["fixed", "growing"] for n in [1, 2, 4]):
|
|
fig, axes = plt.subplots(1, 2, figsize=(11, 4), constrained_layout=True)
|
|
for series, label in [("fixed", "Fixed total CPU/RAM"), ("growing", "Growing total CPU/RAM")]:
|
|
for axis, field, ylabel in zip(axes, ["rps", "error_percent"], ["Confirmed messages / second", "Failed operations, %"]):
|
|
axis.plot([1, 2, 4], [by_name[f"{series}{n}"][field] for n in [1, 2, 4]], marker="o", label=label)
|
|
axis.set(xlabel="API replicas", ylabel=ylabel, xticks=[1, 2, 4])
|
|
axis.grid(alpha=0.25)
|
|
axis.legend()
|
|
fig.savefig(output / "matrix.png", dpi=160)
|
|
fig.savefig(output / "matrix.svg")
|
|
print(json.dumps(rows, indent=2))
|