лаба на 5
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from collections import Counter
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client, Failure
|
||||
|
||||
local = threading.local()
|
||||
|
||||
|
||||
def percentile(values, p):
|
||||
if not values:
|
||||
return None
|
||||
values = sorted(values)
|
||||
index = max(0, math.ceil(len(values) * p) - 1)
|
||||
return round(values[index], 3)
|
||||
|
||||
|
||||
def latency_summary(values):
|
||||
return {
|
||||
"p50": percentile(values, 0.50),
|
||||
"p95": percentile(values, 0.95),
|
||||
"p99": percentile(values, 0.99),
|
||||
"mean": round(sum(values) / len(values), 3)
|
||||
if values else None,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--requests", type=int, default=600)
|
||||
parser.add_argument("--rate", type=float, default=20)
|
||||
parser.add_argument("--workers", type=int, default=16)
|
||||
parser.add_argument("--warmup", type=int, default=50)
|
||||
parser.add_argument("--start-marker", default=None)
|
||||
parser.add_argument("--settle", type=float, default=10)
|
||||
parser.add_argument("--mode", required=True)
|
||||
parser.add_argument("--output", required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
if min(args.requests, args.rate, args.workers) <= 0:
|
||||
parser.error("requests, rate and workers must be positive")
|
||||
|
||||
client = Client(args.base_url)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
|
||||
def request(index, scheduled):
|
||||
if not hasattr(local, "client"):
|
||||
local.client = Client(args.base_url, timeout=10)
|
||||
|
||||
started = time.perf_counter()
|
||||
outcome = "success"
|
||||
|
||||
try:
|
||||
local.client.message(
|
||||
alice, chat, f"benchmark-message-{index:08d}"
|
||||
)
|
||||
except Failure:
|
||||
outcome = "error"
|
||||
|
||||
finished = time.perf_counter()
|
||||
|
||||
return {
|
||||
"outcome": outcome,
|
||||
"latency_ms": (finished - started) * 1000,
|
||||
"scheduled_latency_ms": (finished - scheduled) * 1000,
|
||||
"queue_delay_ms": (started - scheduled) * 1000,
|
||||
}
|
||||
|
||||
with ThreadPoolExecutor(max_workers=args.workers) as pool:
|
||||
for index in range(args.warmup):
|
||||
result = pool.submit(
|
||||
request, -index - 1, time.perf_counter()
|
||||
).result()
|
||||
|
||||
if result["outcome"] != "success":
|
||||
raise RuntimeError("Warmup failed")
|
||||
|
||||
time.sleep(args.settle)
|
||||
|
||||
if args.start_marker:
|
||||
marker = Path(args.start_marker)
|
||||
marker.parent.mkdir(parents=True, exist_ok=True)
|
||||
marker.write_text(str(time.time()))
|
||||
|
||||
deadline = time.monotonic() + 120
|
||||
while marker.exists():
|
||||
if time.monotonic() > deadline:
|
||||
raise RuntimeError("Measurement handshake timeout")
|
||||
time.sleep(0.05)
|
||||
|
||||
started_at = time.time()
|
||||
started = time.perf_counter()
|
||||
futures = []
|
||||
|
||||
for index in range(args.requests):
|
||||
scheduled = started + index / args.rate
|
||||
delay = scheduled - time.perf_counter()
|
||||
|
||||
if delay > 0:
|
||||
time.sleep(delay)
|
||||
|
||||
futures.append(pool.submit(request, index, scheduled))
|
||||
|
||||
results = [future.result() for future in futures]
|
||||
elapsed = time.perf_counter() - started
|
||||
finished_at = time.time()
|
||||
|
||||
counts = Counter(row["outcome"] for row in results)
|
||||
|
||||
successful = [
|
||||
row["latency_ms"]
|
||||
for row in results
|
||||
if row["outcome"] == "success"
|
||||
]
|
||||
|
||||
report = {
|
||||
"mode": args.mode,
|
||||
"workload": "fixed-rate POST messages",
|
||||
"started_at_unix": started_at,
|
||||
"finished_at_unix": finished_at,
|
||||
"requested_requests": args.requests,
|
||||
"target_rps": args.rate,
|
||||
"workers": args.workers,
|
||||
"warmup_requests": args.warmup,
|
||||
"elapsed_seconds": round(elapsed, 3),
|
||||
"successful_requests": counts["success"],
|
||||
"failed_requests": counts["error"],
|
||||
"error_ratio": round(counts["error"] / args.requests, 6),
|
||||
"actual_rps": round(args.requests / elapsed, 3),
|
||||
"successful_rps": round(counts["success"] / elapsed, 3),
|
||||
"successful_latency_ms": latency_summary(successful),
|
||||
"all_latency_ms": latency_summary([
|
||||
row["latency_ms"] for row in results
|
||||
]),
|
||||
"scheduled_latency_ms": latency_summary([
|
||||
row["scheduled_latency_ms"] for row in results
|
||||
]),
|
||||
"client_queue_delay_ms": latency_summary([
|
||||
row["queue_delay_ms"] for row in results
|
||||
]),
|
||||
}
|
||||
|
||||
output = Path(args.output)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
text = json.dumps(report, indent=2, ensure_ascii=False)
|
||||
output.write_text(text + "\n")
|
||||
print(text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user