forked from mtusi2026/mtusi2026lab3
130 lines
4.2 KiB
Python
130 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Open-loop HTTP benchmark: шлёт target_rps в течение duration, считает
|
|
successful RPS, p95/p99, error_ratio. Использует встроенный пул потоков;
|
|
на 400+ RPS рекомендуется запускать из Linux, не из WSL, либо уменьшить
|
|
клиентский overhead."""
|
|
import argparse
|
|
import base64
|
|
import json
|
|
import math
|
|
import sys
|
|
import threading
|
|
import time
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
from client import Client, Failure, check # noqa
|
|
|
|
|
|
class TokenBucket:
|
|
def __init__(self, rps):
|
|
self.rps = rps
|
|
self.interval = 1.0 / rps
|
|
self.next_at = time.monotonic()
|
|
self.lock = threading.Lock()
|
|
|
|
def wait(self):
|
|
with self.lock:
|
|
now = time.monotonic()
|
|
if self.next_at < now:
|
|
self.next_at = now
|
|
sleep_for = self.next_at - now
|
|
self.next_at += self.interval
|
|
if sleep_for > 0:
|
|
time.sleep(sleep_for)
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument('--base-url', default='http://localhost:8080')
|
|
p.add_argument('--target-rps', type=int, required=True)
|
|
p.add_argument('--duration', type=int, default=60)
|
|
p.add_argument('--warmup', type=int, default=10)
|
|
p.add_argument('--output', required=True)
|
|
args = p.parse_args()
|
|
|
|
c = Client(args.base_url)
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
|
|
|
latencies = []
|
|
errors = 0
|
|
lat_lock = threading.Lock()
|
|
running = threading.Event()
|
|
running.set()
|
|
|
|
bucket = TokenBucket(args.target_rps)
|
|
|
|
def sender():
|
|
# Каждый поток держит свой Client, чтобы не сериализовать соединения.
|
|
local = Client(args.base_url, timeout=30)
|
|
while running.is_set():
|
|
bucket.wait()
|
|
if not running.is_set():
|
|
return
|
|
key = uuid.uuid4().hex
|
|
start = time.monotonic()
|
|
try:
|
|
local.call('POST', path, (201, 503), actor=alice,
|
|
body={'text': 'ol-' + key},
|
|
headers={'Idempotency-Key': key})
|
|
elapsed = time.monotonic() - start
|
|
with lat_lock:
|
|
latencies.append(elapsed)
|
|
except Failure:
|
|
with lat_lock:
|
|
errors += 1
|
|
|
|
# Пул потоков — 32, чтобы суммарно не упереться в клиент.
|
|
# Интенсивность задаёт TokenBucket; потоки только реализуют её.
|
|
pool_size = min(32, max(8, args.target_rps // 10))
|
|
with ThreadPoolExecutor(max_workers=pool_size) as pool:
|
|
# warm-up
|
|
warm_start = time.monotonic()
|
|
while time.monotonic() - warm_start < args.warmup:
|
|
bucket.wait()
|
|
# reset stats после warmup
|
|
with lat_lock:
|
|
latencies.clear()
|
|
errors = 0
|
|
|
|
for _ in range(pool_size):
|
|
pool.submit(sender)
|
|
|
|
started = time.monotonic()
|
|
time.sleep(args.duration)
|
|
running.clear()
|
|
# ждём in-flight
|
|
pool.shutdown(wait=True, cancel_futures=False)
|
|
|
|
elapsed = time.monotonic() - started
|
|
lat = sorted(latencies)
|
|
|
|
def pct(p):
|
|
if not lat:
|
|
return None
|
|
idx = max(0, math.ceil(p * len(lat)) - 1)
|
|
return round(lat[idx] * 1000, 3)
|
|
|
|
total = len(lat) + errors
|
|
report = {
|
|
'target_rps': args.target_rps,
|
|
'duration_seconds': args.duration,
|
|
'elapsed_seconds': round(elapsed, 3),
|
|
'successful_requests': len(lat),
|
|
'failed_requests': errors,
|
|
'successful_rps': round(len(lat) / elapsed, 3),
|
|
'error_ratio': round(errors / total, 6) if total else None,
|
|
'successful_latency_ms': {
|
|
'p50': pct(.50), 'p95': pct(.95), 'p99': pct(.99),
|
|
},
|
|
}
|
|
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
|
Path(args.output).write_text(json.dumps(report, indent=2))
|
|
print(json.dumps(report, indent=2))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main() |