forked from mtusi2026/mtusi2026lab4
ЛР4
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
"""Профиль нагрузки для отчёта: публичный tests/load.py + CPU/RAM контейнеров во время нагрузки.
|
||||
|
||||
python student-tests/load_profile.py (30 с, 4 потока — как в tests/README.md)
|
||||
|
||||
Запускает tests/load.py (closed loop: каждый поток шлёт следующий POST сразу после ответа)
|
||||
и параллельно раз в ~2 с снимает `docker stats --no-stream` по контейнерам стенда.
|
||||
Сохраняет evidence/load.json (вывод load.py: успешные RPS, ошибки, p50/p95/p99)
|
||||
и evidence/load-stats.json (максимум CPU % и RAM по каждому сервису за время нагрузки).
|
||||
Числа зависят от машины и Docker Desktop; это замер этого стенда, а не предел пропускной способности.
|
||||
"""
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
from helpers import ROOT, check, run
|
||||
from stand import BASE_URL, compose_output, inspect
|
||||
|
||||
DURATION, CONCURRENCY = 30, 4
|
||||
LOAD_JSON = ROOT / "evidence" / "load.json"
|
||||
STATS_JSON = ROOT / "evidence" / "load-stats.json"
|
||||
|
||||
|
||||
def mib(text):
|
||||
"""'123.4MiB' / '1.2GiB' / '512kB' -> MiB."""
|
||||
text = text.strip()
|
||||
units = {"KiB": 1 / 1024, "kB": 1 / 1024, "MiB": 1, "MB": 1, "GiB": 1024, "GB": 1024, "B": 1 / 2**20}
|
||||
for unit in sorted(units, key=len, reverse=True):
|
||||
if text.endswith(unit):
|
||||
return float(text[: -len(unit)]) * units[unit]
|
||||
return 0.0
|
||||
|
||||
|
||||
def main():
|
||||
# Только основной путь запроса: балансировщик, реплики API, PostgreSQL, Valkey.
|
||||
names = {c[:12]: inspect(c, '{{index .Config.Labels "com.docker.compose.service"}}')
|
||||
for c in compose_output("ps", "-q").split()}
|
||||
names = {cid: service for cid, service in names.items() if service in ("lb", "api", "postgres", "valkey")}
|
||||
containers = list(names)
|
||||
peaks, stopping = {}, threading.Event()
|
||||
|
||||
def sample():
|
||||
while not stopping.is_set():
|
||||
result = subprocess.run(["docker", "stats", "--no-stream", "--format", "{{json .}}", *containers],
|
||||
capture_output=True, text=True)
|
||||
for line in result.stdout.splitlines():
|
||||
item = json.loads(line)
|
||||
key = item["ID"][:12]
|
||||
cpu = float(item["CPUPerc"].rstrip("%") or 0)
|
||||
used, _, limit = item["MemUsage"].partition("/")
|
||||
peak = peaks.setdefault(key, {"service": names.get(key, "?"), "cpu_percent_max": 0.0,
|
||||
"mem_mib_max": 0.0, "mem_limit_mib": round(mib(limit))})
|
||||
peak["cpu_percent_max"] = max(peak["cpu_percent_max"], cpu)
|
||||
peak["mem_mib_max"] = round(max(peak["mem_mib_max"], mib(used)), 1)
|
||||
time.sleep(1)
|
||||
|
||||
sampler = threading.Thread(target=sample, daemon=True)
|
||||
sampler.start()
|
||||
result = subprocess.run([sys.executable, "tests/load.py", "--base-url", BASE_URL, "--duration", str(DURATION),
|
||||
"--concurrency", str(CONCURRENCY), "--output", str(LOAD_JSON)],
|
||||
cwd=ROOT, capture_output=True)
|
||||
stopping.set()
|
||||
sampler.join(timeout=10)
|
||||
print(result.stdout.decode("utf-8", "replace").strip())
|
||||
check(result.returncode == 0, "tests/load.py failed: " + result.stderr.decode("utf-8", "replace")[-300:])
|
||||
rows = sorted(peaks.values(), key=lambda row: row["service"])
|
||||
STATS_JSON.write_text(json.dumps({"duration_seconds": DURATION, "concurrency": CONCURRENCY,
|
||||
"containers": rows}, indent=2) + "\n", encoding="utf-8")
|
||||
print("\nservice cpu % max RAM max / limit, MiB")
|
||||
for row in rows:
|
||||
print(f"{row['service']:<14} {row['cpu_percent_max']:>9.1f} {row['mem_mib_max']:>7.1f} / {row['mem_limit_mib']}")
|
||||
print(f"PASS: load profile saved to {LOAD_JSON.relative_to(ROOT).as_posix()} and "
|
||||
f"{STATS_JSON.relative_to(ROOT).as_posix()}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user