Skip to content

Commit e1e8385

Browse files
add asyncio_webockets benchmark
1 parent eaa3d0c commit e1e8385

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ async_tree_eager_io_tg <local:async_tree>
2121
async_tree_eager_memoization_tg <local:async_tree>
2222
asyncio_tcp <local>
2323
asyncio_tcp_ssl <local:asyncio_tcp>
24+
asyncio_websockets <local>
2425
concurrent_imap <local>
2526
coroutines <local>
2627
coverage <local>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[project]
2+
name = "pyperformance_bm_asyncio_websockets"
3+
requires-python = ">=3.8"
4+
dependencies = ["pyperf", "websockets"]
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "asyncio_websockets"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
websockets==11.0.3
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Benchmark for asyncio websocket server and client performance
3+
transferring 10MB of data.
4+
"""
5+
6+
import pyperf
7+
import websockets.server
8+
import websockets.client
9+
import asyncio
10+
11+
CHUNK_SIZE = 1024
12+
DATA = b"x" * CHUNK_SIZE
13+
14+
stop: asyncio.Event
15+
16+
17+
async def handler(websocket) -> None:
18+
for _ in range(100):
19+
data = await websocket.recv()
20+
assert data == DATA
21+
22+
stop.set()
23+
24+
25+
async def main() -> None:
26+
global stop
27+
t0 = pyperf.perf_counter()
28+
stop = asyncio.Event()
29+
async with websockets.server.serve(handler, "", 8001):
30+
async with websockets.client.connect("ws://localhost:8001") as ws:
31+
await asyncio.gather(*[ws.send(DATA) for _ in range(100)])
32+
await stop.wait()
33+
return pyperf.perf_counter() - t0
34+
35+
36+
if __name__ == "__main__":
37+
runner = pyperf.Runner()
38+
runner.metadata['description'] = "Benchmark asyncio websockets"
39+
runner.bench_async_func('asyncio_websockets', main)

0 commit comments

Comments
 (0)