Files
mtusi2026lab6/scripts/plot_telemetry_experiment.py
T
2026-11-25 22:33:46 +03:00

178 lines
4.2 KiB
Python

import json
import statistics
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
ROOT = Path(__file__).resolve().parents[1]
EVIDENCE = ROOT / "evidence"
MODES = ["baseline", "full", "optimized", "balanced"]
LABELS = ["Baseline", "Full", "Head 10%", "Tail"]
COLORS = ["#94a3b8", "#5794f2", "#eab308", "#16a34a"]
plt.rcParams.update({
"font.family": "DejaVu Sans",
"font.size": 11,
"figure.dpi": 150,
"axes.spines.top": False,
"axes.spines.right": False,
})
data = {
mode: [
json.loads(
(EVIDENCE / f"grade5-{mode}-{i}.json").read_text()
)
for i in range(1, 4)
]
for mode in MODES
}
for mode, rows in data.items():
assert len(rows) == 3
assert all(r["requested_requests"] == 1200 for r in rows)
assert all(r["successful_requests"] == 1200 for r in rows)
assert all(r["failed_requests"] == 0 for r in rows)
def save(fig, name):
fig.tight_layout()
path = EVIDENCE / name
fig.savefig(path, dpi=180, bbox_inches="tight")
plt.close(fig)
print(f"Saved: {path}")
def bars(ax, values, title, ylabel):
result = ax.bar(LABELS, values, color=COLORS)
ax.set_title(title)
ax.set_ylabel(ylabel)
ax.grid(axis="y", alpha=0.2)
ax.set_axisbelow(True)
ax.bar_label(result, fmt="%.2f", padding=3)
ax.set_ylim(0, max(values) * 1.2 if max(values) else 1)
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
for ax, metric in zip(axes, ["p50", "p95", "p99"]):
values = [
statistics.mean(
r["successful_latency_ms"][metric]
for r in data[mode]
)
for mode in MODES
]
bars(ax, values, metric, "Latency, ms")
fig.suptitle("HTTP latency: mean of three experiments")
save(fig, "grade5-latency.png")
fig, axes = plt.subplots(2, 2, figsize=(12, 9))
for ax, group, metric, title, unit in [
(
axes[0][0], "application_total", "cpu_mean_percent",
"Application CPU", "CPU, %"
),
(
axes[0][1], "observability_total", "cpu_mean_percent",
"Observability CPU", "CPU, %"
),
(
axes[1][0], "application_total", "ram_mean_mib",
"Application RAM", "RAM, MiB"
),
(
axes[1][1], "observability_total", "ram_mean_mib",
"Observability RAM", "RAM, MiB"
),
]:
values = [
statistics.mean(
r["resources"][group][metric]
for r in data[mode]
)
for mode in MODES
]
bars(ax, values, title, unit)
fig.suptitle("Resource consumption under identical workload")
save(fig, "grade5-resources.png")
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
for ax, metric, title in [
(
axes[0],
"otelcol_receiver_accepted_spans",
"Collector ingress",
),
(
axes[1],
"otelcol_exporter_sent_spans",
"Collector export",
),
]:
values = [
sum(r["collector_deltas"][metric] for r in data[mode])
for mode in MODES
]
result = ax.bar(LABELS, values, color=COLORS)
ax.bar_label(result, fmt="%.0f", padding=3)
ax.set_title(title)
ax.set_ylabel("Spans")
ax.set_ylim(0, max(values) * 1.2)
ax.grid(axis="y", alpha=0.2)
ax.set_axisbelow(True)
fig.suptitle("Trace volume: total across three experiments")
save(fig, "grade5-ingest.png")
fig, ax = plt.subplots(figsize=(10, 6))
scenarios = ["normal", "error", "slow"]
positions = list(range(len(scenarios)))
width = 0.24
for index, mode in enumerate(MODES[1:]):
report = json.loads(
(EVIDENCE / f"grade5-retention-{mode}.json").read_text()
)
values = [
report["summary"][scenario]["retention_percent"]
for scenario in scenarios
]
result = ax.bar(
[x + (index - 1) * width for x in positions],
values,
width,
label=LABELS[index + 1],
color=COLORS[index + 1],
)
ax.bar_label(result, fmt="%.0f%%", padding=3)
ax.set_xticks(positions, ["Normal", "Error", "Slow"])
ax.set_ylabel("Trace retention, %")
ax.set_ylim(0, 115)
ax.set_title("Retention of controlled HTTP traces")
ax.legend()
ax.grid(axis="y", alpha=0.2)
ax.set_axisbelow(True)
save(fig, "grade5-retention.png")