44
55from pytest_codspeed import BenchmarkFixture
66
7- from aiohttp import web
7+ from aiohttp import hdrs , web
88from aiohttp .pytest_plugin import AiohttpClient
99
1010
@@ -33,7 +33,7 @@ def _run() -> None:
3333 loop .run_until_complete (run_client_benchmark ())
3434
3535
36- def test_one_hundred_get_requests_with_2048_payload (
36+ def test_one_hundred_get_requests_with_2048_chunked_payload (
3737 loop : asyncio .AbstractEventLoop ,
3838 aiohttp_client : AiohttpClient ,
3939 benchmark : BenchmarkFixture ,
@@ -43,7 +43,9 @@ def test_one_hundred_get_requests_with_2048_payload(
4343 payload = b"a" * 2048
4444
4545 async def handler (request : web .Request ) -> web .Response :
46- return web .Response (body = payload )
46+ resp = web .Response (body = payload )
47+ resp .enable_chunked_encoding ()
48+ return resp
4749
4850 app = web .Application ()
4951 app .router .add_route ("GET" , "/" , handler )
@@ -60,7 +62,7 @@ def _run() -> None:
6062 loop .run_until_complete (run_client_benchmark ())
6163
6264
63- def test_one_hundred_get_requests_with_32768_payload (
65+ def test_one_hundred_get_requests_with_32768_chunked_payload (
6466 loop : asyncio .AbstractEventLoop ,
6567 aiohttp_client : AiohttpClient ,
6668 benchmark : BenchmarkFixture ,
@@ -70,7 +72,9 @@ def test_one_hundred_get_requests_with_32768_payload(
7072 payload = b"a" * 32768
7173
7274 async def handler (request : web .Request ) -> web .Response :
73- return web .Response (body = payload )
75+ resp = web .Response (body = payload )
76+ resp .enable_chunked_encoding ()
77+ return resp
7478
7579 app = web .Application ()
7680 app .router .add_route ("GET" , "/" , handler )
@@ -87,7 +91,7 @@ def _run() -> None:
8791 loop .run_until_complete (run_client_benchmark ())
8892
8993
90- def test_one_hundred_get_requests_with_1mib_payload (
94+ def test_one_hundred_get_requests_with_1mib_chunked_payload (
9195 loop : asyncio .AbstractEventLoop ,
9296 aiohttp_client : AiohttpClient ,
9397 benchmark : BenchmarkFixture ,
@@ -97,7 +101,93 @@ def test_one_hundred_get_requests_with_1mib_payload(
97101 payload = b"a" * 1024 ** 2
98102
99103 async def handler (request : web .Request ) -> web .Response :
100- return web .Response (body = payload )
104+ resp = web .Response (body = payload )
105+ resp .enable_chunked_encoding ()
106+ return resp
107+
108+ app = web .Application ()
109+ app .router .add_route ("GET" , "/" , handler )
110+
111+ async def run_client_benchmark () -> None :
112+ client = await aiohttp_client (app )
113+ for _ in range (message_count ):
114+ resp = await client .get ("/" )
115+ await resp .read ()
116+ await client .close ()
117+
118+ @benchmark
119+ def _run () -> None :
120+ loop .run_until_complete (run_client_benchmark ())
121+
122+
123+ def test_one_hundred_get_requests_with_2048_content_length_payload (
124+ loop : asyncio .AbstractEventLoop ,
125+ aiohttp_client : AiohttpClient ,
126+ benchmark : BenchmarkFixture ,
127+ ) -> None :
128+ """Benchmark 100 GET requests with a small payload of 2048 bytes."""
129+ message_count = 100
130+ payload = b"a" * 2048
131+ headers = {hdrs .CONTENT_LENGTH : str (len (payload ))}
132+
133+ async def handler (request : web .Request ) -> web .Response :
134+ return web .Response (body = payload , headers = headers )
135+
136+ app = web .Application ()
137+ app .router .add_route ("GET" , "/" , handler )
138+
139+ async def run_client_benchmark () -> None :
140+ client = await aiohttp_client (app )
141+ for _ in range (message_count ):
142+ resp = await client .get ("/" )
143+ await resp .read ()
144+ await client .close ()
145+
146+ @benchmark
147+ def _run () -> None :
148+ loop .run_until_complete (run_client_benchmark ())
149+
150+
151+ def test_one_hundred_get_requests_with_32768_content_length_payload (
152+ loop : asyncio .AbstractEventLoop ,
153+ aiohttp_client : AiohttpClient ,
154+ benchmark : BenchmarkFixture ,
155+ ) -> None :
156+ """Benchmark 100 GET requests with a payload of 32768 bytes."""
157+ message_count = 100
158+ payload = b"a" * 32768
159+ headers = {hdrs .CONTENT_LENGTH : str (len (payload ))}
160+
161+ async def handler (request : web .Request ) -> web .Response :
162+ return web .Response (body = payload , headers = headers )
163+
164+ app = web .Application ()
165+ app .router .add_route ("GET" , "/" , handler )
166+
167+ async def run_client_benchmark () -> None :
168+ client = await aiohttp_client (app )
169+ for _ in range (message_count ):
170+ resp = await client .get ("/" )
171+ await resp .read ()
172+ await client .close ()
173+
174+ @benchmark
175+ def _run () -> None :
176+ loop .run_until_complete (run_client_benchmark ())
177+
178+
179+ def test_one_hundred_get_requests_with_1mib_content_length_payload (
180+ loop : asyncio .AbstractEventLoop ,
181+ aiohttp_client : AiohttpClient ,
182+ benchmark : BenchmarkFixture ,
183+ ) -> None :
184+ """Benchmark 100 GET requests with a payload of 1MiB bytes."""
185+ message_count = 100
186+ payload = b"a" * 1024 ** 2
187+ headers = {hdrs .CONTENT_LENGTH : str (len (payload ))}
188+
189+ async def handler (request : web .Request ) -> web .Response :
190+ return web .Response (body = payload , headers = headers )
101191
102192 app = web .Application ()
103193 app .router .add_route ("GET" , "/" , handler )
0 commit comments