Files
mtusi2026lab3/research/load.py
T

133 lines
4.5 KiB
Python

"""Open-loop HTTP message writes; count attempts, successes, errors and drops separately."""
import asyncio
import json
import math
import time
import uuid
from collections import Counter
import aiohttp
async def run_load(base, actor, chat_id, *, rate, duration, concurrency=128):
latencies, errors, instances = [], Counter(), Counter()
pending = set()
dropped = 0
dropped_lag = dropped_concurrency = 0
lag = []
total_slots = int(rate * duration)
async with aiohttp.ClientSession(
base_url=base,
connector=aiohttp.TCPConnector(limit=concurrency),
timeout=aiohttp.ClientTimeout(total=6),
trust_env=False,
) as client:
started = time.perf_counter()
cpu_started = time.process_time()
async def request(index):
start = time.perf_counter()
try:
async with client.post(
f"/api/v1/chats/{chat_id}/messages",
headers={
"Authorization": "Bearer " + actor["token"],
"Idempotency-Key": f"study-{uuid.uuid4().hex}",
},
json={"text": f"synthetic-{index}"},
) as response:
await response.read()
if response.status == 201:
latencies.append(time.perf_counter() - start)
instances[response.headers.get("X-Instance-Id", "missing")] += 1
else:
errors[str(response.status)] += 1
except (aiohttp.ClientError, TimeoutError) as exc:
errors[type(exc).__name__] += 1
for index in range(total_slots):
scheduled = started + index / rate
await asyncio.sleep(max(0, scheduled - time.perf_counter()))
delay = time.perf_counter() - scheduled
lag.append(delay)
if delay > 0.25:
dropped += 1
dropped_lag += 1
continue
if len(pending) >= concurrency:
dropped += 1
dropped_concurrency += 1
continue
task = asyncio.create_task(request(index))
pending.add(task)
task.add_done_callback(pending.discard)
await asyncio.gather(*pending)
elapsed = time.perf_counter() - started
latencies.sort()
def percentile(p):
return (
round(1000 * latencies[max(0, math.ceil(p * len(latencies)) - 1)], 3)
if latencies
else None
)
return {
"profile": "open-loop unique POST messages, no automatic retries",
"offered_rate": rate,
"requested_duration_seconds": duration,
"elapsed_including_drain_seconds": round(elapsed, 4),
"scheduled": total_slots,
"attempted": len(latencies) + sum(errors.values()),
"successful": len(latencies),
"errors": dict(errors),
"dropped": dropped,
"dropped_scheduler": dropped_lag,
"dropped_concurrency": dropped_concurrency,
"driver_cpu_seconds": time.process_time() - cpu_started,
"driver_cpu_fraction_one_core": (time.process_time() - cpu_started) / elapsed,
"successful_rps": round(len(latencies) / elapsed, 3),
"attempted_rps": round((len(latencies) + sum(errors.values())) / elapsed, 3),
"latency_success_ms": {
"p50": percentile(0.5),
"p95": percentile(0.95),
"p99": percentile(0.99),
},
"max_schedule_lag_ms": round(max(lag, default=0) * 1000, 3),
"instances": dict(instances),
"concurrency_cap": concurrency,
}
if __name__ == "__main__":
import argparse
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client
parser = argparse.ArgumentParser()
parser.add_argument("--rate", type=int, default=300)
parser.add_argument("--duration", type=float, default=8)
parser.add_argument("--output")
args = parser.parse_args()
c = Client("http://127.0.0.1:8083")
actor = c.user("research")
chat = c.call(
"POST",
"/api/v1/chats",
201,
actor=actor,
body={"title": "study", "member_ids": [actor["id"]]},
schema="Chat",
)["json"]
result = asyncio.run(
run_load(c.base, actor, chat["id"], rate=args.rate, duration=args.duration)
)
text = json.dumps(result, indent=2)
if args.output:
Path(args.output).write_text(text + "\n")
print(text)