8000 Add `asyncio_tcp` benchmark (#254) · python/pyperformance@b3c5e70 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3c5e70

Browse files
Add asyncio_tcp benchmark (#254)
1 parent 340008c commit b3c5e70

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ async_tree <local>
77
async_tree_cpu_io_mixed <local:async_tree>
88
async_tree_io <local:async_tree>
99
async_tree_memoization <local:async_tree>
10+
asyncio_tcp <local>
1011
concurrent_imap <local>
1112
coroutines <local>
1213
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_tcp"
3+
requires-python = ">=3.8"
4+
dependencies = ["pyperf"]
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "asyncio_tcp"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Benchmark for asyncio TCP server and client performance
3+
transferring 10MB of data.
4+
5+
Author: Kumar Aditya
6+
"""
7+
8+
9+
import asyncio
10+
from pyperf import Runner
11+
12+
13+
CHUNK_SIZE = 1024 ** 2 * 10
14+
15+
16+
async def handle_echo(reader: asyncio.StreamReader,
17+
writer: asyncio.StreamWriter) -> None:
18+
data = b'x' * CHUNK_SIZE
19+
for _ in range(100):
20+
writer.write(data)
21+
await writer.drain()
22+
writer.close()
23+
await writer.wait_closed()
24+
25+
26+
async def main() -> None:
27+
server = await asyncio.start_server(handle_echo, '127.0.0.1', 8882)
28+
29+
async with server:
30+
asyncio.create_task(server.start_serving())
31+
reader, writer = await asyncio.open_connection('127.0.0.1', 8882)
32+
data_len = 0
33+
while True:
34+
data = await reader.read(CHUNK_SIZE)
35+
if not data:
36+
break
37+
data_len += len(data)
38+
assert data_len == CHUNK_SIZE * 100
39+
writer.close()
40+
await writer.wait_closed()
41+
42+
if __name__ == '__main__':
43+
runner = Runner()
44+
runner.bench_async_func('asyncio_tcp', main)

0 commit comments

Comments
 (0)
0