forked from mtusi2026/mtusi2026lab5
186 lines
9.6 KiB
Python
186 lines
9.6 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Эксперимент: пик памяти API при N одновременных загрузках (lab4, уровень «4»).
|
||
|
||
python student-tests/upload_memory.py --base-url http://localhost:8080 \
|
||
--concurrency 10 --output evidence/upload-memory-10.json
|
||
|
||
Гипотеза: память реплики почти не зависит от размера и числа одновременных
|
||
загрузок, потому что тело не копится в RAM (≤ UPLOAD_SPOOL_MEMORY_BYTES в памяти,
|
||
остальное во временном файле), проверка изображения потоковая, в S3 уходит поток.
|
||
Наивная реализация (`await request.body()` + `Image.open().load()`) держала бы
|
||
≥ размер файла × 2…4 на каждую загрузку: 10 × 9,5 МБ → 200–400 МБ, что больше
|
||
лимита контейнера 256 МБ (OOM).
|
||
|
||
Методика: генерируется настоящий PNG из шума ≈9,5 МБ (почти не сжимается),
|
||
N потоков одновременно отправляют его через LB (несколько раундов), параллельно
|
||
каждые ~0,3 с читается память cgroup v2 всех контейнеров api (usage как в docker stats, anon, memory.peak). Фиксируются память
|
||
до нагрузки, пик во время и после, время ответа, коды ответов и sha256.
|
||
"""
|
||
import argparse
|
||
import json
|
||
import re
|
||
import subprocess
|
||
import threading
|
||
import time
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
|
||
from common import ROOT, Api, Report, compose, make_png, sha256, upload, wait_ready
|
||
|
||
# Печатаем «имя значение»: cgroup v2, иначе v1 (старые ядра/Docker Desktop с cgroup v1).
|
||
CGROUP = (
|
||
"if [ -f /sys/fs/cgroup/memory.current ]; then "
|
||
"echo current $(cat /sys/fs/cgroup/memory.current); "
|
||
"grep -E '^(anon|inactive_file) ' /sys/fs/cgroup/memory.stat; "
|
||
"[ -f /sys/fs/cgroup/memory.peak ] && echo peak $(cat /sys/fs/cgroup/memory.peak); "
|
||
"else M=/sys/fs/cgroup/memory; "
|
||
"echo current $(cat $M/memory.usage_in_bytes); "
|
||
"grep -E '^total_(rss|inactive_file) ' $M/memory.stat | sed -e 's/^total_rss/anon/' -e 's/^total_//'; "
|
||
"echo peak $(cat $M/memory.max_usage_in_bytes); fi")
|
||
UNITS = {"B": 1, "KiB": 1024, "MiB": 1024 ** 2, "GiB": 1024 ** 3, "kB": 1000, "MB": 1000 ** 2, "GB": 1000 ** 3}
|
||
|
||
|
||
def api_containers() -> list[str]:
|
||
return [c[:12] for c in compose("ps", "-q", "api").split()]
|
||
|
||
|
||
def _docker_stats(cid: str) -> dict:
|
||
"""Запасной путь: docker stats (usage без файлового кэша)."""
|
||
out = subprocess.run(["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", cid],
|
||
capture_output=True, text=True, cwd=ROOT).stdout
|
||
m = re.match(r"\s*([\d.]+)\s*([A-Za-z]+)", out)
|
||
usage = round(float(m.group(1)) * UNITS[m.group(2)] / 1024 ** 2, 1) if m else 0.0
|
||
return {"usage": usage, "anon": usage, "peak": None}
|
||
|
||
|
||
_warned: list = []
|
||
|
||
|
||
def read_cgroup(cid: str) -> dict:
|
||
"""Память контейнера (MiB): usage как в docker stats (current − inactive_file),
|
||
anon — память процессов без файлового кэша, peak — максимум за жизнь cgroup."""
|
||
p = subprocess.run(["docker", "exec", cid, "sh", "-c", CGROUP],
|
||
capture_output=True, text=True, cwd=ROOT)
|
||
vals = {}
|
||
for line in p.stdout.splitlines():
|
||
parts = line.split()
|
||
if len(parts) == 2 and parts[1].isdigit():
|
||
vals[parts[0]] = int(parts[1])
|
||
if "current" not in vals:
|
||
if not _warned:
|
||
_warned.append(1)
|
||
print(f" cgroup read failed (exit {p.returncode}: {p.stderr.strip()[:200]!r}); using docker stats")
|
||
return _docker_stats(cid)
|
||
mib = 1024 ** 2
|
||
return {"usage": round((vals["current"] - vals.get("inactive_file", 0)) / mib, 1),
|
||
"anon": round(vals.get("anon", vals["current"]) / mib, 1),
|
||
"peak": round(vals["peak"] / mib, 1) if "peak" in vals else None}
|
||
|
||
|
||
def sample_mem(ids: list[str]) -> dict:
|
||
with ThreadPoolExecutor(max_workers=len(ids) or 1) as pool:
|
||
return dict(zip(ids, pool.map(read_cgroup, ids)))
|
||
|
||
|
||
def main():
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("--base-url", default="http://localhost:8080")
|
||
ap.add_argument("--concurrency", type=int, default=10)
|
||
ap.add_argument("--rounds", type=int, default=3)
|
||
ap.add_argument("--width", type=int, default=1780, help="PNG width/height: 1780 → ≈9,5 МБ")
|
||
ap.add_argument("--output", default=None)
|
||
args = ap.parse_args()
|
||
api, r = Api(args.base_url), Report()
|
||
|
||
data = make_png(args.width, args.width, noise=True)
|
||
digest = sha256(data)
|
||
print(f"file: {len(data)} bytes ({len(data) / 1024 ** 2:.2f} MiB), sha256 {digest[:12]}…")
|
||
u = api.new_user("memexp")
|
||
st, chat, _ = api.call("POST", "/api/v1/chats", {"title": "memory", "member_ids": [u["id"]]}, token=u["token"])
|
||
|
||
ids = api_containers()
|
||
idle = sample_mem(ids)
|
||
print("idle MiB:", idle)
|
||
peak = {k: {"usage": v["usage"], "anon": v["anon"]} for k, v in idle.items()}
|
||
samples = []
|
||
stop = threading.Event()
|
||
|
||
def sampler():
|
||
t0 = time.monotonic()
|
||
while not stop.is_set():
|
||
m = sample_mem(ids)
|
||
samples.append({"t": round(time.monotonic() - t0, 2), **m})
|
||
for k, v in m.items():
|
||
for f in ("usage", "anon"):
|
||
peak[k][f] = max(peak[k][f], v[f])
|
||
time.sleep(0.2)
|
||
|
||
th = threading.Thread(target=sampler, daemon=True)
|
||
th.start()
|
||
results = []
|
||
|
||
def one(i):
|
||
t = time.monotonic()
|
||
try:
|
||
st, body, rh = upload(api, u["token"], chat["id"], data, f"noise-{i}.png", timeout=120)
|
||
except OSError as exc:
|
||
return {"status": None, "error": type(exc).__name__, "seconds": round(time.monotonic() - t, 2)}
|
||
return {"status": st, "seconds": round(time.monotonic() - t, 2),
|
||
"sha_ok": bool(body) and body.get("sha256") == digest,
|
||
"instance": rh.get("X-Instance-Id"), "id": (body or {}).get("id")}
|
||
|
||
t_start = time.monotonic()
|
||
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
||
for rnd in range(args.rounds):
|
||
results += list(pool.map(one, range(rnd * args.concurrency, (rnd + 1) * args.concurrency)))
|
||
elapsed = time.monotonic() - t_start
|
||
time.sleep(2)
|
||
stop.set()
|
||
th.join()
|
||
after = sample_mem(ids)
|
||
|
||
ok = [x for x in results if x["status"] == 202]
|
||
lat = sorted(x["seconds"] for x in ok)
|
||
summary = {
|
||
"file_bytes": len(data), "concurrency": args.concurrency, "rounds": args.rounds,
|
||
"uploads": len(results), "status_counts": {str(s): sum(1 for x in results if x["status"] == s)
|
||
for s in sorted({x["status"] for x in results}, key=str)},
|
||
"sha256_ok": sum(1 for x in ok if x["sha_ok"]),
|
||
"instances": sorted({x["instance"] for x in ok if x["instance"]}),
|
||
"elapsed_s": round(elapsed, 2),
|
||
"throughput_MiB_s": round(len(ok) * len(data) / 1024 ** 2 / elapsed, 1) if elapsed else None,
|
||
"latency_s": {"p50": lat[len(lat) // 2] if lat else None, "max": lat[-1] if lat else None},
|
||
"memory_MiB": {"idle": idle, "sampled_peak": peak, "after": after,
|
||
"max_growth_usage": round(max(peak[k]["usage"] - idle[k]["usage"] for k in peak), 1),
|
||
"max_growth_anon": round(max(peak[k]["anon"] - idle[k]["anon"] for k in peak), 1),
|
||
"cgroup_peak_after": {k: v["peak"] for k, v in after.items()}},
|
||
"container_limit_MiB": 256, "samples": len(samples),
|
||
}
|
||
print(json.dumps({k: v for k, v in summary.items() if k != "samples"}, ensure_ascii=False, indent=2))
|
||
|
||
# Проверка, что сохранены настоящие байты (выборочно). С lab5 байты отдаются после ready:
|
||
# 202 означает «принято», обработку делает worker (под нагрузкой — с очередью).
|
||
for x in ok[:3]:
|
||
wait_ready(api, u["token"], x["id"], seconds=300)
|
||
st, raw, _ = api.raw("GET", f"/api/v1/attachments/{x['id']}/content",
|
||
headers={"Authorization": "Bearer " + u["token"]}, timeout=60)
|
||
r.check(st == 200 and sha256(raw) == digest, f"download {x['id'][:8]} sha256 matches")
|
||
r.check(len(ok) == len(results), f"all {len(results)} uploads -> 202")
|
||
r.check(summary["sha256_ok"] == len(ok), "sha256 of every upload matches")
|
||
# Критерий — память ПРОЦЕССОВ (anon): именно туда попало бы накопление тела в RAM.
|
||
# usage дополнительно включает файловый кэш ядра для временных spool-файлов (вытесняемый),
|
||
# он печатается выше для полноты, но буферизацию не показывает.
|
||
growth = summary["memory_MiB"]["max_growth_anon"]
|
||
r.check(growth < 100,
|
||
f"peak process memory (anon) growth per replica {growth} MiB < 100 MiB "
|
||
f"(naive buffering: ≥ {args.concurrency // 2} × {len(data) / 1024 ** 2:.1f} MiB × 2)")
|
||
if args.output:
|
||
with open(args.output, "w", encoding="utf-8") as f:
|
||
json.dump({**summary, "raw_samples": samples}, f, ensure_ascii=False, indent=2)
|
||
print("saved", args.output)
|
||
r.finish()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|