From 0af52a5a4caf093f886d03b7f6cb716fde23193d Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 13 Dec 2022 07:25:08 +0000 Subject: [PATCH 1/3] add asyncio tcp benchmark --- pyperformance/data-files/benchmarks/MANIFEST | 1 + .../benchmarks/bm_asyncio_tcp/pyproject.toml | 9 ++++ .../bm_asyncio_tcp/run_benchmark.py | 41 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 pyperformance/data-files/benchmarks/bm_asyncio_tcp/pyproject.toml create mode 100644 pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index 6c5d6e07..671efd15 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -7,6 +7,7 @@ async_tree async_tree_cpu_io_mixed async_tree_io async_tree_memoization +asyncio_tcp concurrent_imap coroutines coverage diff --git a/pyperformance/data-files/benchmarks/bm_asyncio_tcp/pyproject.toml b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/pyproject.toml new file mode 100644 index 00000000..2e5f1e47 --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/pyproject.toml @@ -0,0 +1,9 @@ +[project] +name = "pyperformance_bm_asyncio_tcp" +requires-python = ">=3.8" +dependencies = ["pyperf"] +urls = {repository = "https://github.com/python/pyperformance"} +dynamic = ["version"] + +[tool.pyperformance] +name = "asyncio_tcp" diff --git a/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py new file mode 100644 index 00000000..3f76b6c2 --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py @@ -0,0 +1,41 @@ +""" +Benchmark for asyncio TCP server and client performance +transferring 10MB of data. + +Author: Kumar Aditya +""" + + +import asyncio +from pyperf import Runner + + +CHUNK_SIZE = 1024 ** 2 * 10 + + +async def handle_echo(reader: asyncio.StreamReader, + writer: asyncio.StreamWriter) -> None: + data = b'x' * CHUNK_SIZE + for _ in range(100): + writer.write(data) + await writer.drain() + writer.close() + await writer.wait_closed() + + +async def main() -> None: + server = await asyncio.start_server(handle_echo, '127.0.0.1', 8882) + + async with server: + asyncio.create_task(server.start_serving()) + reader, writer = await asyncio.open_connection('127.0.0.1', 8882) + while True: + data = await reader.read(CHUNK_SIZE) + if not data: + break + writer.close() + await writer.wait_closed() + +if __name__ == '__main__': + runner = Runner() + runner.bench_async_func('asyncio_tcp', main) From 58a7420fd42b3b5ffeb06ddf8f72cd807f58b5b8 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 19 Dec 2022 10:54:42 +0000 Subject: [PATCH 2/3] check for received length --- .../data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py index 3f76b6c2..f69dcaf7 100644 --- a/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py @@ -29,10 +29,13 @@ async def main() -> None: async with server: asyncio.create_task(server.start_serving()) reader, writer = await asyncio.open_connection('127.0.0.1', 8882) + received = 0 while True: data = await reader.read(CHUNK_SIZE) if not data: break + received += len(data) + assert received == CHUNK_SIZE * 100 writer.close() await writer.wait_closed() From 5a258f4ba6d0b3519ceb6b1b02bb22d57d7d9f58 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 19 Dec 2022 10:57:05 +0000 Subject: [PATCH 3/3] rename var --- .../data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py index f69dcaf7..e400fcd1 100644 --- a/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py @@ -29,13 +29,13 @@ async def main() -> None: async with server: asyncio.create_task(server.start_serving()) reader, writer = await asyncio.open_connection('127.0.0.1', 8882) - received = 0 + data_len = 0 while True: data = await reader.read(CHUNK_SIZE) if not data: break - received += len(data) - assert received == CHUNK_SIZE * 100 + data_len += len(data) + assert data_len == CHUNK_SIZE * 100 writer.close() await writer.wait_closed()