Skip to content

Commit 8f6d7b5

Browse files
[PR #9658/6c932dd7 backport][3.11] Add benchmark for sending binary WebSocket messages (#9662)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 8ffa384 commit 8f6d7b5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/test_benchmarks_client_ws.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,37 @@ def _run() -> None:
4040
loop.run_until_complete(run_websocket_benchmark())
4141

4242

43+
def test_one_thousand_round_trip_websocket_binary_messages(
44+
loop: asyncio.AbstractEventLoop,
45+
aiohttp_client: AiohttpClient,
46+
benchmark: BenchmarkFixture,
47+
) -> None:
48+
"""Benchmark round trip of 1000 WebSocket binary messages."""
49+
message_count = 1000
50+
51+
async def handler(request: web.Request) -> web.WebSocketResponse:
52+
ws = web.WebSocketResponse()
53+
await ws.prepare(request)
54+
for _ in range(message_count):
55+
await ws.send_bytes(b"answer")
56+
await ws.close()
57+
return ws
58+
59+
app = web.Application()
60+
app.router.add_route("GET", "/", handler)
61+
62+
async def run_websocket_benchmark() -> None:
63+
client = await aiohttp_client(app)
64+
resp = await client.ws_connect("/")
65+
for _ in range(message_count):
66+
await resp.receive()
67+
await resp.close()
68+
69+
@benchmark
70+
def _run() -> None:
71+
loop.run_until_complete(run_websocket_benchmark())
72+
73+
4374
def test_one_thousand_large_round_trip_websocket_text_messages(
4475
loop: asyncio.AbstractEventLoop,
4576
aiohttp_client: AiohttpClient,

0 commit comments

Comments
 (0)