#!/usr/bin/env python3 import http.client import json import threading import time import uuid BASE_URL = "127.0.0.1" PORT = 8080 def worker(token, chat_id, duration, stats): # Каждый поток открывает ОДНО соединение и шлет по нему много запросов conn = http.client.HTTPConnection(BASE_URL, PORT) headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" } end_time = time.time() + duration while time.time() < end_time: headers["Idempotency-Key"] = uuid.uuid4().hex body = json.dumps({"text": "load-test"}) try: conn.request("POST", f"/api/v1/chats/{chat_id}/messages", body=body, headers=headers) resp = conn.getresponse() resp.read() if resp.status == 201: stats['success'] += 1 else: stats['error'] += 1 except Exception: stats['error'] += 1 conn = http.client.HTTPConnection(BASE_URL, PORT) # Переподключение при ошибке def main(): # 1. Подготовка (регистрация, логин, создание чата) # Для простоты используем готовый tests/client.py import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from client import Client c = Client("http://127.0.0.1:8080") alice, bob, eve, chat = c.bootstrap() token = alice['token'] # 2. Запуск нагрузки duration = 20 num_threads = 20 stats = {'success': 0, 'error': 0} threads = [threading.Thread(target=worker, args=(token, chat['id'], duration, stats)) for _ in range(num_threads)] print(f"Starting load: {num_threads} threads for {duration}s...") start_time = time.time() for t in threads: t.start() for t in threads: t.join() elapsed = time.time() - start_time rps = stats['success'] / elapsed print(f"RESULT: RPS={rps:.2f}, Success={stats['success']}, Errors={stats['error']}") if __name__ == '__main__': main()