-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…9889) Co-authored-by: J. Nick Koston <nick@koston.org>
- Loading branch information
1 parent
581390c
commit 00978de
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""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: | ||
@web.middleware | ||
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()) |