Skip to content

Commit 00978de

Browse files
[PR #9885/76b0d734 backport][3.11] Add benchmarks for web middleware (#9889)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 581390c commit 00978de

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""codspeed benchmarks for web middlewares."""
2+
3+
import asyncio
4+
5+
from pytest_codspeed import BenchmarkFixture
6+
7+
from aiohttp import web
8+
from aiohttp.pytest_plugin import AiohttpClient
9+
from aiohttp.typedefs import Handler
10+
11+
12+
def test_ten_web_middlewares(
13+
benchmark: BenchmarkFixture,
14+
loop: asyncio.AbstractEventLoop,
15+
aiohttp_client: AiohttpClient,
16+
) -> None:
17+
"""Benchmark 100 requests with 10 middlewares."""
18+
message_count = 100
19+
20+
async def handler(request: web.Request) -> web.Response:
21+
return web.Response()
22+
23+
app = web.Application()
24+
app.router.add_route("GET", "/", handler)
25+
26+
class MiddlewareClass:
27+
@web.middleware
28+
async def call(
29+
self, request: web.Request, handler: Handler
30+
) -> web.StreamResponse:
31+
return await handler(request)
32+
33+
for _ in range(10):
34+
app.middlewares.append(MiddlewareClass().call)
35+
36+
async def run_client_benchmark() -> None:
37+
client = await aiohttp_client(app)
38+
for _ in range(message_count):
39+
await client.get("/")
40+
await client.close()
41+
42+
@benchmark
43+
def _run() -> None:
44+
loop.run_until_complete(run_client_benchmark())

0 commit comments

Comments
 (0)