forked from mtusi2026/mtusi2026lab4
108 lines
4.8 KiB
Python
108 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
"""N одновременных медленных загрузок ~10 МиБ через LB; пик памяти api-контейнеров по cgroup.
|
|
python3 student_tests/upload_memory.py --concurrency 10 --output evidence/upload-memory-10.json"""
|
|
import argparse
|
|
import http.client
|
|
import json
|
|
import os
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
import time
|
|
import zlib
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
from test_attachments_level4 import SIG, URL, chunk, compose, new_chat, new_user # noqa: E402
|
|
|
|
CG2 = "cat /sys/fs/cgroup/memory.current; grep -E '^(anon|file) ' /sys/fs/cgroup/memory.stat"
|
|
CG1 = "cat /sys/fs/cgroup/memory/memory.usage_in_bytes; grep -E '^(rss|cache) ' /sys/fs/cgroup/memory/memory.stat"
|
|
MIB = 1024 * 1024
|
|
|
|
|
|
def build_png(target, idat_chunk):
|
|
w = 1000
|
|
h = target // (w * 3 + 1)
|
|
comp = zlib.compress(b"".join(b"\x00" + os.urandom(w * 3) for _ in range(h)), 0)
|
|
step = idat_chunk or len(comp)
|
|
idat = b"".join(chunk(b"IDAT", comp[i:i + step]) for i in range(0, len(comp), step))
|
|
return SIG + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0)) + idat + chunk(b"IEND", b"")
|
|
|
|
|
|
def read_mem(cid):
|
|
for cmd in (CG2, CG1):
|
|
r = subprocess.run(["docker", "exec", cid, "sh", "-c", cmd], capture_output=True, text=True)
|
|
if r.returncode == 0 and r.stdout.strip():
|
|
lines = r.stdout.strip().split("\n")
|
|
d = {p[0]: int(p[1]) for p in (l.split() for l in lines[1:]) if len(p) == 2}
|
|
return int(lines[0]), d.get("anon", d.get("rss", 0)), d.get("file", d.get("cache", 0))
|
|
raise SystemExit("не удалось прочитать cgroup-память контейнера")
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--concurrency", type=int, default=10)
|
|
ap.add_argument("--mode", choices=["normal", "single-idat"], default="normal",
|
|
help="single-idat: весь PNG одним IDAT-чанком (худший случай для проверки структуры)")
|
|
ap.add_argument("--chunk-delay-ms", type=float, default=20, help="пауза между 64 КиБ: медленный клиент")
|
|
ap.add_argument("--output", required=True)
|
|
args = ap.parse_args()
|
|
|
|
body = build_png(10 * MIB - 200_000, 0 if args.mode == "single-idat" else 32768)
|
|
assert len(body) <= 10 * MIB
|
|
user, auth = new_user()
|
|
chat = new_chat(auth, [user["id"]])
|
|
cids = compose("ps", "-q", "api").split()
|
|
limits = {c: int(subprocess.run(["docker", "inspect", "-f", "{{.HostConfig.Memory}}", c],
|
|
capture_output=True, text=True).stdout.strip() or 0) for c in cids}
|
|
base = {c: read_mem(c) for c in cids}
|
|
peak = {c: list(base[c]) for c in cids}
|
|
stop = threading.Event()
|
|
|
|
def monitor(cid):
|
|
while not stop.is_set():
|
|
cur = read_mem(cid)
|
|
peak[cid] = [max(a, b) for a, b in zip(peak[cid], cur)]
|
|
|
|
mons = [threading.Thread(target=monitor, args=(c,)) for c in cids]
|
|
statuses = [None] * args.concurrency
|
|
barrier = threading.Barrier(args.concurrency)
|
|
|
|
def one(i):
|
|
barrier.wait()
|
|
c = http.client.HTTPConnection(URL.hostname, URL.port, timeout=180)
|
|
c.putrequest("POST", f"/api/v1/chats/{chat}/attachments?filename=m{i}.png")
|
|
for k, v in {**auth, "Content-Type": "image/png", "Content-Length": str(len(body))}.items():
|
|
c.putheader(k, v)
|
|
c.endheaders()
|
|
for off in range(0, len(body), 65536):
|
|
c.send(body[off:off + 65536])
|
|
time.sleep(args.chunk_delay_ms / 1000)
|
|
r = c.getresponse()
|
|
r.read()
|
|
statuses[i] = r.status
|
|
|
|
[m.start() for m in mons]
|
|
ts = [threading.Thread(target=one, args=(i,)) for i in range(args.concurrency)]
|
|
t0 = time.time()
|
|
[t.start() for t in ts]
|
|
[t.join() for t in ts]
|
|
wall = time.time() - t0
|
|
stop.set()
|
|
[m.join() for m in mons]
|
|
|
|
result = {"mode": args.mode, "concurrency": args.concurrency, "file_mib": round(len(body) / MIB, 2),
|
|
"payload_total_mib": round(args.concurrency * len(body) / MIB, 1), "wall_s": round(wall, 1),
|
|
"statuses": sorted(set(s for s in statuses)), "ok": statuses.count(201), "containers": {}}
|
|
for c in cids:
|
|
result["containers"][c[:12]] = {
|
|
"limit_mib": round(limits[c] / MIB), "baseline_anon_mib": round(base[c][1] / MIB, 1),
|
|
"peak_anon_mib": round(peak[c][1] / MIB, 1), "anon_growth_mib": round((peak[c][1] - base[c][1]) / MIB, 1),
|
|
"peak_current_mib_incl_page_cache": round(peak[c][0] / MIB, 1)}
|
|
Path(args.output).write_text(json.dumps(result, indent=2, ensure_ascii=False))
|
|
print(json.dumps(result, indent=2, ensure_ascii=False))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |