#!/usr/bin/env python3 import json, time, math, sys, threading, os from concurrent.futures import ThreadPoolExecutor from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) from client import Client, Failure def pct(xs, p): if not xs: return None xs = sorted(xs) return round(1000 * xs[max(0, math.ceil(p * len(xs)) - 1)], 3) def main(): warmup = int(sys.argv[1]) conc = int(sys.argv[2]) duration = float(sys.argv[3]) mode = sys.argv[4] if len(sys.argv) > 4 else "post" base = os.environ.get("BASE_URL", "http://127.0.0.1:8080") c = Client(base) alice, bob, eve, chat = c.bootstrap() for i in range(warmup): c.message(alice, chat, "w-%d" % i) barrier = threading.Barrier(conc) def worker(wid): local = Client(base, timeout=5) barrier.wait() deadline = time.monotonic() + duration lat, err, n = [], 0, 0 while time.monotonic() < deadline: t0 = time.monotonic() try: if mode == "get": local.call( "GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=50", actor=alice, schema="MessagePage", ) else: local.message(alice, chat, "b-%s-%s" % (wid, n)) lat.append(time.monotonic() - t0) except Failure: err += 1 n += 1 return lat, err t0 = time.monotonic() with ThreadPoolExecutor(max_workers=conc) as pool: parts = list(pool.map(worker, range(conc))) elapsed = time.monotonic() - t0 lat = [x for a, _ in parts for x in a] err = sum(e for _, e in parts) out = { "warmup_messages": warmup, "concurrency": conc, "duration_s": duration, "elapsed_s": round(elapsed, 3), "ok": len(lat), "err": err, "rps": round(len(lat) / elapsed, 3) if elapsed else None, "p50_ms": pct(lat, 0.50), "p95_ms": pct(lat, 0.95), "p99_ms": pct(lat, 0.99), "mode": mode, } import subprocess def rss_from_proc(): for ent in os.listdir("/proc"): if not ent.isdigit(): continue try: with open("/proc/%s/cmdline" % ent, "rb") as f: cmd = f.read() except OSError: continue if b"messenger" not in cmd: continue try: with open("/proc/%s/status" % ent) as f: for line in f: if line.startswith("VmRSS:"): kb = int(line.split()[1]) return "%.2fMiB" % (kb / 1024.0) except (OSError, ValueError, IndexError): continue return None mem = rss_from_proc() if not mem: name = os.environ.get("DOCKER_NAME", "mtusi-lab1") for cmd in ( ["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", name], ["sudo", "-n", "docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", name], ): try: mem = subprocess.check_output(cmd, text=True).split("/")[0].strip() if mem: break except Exception: mem = None out["mem"] = mem print(json.dumps(out, ensure_ascii=False)) if __name__ == "__main__": main()