File: test_benchmarks_web_middleware.py

package info (click to toggle)
python-aiohttp 3.11.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,156 kB
  • sloc: python: 51,898; ansic: 20,843; makefile: 395; javascript: 31; sh: 3
file content (44 lines) | stat: -rw-r--r-- 1,188 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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())