From 00927573a270a82a65e1b2fbdf1cd79b8bbe82bc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Nov 2024 21:08:44 -0500 Subject: [PATCH 1/5] Add benchmark for sending large WebSocket messages --- tests/test_benchmarks_http_websocket.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/test_benchmarks_http_websocket.py b/tests/test_benchmarks_http_websocket.py index 11fe132879f..5742d30bd66 100644 --- a/tests/test_benchmarks_http_websocket.py +++ b/tests/test_benchmarks_http_websocket.py @@ -1,10 +1,12 @@ """codspeed benchmarks for http websocket.""" import asyncio +from typing import Iterable, Union from pytest_codspeed import BenchmarkFixture from aiohttp import DataQueue +from aiohttp._websocket.helpers import MSG_SIZE from aiohttp.base_protocol import BaseProtocol from aiohttp.http_websocket import ( WebSocketReader, @@ -41,7 +43,7 @@ def is_closing(self) -> bool: """Swallow is_closing.""" return False - def write(self, data: bytes) -> None: + def write(self, data: Iterable[Union[bytes, bytearray, memoryview]]) -> None: """Swallow writes.""" @@ -67,6 +69,22 @@ def _run() -> None: loop.run_until_complete(_send_one_hundred_websocket_text_messages()) +def test_send_one_hundred_large_websocket_text_messages( + loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture +) -> None: + """Benchmark sending 100 WebSocket text messages.""" + writer = WebSocketWriter(MockProtocol(loop=loop), MockTransport()) + raw_message = b"x" * MSG_SIZE * 2 + + async def _send_one_hundred_websocket_text_messages() -> None: + for _ in range(100): + await writer.send_frame(raw_message, WSMsgType.TEXT) + + @benchmark + def _run() -> None: + loop.run_until_complete(_send_one_hundred_websocket_text_messages()) + + def test_send_one_hundred_websocket_text_messages_with_mask( loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture ) -> None: From 938c5c67e1355a1ac203aaf53db0465347f720ac Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Nov 2024 21:10:30 -0500 Subject: [PATCH 2/5] Update tests/test_benchmarks_http_websocket.py --- tests/test_benchmarks_http_websocket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_benchmarks_http_websocket.py b/tests/test_benchmarks_http_websocket.py index 5742d30bd66..8bc15d0e306 100644 --- a/tests/test_benchmarks_http_websocket.py +++ b/tests/test_benchmarks_http_websocket.py @@ -43,7 +43,7 @@ def is_closing(self) -> bool: """Swallow is_closing.""" return False - def write(self, data: Iterable[Union[bytes, bytearray, memoryview]]) -> None: + def write(self, data: Union[bytes, bytearray, memoryview]) -> None: """Swallow writes.""" From 319f13e2e3428c659a0a4c8b30a56f0004674a8b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Nov 2024 21:28:57 -0500 Subject: [PATCH 3/5] Apply suggestions from code review --- tests/test_benchmarks_http_websocket.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_benchmarks_http_websocket.py b/tests/test_benchmarks_http_websocket.py index 8bc15d0e306..05379b6e38f 100644 --- a/tests/test_benchmarks_http_websocket.py +++ b/tests/test_benchmarks_http_websocket.py @@ -1,7 +1,7 @@ """codspeed benchmarks for http websocket.""" import asyncio -from typing import Iterable, Union +from typing import Union from pytest_codspeed import BenchmarkFixture @@ -74,7 +74,7 @@ def test_send_one_hundred_large_websocket_text_messages( ) -> None: """Benchmark sending 100 WebSocket text messages.""" writer = WebSocketWriter(MockProtocol(loop=loop), MockTransport()) - raw_message = b"x" * MSG_SIZE * 2 + raw_message = b"x" * MSG_SIZE * 4 async def _send_one_hundred_websocket_text_messages() -> None: for _ in range(100): From db923934973ac693ee57e2c6b9866b5f7911caa8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Nov 2024 21:36:28 -0500 Subject: [PATCH 4/5] reduce code --- tests/test_benchmarks_http_websocket.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/test_benchmarks_http_websocket.py b/tests/test_benchmarks_http_websocket.py index 05379b6e38f..b47e75262fc 100644 --- a/tests/test_benchmarks_http_websocket.py +++ b/tests/test_benchmarks_http_websocket.py @@ -64,9 +64,7 @@ async def _send_one_hundred_websocket_text_messages() -> None: for _ in range(100): await writer.send_frame(raw_message, WSMsgType.TEXT) - @benchmark - def _run() -> None: - loop.run_until_complete(_send_one_hundred_websocket_text_messages()) + benchmark(loop.run_until_complete(_send_one_hundred_websocket_text_messages())) def test_send_one_hundred_large_websocket_text_messages( @@ -80,9 +78,7 @@ async def _send_one_hundred_websocket_text_messages() -> None: for _ in range(100): await writer.send_frame(raw_message, WSMsgType.TEXT) - @benchmark - def _run() -> None: - loop.run_until_complete(_send_one_hundred_websocket_text_messages()) + benchmark(loop.run_until_complete(_send_one_hundred_websocket_text_messages())) def test_send_one_hundred_websocket_text_messages_with_mask( @@ -96,6 +92,4 @@ async def _send_one_hundred_websocket_text_messages() -> None: for _ in range(100): await writer.send_frame(raw_message, WSMsgType.TEXT) - @benchmark - def _run() -> None: - loop.run_until_complete(_send_one_hundred_websocket_text_messages()) + benchmark(loop.run_until_complete(_send_one_hundred_websocket_text_messages())) From 70fc69e30f80b3db638af973e70a93dae88ae77f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Nov 2024 21:54:08 -0500 Subject: [PATCH 5/5] Revert "reduce code" This reverts commit db923934973ac693ee57e2c6b9866b5f7911caa8. --- tests/test_benchmarks_http_websocket.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/test_benchmarks_http_websocket.py b/tests/test_benchmarks_http_websocket.py index b47e75262fc..05379b6e38f 100644 --- a/tests/test_benchmarks_http_websocket.py +++ b/tests/test_benchmarks_http_websocket.py @@ -64,7 +64,9 @@ async def _send_one_hundred_websocket_text_messages() -> None: for _ in range(100): await writer.send_frame(raw_message, WSMsgType.TEXT) - benchmark(loop.run_until_complete(_send_one_hundred_websocket_text_messages())) + @benchmark + def _run() -> None: + loop.run_until_complete(_send_one_hundred_websocket_text_messages()) def test_send_one_hundred_large_websocket_text_messages( @@ -78,7 +80,9 @@ async def _send_one_hundred_websocket_text_messages() -> None: for _ in range(100): await writer.send_frame(raw_message, WSMsgType.TEXT) - benchmark(loop.run_until_complete(_send_one_hundred_websocket_text_messages())) + @benchmark + def _run() -> None: + loop.run_until_complete(_send_one_hundred_websocket_text_messages()) def test_send_one_hundred_websocket_text_messages_with_mask( @@ -92,4 +96,6 @@ async def _send_one_hundred_websocket_text_messages() -> None: for _ in range(100): await writer.send_frame(raw_message, WSMsgType.TEXT) - benchmark(loop.run_until_complete(_send_one_hundred_websocket_text_messages())) + @benchmark + def _run() -> None: + loop.run_until_complete(_send_one_hundred_websocket_text_messages())