Files
mtusi2026lab4/research/analyze.py
T

59 lines
2.1 KiB
Python

"""Render measured kernel memory peaks and dry-run traversal costs."""
import argparse
import json
from pathlib import Path
from statistics import median
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt # noqa: E402
ROOT = Path(__file__).resolve().parents[1]
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("input", nargs="?", type=Path, default=ROOT / "evidence/storage.json")
args = parser.parse_args()
d = json.loads(args.input.read_text())
plt.rcParams.update(
{
"font.size": 10,
"figure.dpi": 150,
"axes.grid": True,
"grid.alpha": 0.2,
"axes.spines.top": False,
"axes.spines.right": False,
}
)
fig, ax = plt.subplots(1, 2, figsize=(11, 4), constrained_layout=True)
labels = [f"{r['bytes_each'] / 1048576:.2f} MiB" for r in d["uploads"]]
base = [max(v["current_bytes"] for v in r["before"].values()) / 1048576 for r in d["uploads"]]
peak = [max(v["peak_bytes"] for v in r["after"].values()) / 1048576 for r in d["uploads"]]
x = list(range(len(labels)))
ax[0].bar([v - 0.18 for v in x], base, width=0.36, label="Before: max API memory.current")
ax[0].bar([v + 0.18 for v in x], peak, width=0.36, label="Kernel memory.peak incl. startup")
ax[0].axhline(512, color="gray", linestyle="--", label="Per API limit: 512 MiB")
ax[0].set(
xticks=x,
xticklabels=labels,
ylabel="MiB per API container",
title="10 simultaneous clients, 1 active upload/API",
)
ax[0].legend(fontsize=7)
cost = d["scan_cost"]
xs = [r["extra_fresh_objects"] for r in cost]
values = [[v["elapsed_seconds"] * 1000 for v in r["repeats"]] for r in cost]
ax[1].plot(xs, [median(v) for v in values], marker="o", label="Median of 3 dry runs")
ax[1].fill_between(
xs, [min(v) for v in values], [max(v) for v in values], alpha=0.15, label="Min/max"
)
ax[1].set(
xlabel="Extra fresh objects (metadata unchanged)",
ylabel="Traversal wall time, ms",
title="LIST pagination + metadata HEAD checks",
)
ax[1].legend(fontsize=8)
fig.savefig(args.input.with_name(args.input.stem + "-research.png"))
plt.close(fig)
print("PASS: research figure generated from storage.json")