File size: 3,520 Bytes
ab59633 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | """Load test: N parallel WebSocket sessions for a while, mixing solved episodes, heavy Python and quick exits.
Usage: .venv/bin/python scripts/load_test.py --url http://localhost:8011 --workers 8 --seconds 180
"""
import argparse
import random
import statistics
import sys
import threading
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
sys.path.insert(0, str(Path(__file__).resolve().parent))
from diligence.environment import TASK_IDS, build # noqa: E402
from wsclient import Session # noqa: E402
HEAVY = "x = bytearray(450 * 1024 * 1024)\nimport hashlib\nprint(hashlib.sha256(x).hexdigest()[:8])"
SPIN = "import time\nt = time.time()\nwhile time.time() - t < 4: pass\nprint('spun')"
def worker(url, until, rng, stats, lock):
while time.time() < until:
task_id = rng.choice(TASK_IDS)
seed = rng.randrange(10 ** 9)
mode = rng.choices(["oracle", "heavy", "quick"], [0.5, 0.3, 0.2])[0]
try:
with Session(url, timeout=120) as s:
t0 = time.time()
data = s.reset(task_id=task_id, seed=seed)
lat = [time.time() - t0]
tools = data["observation"]["output"]
if mode == "oracle":
actions = build(task_id, seed).oracle()
elif mode == "heavy" and '- python {"code"' in tools:
actions = [{"tool": "python", "args": {"code": rng.choice([HEAVY, SPIN])}}, {"tool": "submit", "args": {}}]
else:
actions = [{"tool": "list_files" if "list_files" in tools else "nope", "args": {}},
{"tool": "submit", "args": {}}]
for action in actions:
t0 = time.time()
data = s.step(action)
lat.append(time.time() - t0)
if data["done"]:
break
ok = data["done"] and (mode != "oracle" or data["reward"] == 1.0)
with lock:
stats["episodes"] += 1
stats["latency"] += lat
if not ok:
stats["bad"].append((task_id, seed, mode, data.get("reward")))
except Exception as exc: # noqa: BLE001
with lock:
stats["errors"].append(f"{task_id} {mode}: {type(exc).__name__}: {exc}"[:200])
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--url", default="http://localhost:8011")
ap.add_argument("--workers", type=int, default=8)
ap.add_argument("--seconds", type=int, default=180)
args = ap.parse_args()
stats = {"episodes": 0, "latency": [], "bad": [], "errors": []}
lock = threading.Lock()
until = time.time() + args.seconds
threads = [threading.Thread(target=worker, args=(args.url, until, random.Random(i), stats, lock))
for i in range(args.workers)]
for t in threads:
t.start()
for t in threads:
t.join()
lat = sorted(stats["latency"]) or [0.0]
print(f"episodes {stats['episodes']}, calls {len(lat)}, errors {len(stats['errors'])}, "
f"wrong results {len(stats['bad'])}")
print(f"latency p50 {statistics.median(lat):.2f}s p95 {lat[int(0.95 * len(lat))]:.2f}s max {lat[-1]:.2f}s")
for e in stats["errors"][:10] + [str(b) for b in stats["bad"][:10]]:
print(" ", e)
sys.exit(1 if stats["errors"] or stats["bad"] else 0)
if __name__ == "__main__":
main()
|