Skip to content

Commit 97eae19

Browse files
authored
Add benchmark requests without session and alternating clients (#10848)
1 parent e4c06ae commit 97eae19

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

tests/test_benchmarks_client.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import pytest
66
from pytest_codspeed import BenchmarkFixture
7+
from yarl import URL
78

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
1011

1112

1213
def test_one_hundred_simple_get_requests(
@@ -34,6 +35,62 @@ def _run() -> None:
3435
loop.run_until_complete(run_client_benchmark())
3536

3637

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+
3794
def test_one_hundred_simple_get_requests_multiple_methods_route(
3895
loop: asyncio.AbstractEventLoop,
3996
aiohttp_client: AiohttpClient,

0 commit comments

Comments
 (0)