forked from mtusi2026/mtusi2026lab4
lab4 s3 lvl4
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Peak-RAM experiment: N concurrent uploads, reads VmHWM (peak RSS) of every API replica process.
|
||||
|
||||
Docker mode (default): `docker compose exec -T api` is not enough to pick each replica, so we iterate
|
||||
`docker compose ps -q api` and `docker exec <id> cat /proc/1/status`. Local mode: --pids 123,456.
|
||||
Run on a fresh stack (restart api first) so VmHWM starts from the idle baseline.
|
||||
"""
|
||||
import argparse, json, re, subprocess, sys, time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
sys.path[:0] = [str(Path(__file__).resolve().parents[1] / "tests"), str(Path(__file__).parent)]
|
||||
from client import Client, check, run
|
||||
import imagegen
|
||||
|
||||
def reset_peak(args):
|
||||
"""Writing 5 to /proc/PID/clear_refs resets VmHWM to the current RSS (own process, no privileges)."""
|
||||
if args.pids:
|
||||
for pid in args.pids.split(","):
|
||||
Path(f"/proc/{pid}/clear_refs").write_text("5")
|
||||
else:
|
||||
for cid in subprocess.check_output(["docker", "compose", "ps", "-q", "api"], text=True).split():
|
||||
subprocess.check_call(["docker", "exec", cid, "sh", "-c", "echo 5 > /proc/1/clear_refs"])
|
||||
|
||||
def read_status(args):
|
||||
out = {}
|
||||
if args.pids:
|
||||
for pid in args.pids.split(","):
|
||||
out[pid] = Path(f"/proc/{pid}/status").read_text()
|
||||
else:
|
||||
for cid in subprocess.check_output(["docker", "compose", "ps", "-q", "api"], text=True).split():
|
||||
out[cid[:12]] = subprocess.check_output(["docker", "exec", cid, "cat", "/proc/1/status"], text=True)
|
||||
res = {}
|
||||
for k, t in out.items():
|
||||
g = lambda n: int(re.search(n + r":\s+(\d+) kB", t).group(1)) // 1024
|
||||
res[k] = {"VmRSS_MiB": g("VmRSS"), "VmHWM_MiB": g("VmHWM")}
|
||||
return res
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--base-url", default=None); p.add_argument("--ca-file", default=None)
|
||||
p.add_argument("--concurrency", type=int, default=10); p.add_argument("--pids", default="")
|
||||
p.add_argument("--profile", choices=["big-bytes", "max-pixels"], default="big-bytes")
|
||||
p.add_argument("--output", default=None)
|
||||
args = p.parse_args()
|
||||
check(args.concurrency >= 10, "the lab requires >= 10 concurrent uploads")
|
||||
c = Client(args.base_url, args.ca_file, timeout=120)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
if args.profile == "big-bytes": # ~9.6 MiB incompressible RGB PNG: stresses body streaming
|
||||
data = imagegen.png(1830, 1830, color_type=2, noise=True, level=0)
|
||||
else: # exactly 40 MP, tiny file: stresses the decoder
|
||||
data = imagegen.png(8000, 5000, bit_depth=8, color_type=0)
|
||||
check(len(data) <= 10 * 1024 * 1024, "payload above the limit")
|
||||
reset_peak(args)
|
||||
before = read_status(args)
|
||||
def one(_):
|
||||
t = time.monotonic(); r = c.call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=m.png", 201, actor=alice, raw=data, headers={"Content-Type": "image/png"}, schema="Attachment")
|
||||
return time.monotonic() - t
|
||||
t0 = time.monotonic()
|
||||
with ThreadPoolExecutor(args.concurrency) as pool:
|
||||
lat = sorted(pool.map(one, range(args.concurrency)))
|
||||
after = read_status(args)
|
||||
rep = {"profile": args.profile, "concurrency": args.concurrency, "payload_bytes": len(data), "wall_seconds": round(time.monotonic() - t0, 2),
|
||||
"latency_s": {"min": round(lat[0], 2), "max": round(lat[-1], 2)}, "before": before, "after_peak": after,
|
||||
"naive_estimate_MiB": round(args.concurrency * len(data) / 2**20 * 2)}
|
||||
print(json.dumps(rep, indent=2))
|
||||
if args.output: Path(args.output).write_text(json.dumps(rep, indent=2) + "\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user