4
4
5
5
from pytest_codspeed import BenchmarkFixture
6
6
7
- from aiohttp import web
7
+ from aiohttp import hdrs , web
8
8
from aiohttp .pytest_plugin import AiohttpClient
9
9
10
10
@@ -33,7 +33,7 @@ def _run() -> None:
33
33
loop .run_until_complete (run_client_benchmark ())
34
34
35
35
36
- def test_one_hundred_get_requests_with_2048_payload (
36
+ def test_one_hundred_get_requests_with_2048_chunked_payload (
37
37
loop : asyncio .AbstractEventLoop ,
38
38
aiohttp_client : AiohttpClient ,
39
39
benchmark : BenchmarkFixture ,
@@ -43,7 +43,9 @@ def test_one_hundred_get_requests_with_2048_payload(
43
43
payload = b"a" * 2048
44
44
45
45
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
47
49
48
50
app = web .Application ()
49
51
app .router .add_route ("GET" , "/" , handler )
@@ -60,7 +62,7 @@ def _run() -> None:
60
62
loop .run_until_complete (run_client_benchmark ())
61
63
62
64
63
- def test_one_hundred_get_requests_with_32768_payload (
65
+ def test_one_hundred_get_requests_with_32768_chunked_payload (
64
66
loop : asyncio .AbstractEventLoop ,
65
67
aiohttp_client : AiohttpClient ,
66
68
benchmark : BenchmarkFixture ,
@@ -70,7 +72,9 @@ def test_one_hundred_get_requests_with_32768_payload(
70
72
payload = b"a" * 32768
71
73
72
74
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
74
78
75
79
app = web .Application ()
76
80
app .router .add_route ("GET" , "/" , handler )
@@ -87,7 +91,7 @@ def _run() -> None:
87
91
loop .run_until_complete (run_client_benchmark ())
88
92
89
93
90
- def test_one_hundred_get_requests_with_1mib_payload (
94
+ def test_one_hundred_get_requests_with_1mib_chunked_payload (
91
95
loop : asyncio .AbstractEventLoop ,
92
96
aiohttp_client : AiohttpClient ,
93
97
benchmark : BenchmarkFixture ,
@@ -97,7 +101,93 @@ def test_one_hundred_get_requests_with_1mib_payload(
97
101
payload = b"a" * 1024 ** 2
98
102
99
103
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 )
101
191
102
192
app = web .Application ()
103
193
app .router .add_route ("GET" , "/" , handler )
0 commit comments