forked from mtusi2026/mtusi2026lab4
Исходники, Dockerfile, compose.yaml, скрипты и собственные тесты скопированы из mtusi2026lab3 (ветка solution/bvt2302-bodanyuk, b6847e2) как есть; изменения lab4 — отдельными коммитами. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
76 lines
3.3 KiB
Python
76 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Level 4: scripts/rolling-update.sh while bench/loadgen.py traffic runs through the LB.
|
|
|
|
python bench/rolling_under_load.py --output evidence/rolling-update
|
|
|
|
Writes <output>/summary.json (errors, RPS, history check, revisions before/after),
|
|
<output>/timeline.csv (per second ok/failed/instances) and <output>/script-output.txt.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import csv
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
sys.path.insert(0, str(ROOT / "bench"))
|
|
import lbctl # noqa: E402
|
|
from loadgen import Load # noqa: E402
|
|
|
|
|
|
def revisions() -> dict[str, str]:
|
|
result = {}
|
|
for r in lbctl.replicas():
|
|
if r["state"] == "running":
|
|
label = subprocess.run(["docker", "inspect", "-f", '{{index .Config.Labels "org.opencontainers.image.revision"}}',
|
|
r["id"]], capture_output=True, text=True).stdout.strip()
|
|
result[r["name"]] = f"{r['hostname']} revision={label}"
|
|
return result
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
|
|
parser.add_argument("--output", default="evidence/rolling-update")
|
|
parser.add_argument("--workers", type=int, default=4)
|
|
args = parser.parse_args()
|
|
out = ROOT / args.output
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
before = revisions()
|
|
load = Load(args.workers).start()
|
|
time.sleep(5)
|
|
t_begin = load.now()
|
|
bash = (os.path.join(os.environ.get("ProgramFiles", r"C:\Program Files"), "Git", "bin", "bash.exe")
|
|
if os.name == "nt" else "sh")
|
|
env = {**os.environ, "PYTHON": sys.executable}
|
|
script = subprocess.run([bash, str(ROOT / "scripts" / "rolling-update.sh")], cwd=ROOT, env=env,
|
|
capture_output=True, text=True, encoding="utf-8", timeout=900)
|
|
t_end = load.now()
|
|
time.sleep(5)
|
|
load.stop()
|
|
(out / "script-output.txt").write_text(f"$ sh scripts/rolling-update.sh (exit {script.returncode})\n"
|
|
+ script.stdout + script.stderr, encoding="utf-8")
|
|
with (out / "timeline.csv").open("w", newline="", encoding="utf-8") as f:
|
|
writer = csv.DictWriter(f, fieldnames=["second", "ok", "failed", "instances"])
|
|
writer.writeheader()
|
|
writer.writerows(load.per_second())
|
|
summary = {"script_exit_code": script.returncode, "update_seconds": round(t_end - t_begin, 1),
|
|
"before": before, "after": revisions(),
|
|
"during_update": load.window(t_begin, t_end), "whole_run": load.window(0, load.now()),
|
|
"client_retries_same_key": load.retries, "history": load.verify_history(),
|
|
"workload": f"{args.workers} closed-loop workers: POST (Idempotency-Key) + GET page via 127.0.0.1:8080"}
|
|
(out / "summary.json").write_text(json.dumps(summary, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
print(json.dumps({k: summary[k] for k in ("script_exit_code", "update_seconds", "before", "after", "history")},
|
|
ensure_ascii=False, indent=2))
|
|
print(json.dumps(summary["during_update"], ensure_ascii=False))
|
|
return 0 if script.returncode == 0 else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|