From 6063343b2d0857b5397aa4bd997f4c7dc333b3da Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 14 Nov 2024 14:54:03 -0600 Subject: [PATCH 1/2] Add benchmarks for web middleware --- tests/test_benchmarks_web_middleware.py | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/test_benchmarks_web_middleware.py diff --git a/tests/test_benchmarks_web_middleware.py b/tests/test_benchmarks_web_middleware.py new file mode 100644 index 00000000000..9635c9eb82f --- /dev/null +++ b/tests/test_benchmarks_web_middleware.py @@ -0,0 +1,43 @@ +"""codspeed benchmarks for web middlewares.""" + +import asyncio + +from pytest_codspeed import BenchmarkFixture + +from aiohttp import web +from aiohttp.pytest_plugin import AiohttpClient +from aiohttp.typedefs import Handler + + +def test_serialize_headers( + benchmark: BenchmarkFixture, + loop: asyncio.AbstractEventLoop, + aiohttp_client: AiohttpClient, +) -> None: + """Benchmark 100 requests with 10 middlewares.""" + message_count = 100 + + async def handler(request: web.Request) -> web.Response: + return web.Response() + + app = web.Application() + app.router.add_route("GET", "/", handler) + + class MiddlewareClass: + async def call( + self, request: web.Request, handler: Handler + ) -> web.StreamResponse: + return await handler(request) + + for _ in range(10): + app.middlewares.append(MiddlewareClass().call) + + async def run_client_benchmark() -> None: + client = await aiohttp_client(app) + for _ in range(message_count): + await client.get("/") + await client.close() + + @benchmark + def _run() -> None: + loop.run_until_complete(run_client_benchmark()) From 1935827dd2a3cf66d02b95fdd020f91cd7aa612a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 14 Nov 2024 14:59:25 -0600 Subject: [PATCH 2/2] Update tests/test_benchmarks_web_middleware.py --- tests/test_benchmarks_web_middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_benchmarks_web_middleware.py b/tests/test_benchmarks_web_middleware.py index 9635c9eb82f..497da1819c9 100644 --- a/tests/test_benchmarks_web_middleware.py +++ b/tests/test_benchmarks_web_middleware.py @@ -9,7 +9,7 @@ from aiohttp.typedefs import Handler -def test_serialize_headers( +def test_ten_web_middlewares( benchmark: BenchmarkFixture, loop: asyncio.AbstractEventLoop, aiohttp_client: AiohttpClient,