1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
"""codspeed benchmarks for websocket client."""
import asyncio
from pytest_codspeed import BenchmarkFixture
from aiohttp import web
from aiohttp._websocket.helpers import MSG_SIZE
from aiohttp.pytest_plugin import AiohttpClient
def test_one_thousand_round_trip_websocket_text_messages(
loop: asyncio.AbstractEventLoop,
aiohttp_client: AiohttpClient,
benchmark: BenchmarkFixture,
) -> None:
"""Benchmark round trip of 1000 WebSocket text messages."""
message_count = 1000
async def handler(request: web.Request) -> web.WebSocketResponse:
ws = web.WebSocketResponse()
await ws.prepare(request)
for _ in range(message_count):
await ws.send_str("answer")
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())
def test_one_thousand_round_trip_websocket_binary_messages(
loop: asyncio.AbstractEventLoop,
aiohttp_client: AiohttpClient,
benchmark: BenchmarkFixture,
) -> None:
"""Benchmark round trip of 1000 WebSocket binary messages."""
message_count = 1000
async def handler(request: web.Request) -> web.WebSocketResponse:
ws = web.WebSocketResponse()
await ws.prepare(request)
for _ in range(message_count):
await ws.send_bytes(b"answer")
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())
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())
|