Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks for web middleware #9885

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tests/test_benchmarks_web_middleware.py
Original file line number Diff line number Diff line change
@@ -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_ten_web_middlewares(
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:
bdraco marked this conversation as resolved.
Show resolved Hide resolved
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())
Loading