Skip to content

Commit

Permalink
Add benchmark for sending client requests (#9689)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 7, 2024
1 parent 274c54e commit 753460d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/test_benchmarks_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
from http.cookies import Morsel
from typing import Union

from pytest_codspeed import BenchmarkFixture
from yarl import URL
Expand Down Expand Up @@ -65,3 +66,46 @@ def _run() -> None:
chunked=None,
expect100=False,
)


def test_send_client_request_one_hundred(
loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture
) -> None:
url = URL("http://python.org")
req = ClientRequest("get", url, loop=loop)

class MockTransport(asyncio.Transport):
"""Mock transport for testing that do no real I/O."""

def is_closing(self) -> bool:
"""Swallow is_closing."""
return False

def write(self, data: Union[bytes, bytearray, memoryview]) -> None:
"""Swallow writes."""

class MockProtocol(asyncio.BaseProtocol):

def __init__(self) -> None:
self.transport = MockTransport()

async def _drain_helper(self) -> None:
"""Swallow drain."""

def start_timeout(self) -> None:
"""Swallow start_timeout."""

class MockConnection:
def __init__(self) -> None:
self.transport = None
self.protocol = MockProtocol()

conn = MockConnection()

async def send_requests() -> None:
for _ in range(100):
await req.send(conn) # type: ignore[arg-type]

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

0 comments on commit 753460d

Please sign in to comment.