forked from mtusi2026/mtusi2026lab3
168 lines
5.9 KiB
Python
168 lines
5.9 KiB
Python
"""Повторяемый closed-loop: постоянные HTTP-соединения, 80% POST / 20% GET.
|
|
Не измеряет open-loop SLA. Ошибки и задержки всех попыток учитываются отдельно.
|
|
"""
|
|
|
|
import http.client
|
|
import json
|
|
import math
|
|
import os
|
|
import sys
|
|
import threading
|
|
import time
|
|
import uuid
|
|
from collections import Counter
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
from urllib.parse import urlsplit
|
|
|
|
from runtime_config import base_url
|
|
|
|
os.environ.setdefault("BASE_URL", base_url())
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
|
from client import Client
|
|
|
|
|
|
def fixture():
|
|
c = Client(os.environ.get("BASE_URL"))
|
|
actor = c.user("bench")
|
|
chat = c.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
201,
|
|
actor=actor,
|
|
body={"title": "Synthetic benchmark", "member_ids": [actor["id"]]},
|
|
schema="Chat",
|
|
)["json"]
|
|
for n in range(20):
|
|
c.message(actor, chat, "seed-%d" % n)
|
|
return actor, chat
|
|
|
|
|
|
def measure(actor, chat, duration=15, concurrency=16, event=None):
|
|
url = urlsplit(os.environ.get("BASE_URL", "http://localhost:8080"))
|
|
assert url.scheme == "http", "Local isolated HTTP benchmark only"
|
|
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
barrier = threading.Barrier(concurrency + 1)
|
|
start = [None]
|
|
prefix = uuid.uuid4().hex
|
|
|
|
def worker(index):
|
|
connection = http.client.HTTPConnection(url.hostname, url.port or 80, timeout=5)
|
|
headers = {
|
|
"Authorization": "Bearer " + actor["token"],
|
|
"Content-Type": "application/json",
|
|
}
|
|
latencies, successes, failures, instances, seconds, confirmations = (
|
|
[],
|
|
Counter(),
|
|
Counter(),
|
|
set(),
|
|
{},
|
|
[],
|
|
)
|
|
completed = []
|
|
counter = 0
|
|
barrier.wait()
|
|
while time.monotonic() < start[0] + duration:
|
|
write = counter % 5 != 4
|
|
key = "%s-%d-%d" % (prefix, index, counter)
|
|
body = json.dumps({"text": "load-" + key}) if write else None
|
|
before = time.monotonic()
|
|
status = 0
|
|
try:
|
|
connection.request(
|
|
"POST" if write else "GET",
|
|
path if write else path + "?limit=20",
|
|
body=body,
|
|
headers={**headers, "Idempotency-Key": key},
|
|
)
|
|
response = connection.getresponse()
|
|
data = response.read()
|
|
status = response.status
|
|
if status == (201 if write else 200):
|
|
parsed = json.loads(data)
|
|
successes["write" if write else "read"] += 1
|
|
instances.add(response.getheader("X-Instance-Id"))
|
|
completed.append((time.monotonic(), "write" if write else "read"))
|
|
if write:
|
|
confirmations.append((key, parsed["id"]))
|
|
else:
|
|
failures[str(status)] += 1
|
|
except (OSError, http.client.HTTPException, ValueError):
|
|
failures["connection"] += 1
|
|
connection.close()
|
|
elapsed = time.monotonic() - before
|
|
latencies.append((elapsed, status == (201 if write else 200)))
|
|
bucket = int(before - start[0])
|
|
record = seconds.setdefault(bucket, {"success": 0, "error": 0})
|
|
record["success" if status == (201 if write else 200) else "error"] += 1
|
|
counter += 1
|
|
connection.close()
|
|
return (
|
|
latencies,
|
|
successes,
|
|
failures,
|
|
instances,
|
|
seconds,
|
|
confirmations,
|
|
completed,
|
|
)
|
|
|
|
with ThreadPoolExecutor(max_workers=concurrency) as pool:
|
|
futures = [pool.submit(worker, i) for i in range(concurrency)]
|
|
start[0] = time.monotonic()
|
|
barrier.wait()
|
|
results = [f.result() for f in futures]
|
|
elapsed = time.monotonic() - start[0]
|
|
latencies = sorted(v for r in results for v, _ in r[0])
|
|
successful = sorted(v for r in results for v, ok in r[0] if ok)
|
|
successes, failures, instances, seconds = Counter(), Counter(), set(), {}
|
|
for _, ok, bad, seen, buckets, _, _ in results:
|
|
successes.update(ok)
|
|
failures.update(bad)
|
|
instances.update(seen)
|
|
for b, counts in buckets.items():
|
|
dest = seconds.setdefault(b, {"success": 0, "error": 0})
|
|
for k, v in counts.items():
|
|
dest[k] += v
|
|
|
|
def percentiles(values):
|
|
return {
|
|
name: round(values[max(0, math.ceil(p * len(values)) - 1)] * 1000, 3)
|
|
if values
|
|
else None
|
|
for name, p in [("p50", 0.5), ("p95", 0.95), ("p99", 0.99)]
|
|
}
|
|
|
|
report = {
|
|
"duration_seconds": duration,
|
|
"elapsed_seconds": round(elapsed, 3),
|
|
"concurrency": concurrency,
|
|
"successes": dict(successes),
|
|
"errors": dict(failures),
|
|
"rps": round(sum(successes.values()) / elapsed, 3),
|
|
"all_attempt_latency_ms": percentiles(latencies),
|
|
"successful_latency_ms": percentiles(successful),
|
|
"instances": sorted(instances),
|
|
"seconds": seconds,
|
|
"profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries",
|
|
}
|
|
if event:
|
|
report["first_success_after_action_seconds"] = {
|
|
kind: round(
|
|
min(
|
|
(
|
|
at - event[0]
|
|
for r in results
|
|
for at, method in r[6]
|
|
if method == kind and at >= event[0]
|
|
),
|
|
default=duration,
|
|
),
|
|
3,
|
|
)
|
|
for kind in ["read", "write"]
|
|
}
|
|
return report, [x for r in results for x in r[5]]
|