From 6cf4497c8d97671846abe3f7b3149468e2fb91e2 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 17 Nov 2024 19:24:12 +0000 Subject: [PATCH] [PR #9940/9ca1a581 backport][3.11] Add benchmarks for creating web responses (#9942) Co-authored-by: J. Nick Koston --- tests/test_benchmarks_web_response.py | 62 +++++++++++++++++++++++++++ 1 file changed, 62 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..fbf1fadf1e1 --- /dev/null +++ b/tests/test_benchmarks_web_response.py @@ -0,0 +1,62 @@ +"""codspeed benchmarks for the web responses.""" + +from pytest_codspeed import BenchmarkFixture + +from aiohttp import web + + +def test_simple_web_response(benchmark: BenchmarkFixture) -> None: + """Benchmark creating 100 simple web.Response.""" + response_count = 100 + + @benchmark + def _run() -> None: + for _ in range(response_count): + web.Response() + + +def test_web_response_with_headers(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", + } + + @benchmark + def _run() -> None: + for _ in range(response_count): + web.Response(headers=headers) + + +def test_web_response_with_bytes_body( + benchmark: BenchmarkFixture, +) -> None: + """Benchmark creating 100 web.Response with bytes.""" + response_count = 100 + + @benchmark + def _run() -> None: + for _ in range(response_count): + web.Response(body=b"Hello, World!") + + +def test_web_response_with_text_body(benchmark: BenchmarkFixture) -> None: + """Benchmark creating 100 web.Response with text.""" + response_count = 100 + + @benchmark + def _run() -> None: + for _ in range(response_count): + web.Response(text="Hello, World!") + + +def test_simple_web_stream_response(benchmark: BenchmarkFixture) -> None: + """Benchmark creating 100 simple web.StreamResponse.""" + response_count = 100 + + @benchmark + def _run() -> None: + for _ in range(response_count): + web.StreamResponse()