Skip to content

Commit f3a5ea2

Browse files
[PR #9689/753460da backport][3.11] Add benchmark for sending client requests (#9691)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 74d9355 commit f3a5ea2

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tests/test_benchmarks_client_request.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
from http.cookies import Morsel
5+
from typing import Union
56

67
from pytest_codspeed import BenchmarkFixture
78
from yarl import URL
@@ -65,3 +66,46 @@ def _run() -> None:
6566
chunked=None,
6667
expect100=False,
6768
)
69+
70+
71+
def test_send_client_request_one_hundred(
72+
loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture
73+
) -> None:
74+
url = URL("http://python.org")
75+
req = ClientRequest("get", url, loop=loop)
76+
77+
class MockTransport(asyncio.Transport):
78+
"""Mock transport for testing that do no real I/O."""
79+
80+
def is_closing(self) -> bool:
81+
"""Swallow is_closing."""
82+
return False
83+
84+
def write(self, data: Union[bytes, bytearray, memoryview]) -> None:
85+
"""Swallow writes."""
86+
87+
class MockProtocol(asyncio.BaseProtocol):
88+
89+
def __init__(self) -> None:
90+
self.transport = MockTransport()
91+
92+
async def _drain_helper(self) -> None:
93+
"""Swallow drain."""
94+
95+
def start_timeout(self) -> None:
96+
"""Swallow start_timeout."""
97+
98+
class MockConnection:
99+
def __init__(self) -> None:
100+
self.transport = None
101+
self.protocol = MockProtocol()
102+
103+
conn = MockConnection()
104+
105+
async def send_requests() -> None:
106+
for _ in range(100):
107+
await req.send(conn) # type: ignore[arg-type]
108+
109+
@benchmark
110+
def _run() -> None:
111+
loop.run_until_complete(send_requests())

0 commit comments

Comments
 (0)