Skip to content

Add benchmark requests without session and alternating clients #10848

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

Merged
merged 3 commits into from
May 16, 2025
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
61 changes: 59 additions & 2 deletions tests/test_benchmarks_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import pytest
from pytest_codspeed import BenchmarkFixture
from yarl import URL

from aiohttp import hdrs, web
from aiohttp.pytest_plugin import AiohttpClient
from aiohttp import hdrs, request, web
from aiohttp.pytest_plugin import AiohttpClient, AiohttpServer


def test_one_hundred_simple_get_requests(
Expand Down Expand Up @@ -34,6 +35,62 @@ def _run() -> None:
loop.run_until_complete(run_client_benchmark())


def test_one_hundred_simple_get_requests_alternating_clients(
loop: asyncio.AbstractEventLoop,
aiohttp_client: AiohttpClient,
benchmark: BenchmarkFixture,
) -> None:
"""Benchmark 100 simple GET requests with alternating clients."""
message_count = 100

async def handler(request: web.Request) -> web.Response:
return web.Response()

app = web.Application()
app.router.add_route("GET", "/", handler)

async def run_client_benchmark() -> None:
client1 = await aiohttp_client(app)
client2 = await aiohttp_client(app)
for i in range(message_count):
if i % 2 == 0:
await client1.get("/")
else:
await client2.get("/")
await client1.close()
await client2.close()

@benchmark
def _run() -> None:
loop.run_until_complete(run_client_benchmark())


def test_one_hundred_simple_get_requests_no_session(
loop: asyncio.AbstractEventLoop,
aiohttp_server: AiohttpServer,
benchmark: BenchmarkFixture,
) -> None:
"""Benchmark 100 simple GET requests without a session."""
message_count = 100

async def handler(request: web.Request) -> web.Response:
return web.Response()

app = web.Application()
app.router.add_route("GET", "/", handler)
server = loop.run_until_complete(aiohttp_server(app))
url = URL(f"http://{server.host}:{server.port}/")

async def run_client_benchmark() -> None:
for _ in range(message_count):
async with request("GET", url):
pass

@benchmark
def _run() -> None:
loop.run_until_complete(run_client_benchmark())


def test_one_hundred_simple_get_requests_multiple_methods_route(
loop: asyncio.AbstractEventLoop,
aiohttp_client: AiohttpClient,
Expand Down
Loading