Skip to content

Commit

Permalink
Add benchmark for sending large WebSocket messages (#9635)
Browse files Browse the repository at this point in the history
(cherry picked from commit 23d2966)
  • Loading branch information
bdraco authored and patchback[bot] committed Nov 3, 2024
1 parent 0550d8f commit 3b8379e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tests/test_benchmarks_http_websocket.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""codspeed benchmarks for http websocket."""

import asyncio
from typing import Union

from pytest_codspeed import BenchmarkFixture # type: ignore[import-untyped]

from aiohttp import DataQueue
from aiohttp._websocket.helpers import MSG_SIZE
from aiohttp.base_protocol import BaseProtocol
from aiohttp.http_websocket import (
WebSocketReader,
Expand Down Expand Up @@ -41,7 +43,7 @@ def is_closing(self) -> bool:
"""Swallow is_closing."""
return False

def write(self, data: bytes) -> None:
def write(self, data: Union[bytes, bytearray, memoryview]) -> None:
"""Swallow writes."""


Expand All @@ -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 * 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())


def test_send_one_hundred_websocket_text_messages_with_mask(
loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture
) -> None:
Expand Down

0 comments on commit 3b8379e

Please sign in to comment.