forked from mtusi2026/mtusi2026lab2
feat(research) grade 5 assignment done
This commit is contained in:
Executable
+46
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fit recovery time against message count and estimate the dump-loss window."""
|
||||
import csv
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
RPS = float(os.environ.get("RPS", "878")) # closed-loop POST RPS from REPORT.md
|
||||
rows = list(csv.DictReader((ROOT / "results/recovery.csv").open()))
|
||||
|
||||
|
||||
def fit(ys):
|
||||
# AGENT: three points are enough for the linear trend this lab asks for;
|
||||
# R^2 is printed so a bad fit is visible instead of silently accepted.
|
||||
xs = [int(row["messages"]) for row in rows]
|
||||
n = len(xs)
|
||||
mx = sum(xs) / n
|
||||
my = sum(ys) / n
|
||||
slope = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sum((x - mx) ** 2 for x in xs)
|
||||
intercept = my - slope * mx
|
||||
total = sum((y - my) ** 2 for y in ys)
|
||||
residual = sum((y - (intercept + slope * x)) ** 2 for x, y in zip(xs, ys))
|
||||
r2 = 1 - residual / total if total else 1.0
|
||||
return intercept, slope, r2
|
||||
|
||||
|
||||
dump = fit([float(row["dump_s"]) for row in rows])
|
||||
restore = fit([float(row["restore_s"]) for row in rows])
|
||||
total = fit([float(row["dump_s"]) + float(row["restore_s"]) for row in rows])
|
||||
|
||||
print("| messages | dump s | restore s | dump MiB | DB MiB | lost msgs at %.0f RPS |" % RPS)
|
||||
print("| ---: | ---: | ---: | ---: | ---: | ---: |")
|
||||
for row in rows:
|
||||
dump_s = float(row["dump_s"])
|
||||
print("| {messages} | {dump_s} | {restore_s} | {dump_mib:.1f} | {db_mib:.1f} | {loss:.0f} |".format(
|
||||
messages=row["messages"],
|
||||
dump_s=row["dump_s"],
|
||||
restore_s=row["restore_s"],
|
||||
dump_mib=int(row["dump_bytes"]) / 2**20,
|
||||
db_mib=int(row["db_bytes"]) / 2**20,
|
||||
loss=dump_s * RPS,
|
||||
))
|
||||
|
||||
print()
|
||||
for name, (a, b, r2) in (("dump", dump), ("restore", restore), ("dump+restore", total)):
|
||||
print(f"{name}: t = {a:.3f} + {b * 1_000_000:.3f} * (messages / 1M) s, R^2={r2:.3f}")
|
||||
Reference in New Issue
Block a user