From 24568fa9392717c20614a61b8704d9bd5699eda1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 17 Nov 2024 12:32:40 -0600 Subject: [PATCH 1/3] Add benchmarks for creating web responses --- tests/test_benchmarks_web_response.py | 92 +++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/test_benchmarks_web_response.py diff --git a/tests/test_benchmarks_web_response.py b/tests/test_benchmarks_web_response.py new file mode 100644 index 00000000000..fab628485d1 --- /dev/null +++ b/tests/test_benchmarks_web_response.py @@ -0,0 +1,92 @@ +"""codspeed benchmarks for the web responses.""" + +import asyncio + +from pytest_codspeed import BenchmarkFixture + +from aiohttp import web + + +def test_simple_web_response( + loop: asyncio.AbstractEventLoop, + benchmark: BenchmarkFixture, +) -> None: + """Benchmark creating 100 simple web.Response.""" + response_count = 100 + + async def run_web_response_benchmark() -> None: + for _ in range(response_count): + web.Response() + + @benchmark + def _run() -> None: + loop.run_until_complete(run_web_response_benchmark()) + + +def test_web_response_with_headers( + loop: asyncio.AbstractEventLoop, + benchmark: BenchmarkFixture, +) -> None: + """Benchmark creating 100 web.Response with headers.""" + response_count = 100 + headers = { + "Content-Type": "text/plain", + "Server": "aiohttp", + "Date": "Sun, 01 Aug 2021 12:00:00 GMT", + } + + async def run_web_response_benchmark() -> None: + for _ in range(response_count): + web.Response(headers=headers) + + @benchmark + def _run() -> None: + loop.run_until_complete(run_web_response_benchmark()) + + +def test_web_response_with_bytes_body( + loop: asyncio.AbstractEventLoop, + benchmark: BenchmarkFixture, +) -> None: + """Benchmark creating 100 web.Response with bytes.""" + response_count = 100 + + async def run_web_response_benchmark() -> None: + for _ in range(response_count): + web.Response(body=b"Hello, World!") + + @benchmark + def _run() -> None: + loop.run_until_complete(run_web_response_benchmark()) + + +def test_web_response_with_text_body( + loop: asyncio.AbstractEventLoop, + benchmark: BenchmarkFixture, +) -> None: + """Benchmark creating 100 web.Response with text.""" + response_count = 100 + + async def run_web_response_benchmark() -> None: + for _ in range(response_count): + web.Response(text="Hello, World!") + + @benchmark + def _run() -> None: + loop.run_until_complete(run_web_response_benchmark()) + + +def test_simple_web_stream_response( + loop: asyncio.AbstractEventLoop, + benchmark: BenchmarkFixture, +) -> None: + """Benchmark creating 100 simple web.StreamResponse.""" + response_count = 100 + + async def run_web_response_benchmark() -> None: + for _ in range(response_count): + web.StreamResponse() + + @benchmark + def _run() -> None: + loop.run_until_complete(run_web_response_benchmark()) From 4c1a2fdf1227c6620f773bc5ad07140bb6d4faf7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 17 Nov 2024 12:33:21 -0600 Subject: [PATCH 2/3] Add benchmarks for creating web responses --- tests/test_benchmarks_web_response.py | 35 ++++++++------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/tests/test_benchmarks_web_response.py b/tests/test_benchmarks_web_response.py index fab628485d1..f864d1913da 100644 --- a/tests/test_benchmarks_web_response.py +++ b/tests/test_benchmarks_web_response.py @@ -14,13 +14,10 @@ def test_simple_web_response( """Benchmark creating 100 simple web.Response.""" response_count = 100 - async def run_web_response_benchmark() -> None: - for _ in range(response_count): - web.Response() - @benchmark def _run() -> None: - loop.run_until_complete(run_web_response_benchmark()) + for _ in range(response_count): + web.Response() def test_web_response_with_headers( @@ -35,13 +32,10 @@ def test_web_response_with_headers( "Date": "Sun, 01 Aug 2021 12:00:00 GMT", } - async def run_web_response_benchmark() -> None: - for _ in range(response_count): - web.Response(headers=headers) - @benchmark def _run() -> None: - loop.run_until_complete(run_web_response_benchmark()) + for _ in range(response_count): + web.Response(headers=headers) def test_web_response_with_bytes_body( @@ -51,13 +45,10 @@ def test_web_response_with_bytes_body( """Benchmark creating 100 web.Response with bytes.""" response_count = 100 - async def run_web_response_benchmark() -> None: - for _ in range(response_count): - web.Response(body=b"Hello, World!") - @benchmark def _run() -> None: - loop.run_until_complete(run_web_response_benchmark()) + for _ in range(response_count): + web.Response(body=b"Hello, World!") def test_web_response_with_text_body( @@ -67,13 +58,10 @@ def test_web_response_with_text_body( """Benchmark creating 100 web.Response with text.""" response_count = 100 - async def run_web_response_benchmark() -> None: - for _ in range(response_count): - web.Response(text="Hello, World!") - @benchmark def _run() -> None: - loop.run_until_complete(run_web_response_benchmark()) + for _ in range(response_count): + web.Response(text="Hello, World!") def test_simple_web_stream_response( @@ -83,10 +71,7 @@ def test_simple_web_stream_response( """Benchmark creating 100 simple web.StreamResponse.""" response_count = 100 - async def run_web_response_benchmark() -> None: - for _ in range(response_count): - web.StreamResponse() - @benchmark def _run() -> None: - loop.run_until_complete(run_web_response_benchmark()) + for _ in range(response_count): + web.StreamResponse() From 2b7baca948ef5772506080c05fc51e1f28d98a42 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 17 Nov 2024 12:42:42 -0600 Subject: [PATCH 3/3] preen, no requirement to be made in loop currently --- tests/test_benchmarks_web_response.py | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/tests/test_benchmarks_web_response.py b/tests/test_benchmarks_web_response.py index f864d1913da..fbf1fadf1e1 100644 --- a/tests/test_benchmarks_web_response.py +++ b/tests/test_benchmarks_web_response.py @@ -1,16 +1,11 @@ """codspeed benchmarks for the web responses.""" -import asyncio - from pytest_codspeed import BenchmarkFixture from aiohttp import web -def test_simple_web_response( - loop: asyncio.AbstractEventLoop, - benchmark: BenchmarkFixture, -) -> None: +def test_simple_web_response(benchmark: BenchmarkFixture) -> None: """Benchmark creating 100 simple web.Response.""" response_count = 100 @@ -20,10 +15,7 @@ def _run() -> None: web.Response() -def test_web_response_with_headers( - loop: asyncio.AbstractEventLoop, - benchmark: BenchmarkFixture, -) -> None: +def test_web_response_with_headers(benchmark: BenchmarkFixture) -> None: """Benchmark creating 100 web.Response with headers.""" response_count = 100 headers = { @@ -39,7 +31,6 @@ def _run() -> None: def test_web_response_with_bytes_body( - loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture, ) -> None: """Benchmark creating 100 web.Response with bytes.""" @@ -51,10 +42,7 @@ def _run() -> None: web.Response(body=b"Hello, World!") -def test_web_response_with_text_body( - loop: asyncio.AbstractEventLoop, - benchmark: BenchmarkFixture, -) -> None: +def test_web_response_with_text_body(benchmark: BenchmarkFixture) -> None: """Benchmark creating 100 web.Response with text.""" response_count = 100 @@ -64,10 +52,7 @@ def _run() -> None: web.Response(text="Hello, World!") -def test_simple_web_stream_response( - loop: asyncio.AbstractEventLoop, - benchmark: BenchmarkFixture, -) -> None: +def test_simple_web_stream_response(benchmark: BenchmarkFixture) -> None: """Benchmark creating 100 simple web.StreamResponse.""" response_count = 100