import time import base64 import uuid import httpx from concurrent.futures import ThreadPoolExecutor BASE_URL = "http://localhost:8080" def get_client(): return httpx.Client(base_url=BASE_URL, trust_env=False, timeout=10.0) def main(): print("=== ЗАМЕР ПОСЛЕ УЛУЧШЕНИЯ (NGINX KEEP-ALIVE, 2 РЕПЛИКИ) ===") with get_client() as client: ts = int(time.time() * 1000) uname = f"after_{ts}" pw = "password_12345678" client.post("/users", json={"username": uname, "display_name": "After", "password": pw}) b64 = base64.b64encode(f"{uname}:{pw}".encode()).decode() token = client.post("/auth/sessions", headers={"Authorization": f"Basic {b64}"}).json()["token"] uid = client.get("/users/me", headers={"Authorization": f"Bearer {token}"}).json()["id"] h = {"Authorization": f"Bearer {token}"} chat_id = client.post("/chats", json={"title": "After Chat", "member_ids": [uid]}, headers=h).json()["id"] print("Запуск нагрузки (10 секунд, concurrency=8)...") stop_at = time.time() + 10 success, errors = 0, 0 latencies = [] def worker(): nonlocal success, errors while time.time() < stop_at: t0 = time.time() try: idem = f"after_{uuid.uuid4()}" r = client.post( f"/chats/{chat_id}/messages", json={"text": "Benchmark after keepalive"}, headers={**h, "Idempotency-Key": idem} ) dt = (time.time() - t0) * 1000 if r.status_code == 201: success += 1 latencies.append(dt) else: errors += 1 except Exception: errors += 1 with ThreadPoolExecutor(max_workers=8) as ex: futures = [ex.submit(worker) for _ in range(8)] for f in futures: f.result() latencies.sort() n = len(latencies) p50 = latencies[int(n * 0.50)] if n else 0 p95 = latencies[int(n * 0.95)] if n else 0 p99 = latencies[int(n * 0.99)] if n else 0 rps = success / 10.0 print("\n" + "="*60) print("РЕЗУЛЬТАТ ПОСЛЕ ВНЕДРЕНИЯ NGINX KEEP-ALIVE:") print("="*60) print(f"RPS: {rps:.1f} | p50: {p50:.1f} ms | p95: {p95:.1f} ms | p99: {p99:.1f} ms | Ошибки: {errors}") if __name__ == "__main__": main()