|
4 | 4 |
|
5 | 5 | import pytest
|
6 | 6 | from pytest_codspeed import BenchmarkFixture
|
| 7 | +from yarl import URL |
7 | 8 |
|
8 |
| -from aiohttp import hdrs, web |
9 |
| -from aiohttp.pytest_plugin import AiohttpClient |
| 9 | +from aiohttp import hdrs, request, web |
| 10 | +from aiohttp.pytest_plugin import AiohttpClient, AiohttpServer |
10 | 11 |
|
11 | 12 |
|
12 | 13 | def test_one_hundred_simple_get_requests(
|
@@ -34,6 +35,62 @@ def _run() -> None:
|
34 | 35 | loop.run_until_complete(run_client_benchmark())
|
35 | 36 |
|
36 | 37 |
|
| 38 | +def test_one_hundred_simple_get_requests_alternating_clients( |
| 39 | + loop: asyncio.AbstractEventLoop, |
| 40 | + aiohttp_client: AiohttpClient, |
| 41 | + benchmark: BenchmarkFixture, |
| 42 | +) -> None: |
| 43 | + """Benchmark 100 simple GET requests with alternating clients.""" |
| 44 | + message_count = 100 |
| 45 | + |
| 46 | + async def handler(request: web.Request) -> web.Response: |
| 47 | + return web.Response() |
| 48 | + |
| 49 | + app = web.Application() |
| 50 | + app.router.add_route("GET", "/", handler) |
| 51 | + |
| 52 | + async def run_client_benchmark() -> None: |
| 53 | + client1 = await aiohttp_client(app) |
| 54 | + client2 = await aiohttp_client(app) |
| 55 | + for i in range(message_count): |
| 56 | + if i % 2 == 0: |
| 57 | + await client1.get("/") |
| 58 | + else: |
| 59 | + await client2.get("/") |
| 60 | + await client1.close() |
| 61 | + await client2.close() |
| 62 | + |
| 63 | + @benchmark |
| 64 | + def _run() -> None: |
| 65 | + loop.run_until_complete(run_client_benchmark()) |
| 66 | + |
| 67 | + |
| 68 | +def test_one_hundred_simple_get_requests_no_session( |
| 69 | + loop: asyncio.AbstractEventLoop, |
| 70 | + aiohttp_server: AiohttpServer, |
| 71 | + benchmark: BenchmarkFixture, |
| 72 | +) -> None: |
| 73 | + """Benchmark 100 simple GET requests without a session.""" |
| 74 | + message_count = 100 |
| 75 | + |
| 76 | + async def handler(request: web.Request) -> web.Response: |
| 77 | + return web.Response() |
| 78 | + |
| 79 | + app = web.Application() |
| 80 | + app.router.add_route("GET", "/", handler) |
| 81 | + server = loop.run_until_complete(aiohttp_server(app)) |
| 82 | + url = URL(f"http://{server.host}:{server.port}/") |
| 83 | + |
| 84 | + async def run_client_benchmark() -> None: |
| 85 | + for _ in range(message_count): |
| 86 | + async with request("GET", url): |
| 87 | + pass |
| 88 | + |
| 89 | + @benchmark |
| 90 | + def _run() -> None: |
| 91 | + loop.run_until_complete(run_client_benchmark()) |
| 92 | + |
| 93 | + |
37 | 94 | def test_one_hundred_simple_get_requests_multiple_methods_route(
|
38 | 95 | loop: asyncio.AbstractEventLoop,
|
39 | 96 | aiohttp_client: AiohttpClient,
|
|
0 commit comments