From 1f830ec46a78adc81ccf17b4a038e8c6b6585d9c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 Nov 2024 01:30:32 -0600 Subject: [PATCH] Add benchmark for roundtrip of 100 large WebSocket messages (#9645) (cherry picked from commit 541b14965b81363c2ee3c5e2030333fb5bd4565d) --- tests/test_benchmarks_client_ws.py | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_benchmarks_client_ws.py b/tests/test_benchmarks_client_ws.py index 29d6047b3fa..f93e84a9cba 100644 --- a/tests/test_benchmarks_client_ws.py +++ b/tests/test_benchmarks_client_ws.py @@ -5,6 +5,7 @@ from pytest_codspeed import BenchmarkFixture from aiohttp import web +from aiohttp._websocket.helpers import MSG_SIZE from aiohttp.pytest_plugin import AiohttpClient @@ -37,3 +38,35 @@ async def run_websocket_benchmark() -> None: @benchmark def _run() -> None: loop.run_until_complete(run_websocket_benchmark()) + + +def test_one_thousand_large_round_trip_websocket_text_messages( + loop: asyncio.AbstractEventLoop, + aiohttp_client: AiohttpClient, + benchmark: BenchmarkFixture, +) -> None: + """Benchmark round trip of 100 large WebSocket text messages.""" + message_count = 100 + raw_message = "x" * MSG_SIZE * 4 + + async def handler(request: web.Request) -> web.WebSocketResponse: + ws = web.WebSocketResponse() + await ws.prepare(request) + for _ in range(message_count): + await ws.send_str(raw_message) + await ws.close() + return ws + + app = web.Application() + app.router.add_route("GET", "/", handler) + + async def run_websocket_benchmark() -> None: + client = await aiohttp_client(app) + resp = await client.ws_connect("/") + for _ in range(message_count): + await resp.receive() + await resp.close() + + @benchmark + def _run() -> None: + loop.run_until_complete(run_websocket_benchmark())