Debian Codegraph @

tests/benchmarks/test_general_performance.py

raw
1 import json
2 import sys
3 from collections.abc import Iterator
4 from typing import Annotated, Any
5
6 import pytest
7 from fastapi import Depends, FastAPI
8 from fastapi.testclient import TestClient
9 from pydantic import BaseModel
10
11 if "--codspeed" not in sys.argv:
12 pytest.skip(
13 "Benchmark tests are skipped by default; run with --codspeed.",
14 allow_module_level=True,
15 )
16
17 LARGE_ITEMS: list[dict[str, Any]] = [
18 {
19 "id": i,
20 "name": f"item-{i}",
21 "values": list(range(25)),
22 "meta": {
23 "active": True,
24 "group": i % 10,
25 "tag": f"t{i % 5}",
26 },
27 }
28 for i in range(300)
29 ]
30
31 LARGE_METADATA: dict[str, Any] = {
32 "source": "benchmark",
33 "version": 1,
34 "flags": {"a": True, "b": False, "c": True},
35 "notes": ["x" * 50, "y" * 50, "z" * 50],
36 }
37
38 LARGE_PAYLOAD: dict[str, Any] = {"items": LARGE_ITEMS, "metadata": LARGE_METADATA}
39
40
41 def dep_a():
42 return 40
43
44
45 def dep_b(a: Annotated[int, Depends(dep_a)]):
46 return a + 2
47
48
49 class ItemIn(BaseModel):
50 name: str
51 value: int
52
53
54 class ItemOut(BaseModel):
55 name: str
56 value: int
57 dep: int
58
59
60 class LargeIn(BaseModel):
61 items: list[dict[str, Any]]
62 metadata: dict[str, Any]
63
64
65 class LargeOut(BaseModel):
66 items: list[dict[str, Any]]
67 metadata: dict[str, Any]
68
69
70 app = FastAPI()
71
72
73 @app.post("/sync/validated", response_model=ItemOut)
74 def sync_validated(item: ItemIn, dep: Annotated[int, Depends(dep_b)]):
75 return ItemOut(name=item.name, value=item.value, dep=dep)
76
77
78 @app.get("/sync/dict-no-response-model")
79 def sync_dict_no_response_model():
80 return {"name": "foo", "value": 123}
81
82
83 @app.get("/sync/dict-with-response-model", response_model=ItemOut)
84 def sync_dict_with_response_model(
85 dep: Annotated[int, Depends(dep_b)],
86 ):
87 return {"name": "foo", "value": 123, "dep": dep}
88
89
90 @app.get("/sync/model-no-response-model")
91 def sync_model_no_response_model(dep: Annotated[int, Depends(dep_b)]):
92 return ItemOut(name="foo", value=123, dep=dep)
93
94
95 @app.get("/sync/model-with-response-model", response_model=ItemOut)
96 def sync_model_with_response_model(dep: Annotated[int, Depends(dep_b)]):
97 return ItemOut(name="foo", value=123, dep=dep)
98
99
100 @app.post("/async/validated", response_model=ItemOut)
101 async def async_validated(
102 item: ItemIn,
103 dep: Annotated[int, Depends(dep_b)],
104 ):
105 return ItemOut(name=item.name, value=item.value, dep=dep)
106
107
108 @app.post("/sync/large-receive")
109 def sync_large_receive(payload: LargeIn):
110 return {"received": len(payload.items)}
111
112
113 @app.post("/async/large-receive")
114 async def async_large_receive(payload: LargeIn):
115 return {"received": len(payload.items)}
116
117
118 @app.get("/sync/large-dict-no-response-model")
119 def sync_large_dict_no_response_model():
120 return LARGE_PAYLOAD
121
122
123 @app.get("/sync/large-dict-with-response-model", response_model=LargeOut)
124 def sync_large_dict_with_response_model():
125 return LARGE_PAYLOAD
126
127
128 @app.get("/sync/large-model-no-response-model")
129 def sync_large_model_no_response_model():
130 return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA)
131
132
133 @app.get("/sync/large-model-with-response-model", response_model=LargeOut)
134 def sync_large_model_with_response_model():
135 return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA)
136
137
138 @app.get("/async/large-dict-no-response-model")
139 async def async_large_dict_no_response_model():
140 return LARGE_PAYLOAD
141
142
143 @app.get("/async/large-dict-with-response-model", response_model=LargeOut)
144 async def async_large_dict_with_response_model():
145 return LARGE_PAYLOAD
146
147
148 @app.get("/async/large-model-no-response-model")
149 async def async_large_model_no_response_model():
150 return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA)
151
152
153 @app.get("/async/large-model-with-response-model", response_model=LargeOut)
154 async def async_large_model_with_response_model():
155 return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA)
156
157
158 @app.get("/async/dict-no-response-model")
159 async def async_dict_no_response_model():
160 return {"name": "foo", "value": 123}
161
162
163 @app.get("/async/dict-with-response-model", response_model=ItemOut)
164 async def async_dict_with_response_model(
165 dep: Annotated[int, Depends(dep_b)],
166 ):
167 return {"name": "foo", "value": 123, "dep": dep}
168
169
170 @app.get("/async/model-no-response-model")
171 async def async_model_no_response_model(
172 dep: Annotated[int, Depends(dep_b)],
173 ):
174 return ItemOut(name="foo", value=123, dep=dep)
175
176
177 @app.get("/async/model-with-response-model", response_model=ItemOut)
178 async def async_model_with_response_model(
179 dep: Annotated[int, Depends(dep_b)],
180 ):
181 return ItemOut(name="foo", value=123, dep=dep)
182
183
184 @pytest.fixture(scope="module")
185 def client() -> Iterator[TestClient]:
186 with TestClient(app) as client:
187 yield client
188
189
190 def _bench_get(benchmark, client: TestClient, path: str) -> tuple[int, bytes]:
191 warmup = client.get(path)
192 assert warmup.status_code == 200
193
194 def do_request() -> tuple[int, bytes]:
195 response = client.get(path)
196 return response.status_code, response.content
197
198 return benchmark(do_request)
199
200
201 def _bench_post_json(
202 benchmark, client: TestClient, path: str, json: dict[str, Any]
203 ) -> tuple[int, bytes]:
204 warmup = client.post(path, json=json)
205 assert warmup.status_code == 200
206
207 def do_request() -> tuple[int, bytes]:
208 response = client.post(path, json=json)
209 return response.status_code, response.content
210
211 return benchmark(do_request)
212
213
214 def test_sync_receiving_validated_pydantic_model(benchmark, client: TestClient) -> None:
215 status_code, body = _bench_post_json(
216 benchmark,
217 client,
218 "/sync/validated",
219 json={"name": "foo", "value": 123},
220 )
221 assert status_code == 200
222 assert body == b'{"name":"foo","value":123,"dep":42}'
223
224
225 def test_sync_return_dict_without_response_model(benchmark, client: TestClient) -> None:
226 status_code, body = _bench_get(benchmark, client, "/sync/dict-no-response-model")
227 assert status_code == 200
228 assert body == b'{"name":"foo","value":123}'
229
230
231 def test_sync_return_dict_with_response_model(benchmark, client: TestClient) -> None:
232 status_code, body = _bench_get(benchmark, client, "/sync/dict-with-response-model")
233 assert status_code == 200
234 assert body == b'{"name":"foo","value":123,"dep":42}'
235
236
237 def test_sync_return_model_without_response_model(
238 benchmark, client: TestClient
239 ) -> None:
240 status_code, body = _bench_get(benchmark, client, "/sync/model-no-response-model")
241 assert status_code == 200
242 assert body == b'{"name":"foo","value":123,"dep":42}'
243
244
245 def test_sync_return_model_with_response_model(benchmark, client: TestClient) -> None:
246 status_code, body = _bench_get(benchmark, client, "/sync/model-with-response-model")
247 assert status_code == 200
248 assert body == b'{"name":"foo","value":123,"dep":42}'
249
250
251 def test_async_receiving_validated_pydantic_model(
252 benchmark, client: TestClient
253 ) -> None:
254 status_code, body = _bench_post_json(
255 benchmark, client, "/async/validated", json={"name": "foo", "value": 123}
256 )
257 assert status_code == 200
258 assert body == b'{"name":"foo","value":123,"dep":42}'
259
260
261 def test_async_return_dict_without_response_model(
262 benchmark, client: TestClient
263 ) -> None:
264 status_code, body = _bench_get(benchmark, client, "/async/dict-no-response-model")
265 assert status_code == 200
266 assert body == b'{"name":"foo","value":123}'
267
268
269 def test_async_return_dict_with_response_model(benchmark, client: TestClient) -> None:
270 status_code, body = _bench_get(benchmark, client, "/async/dict-with-response-model")
271 assert status_code == 200
272 assert body == b'{"name":"foo","value":123,"dep":42}'
273
274
275 def test_async_return_model_without_response_model(
276 benchmark, client: TestClient
277 ) -> None:
278 status_code, body = _bench_get(benchmark, client, "/async/model-no-response-model")
279 assert status_code == 200
280 assert body == b'{"name":"foo","value":123,"dep":42}'
281
282
283 def test_async_return_model_with_response_model(benchmark, client: TestClient) -> None:
284 status_code, body = _bench_get(
285 benchmark, client, "/async/model-with-response-model"
286 )
287 assert status_code == 200
288 assert body == b'{"name":"foo","value":123,"dep":42}'
289
290
291 def test_sync_receiving_large_payload(benchmark, client: TestClient) -> None:
292 status_code, body = _bench_post_json(
293 benchmark,
294 client,
295 "/sync/large-receive",
296 json=LARGE_PAYLOAD,
297 )
298 assert status_code == 200
299 assert body == b'{"received":300}'
300
301
302 def test_async_receiving_large_payload(benchmark, client: TestClient) -> None:
303 status_code, body = _bench_post_json(
304 benchmark,
305 client,
306 "/async/large-receive",
307 json=LARGE_PAYLOAD,
308 )
309 assert status_code == 200
310 assert body == b'{"received":300}'
311
312
313 def _expected_large_payload_json_bytes() -> bytes:
314 return json.dumps(
315 LARGE_PAYLOAD,
316 ensure_ascii=False,
317 allow_nan=False,
318 separators=(",", ":"),
319 ).encode("utf-8")
320
321
322 def test_sync_return_large_dict_without_response_model(
323 benchmark, client: TestClient
324 ) -> None:
325 status_code, body = _bench_get(
326 benchmark, client, "/sync/large-dict-no-response-model"
327 )
328 assert status_code == 200
329 assert body == _expected_large_payload_json_bytes()
330
331
332 def test_sync_return_large_dict_with_response_model(
333 benchmark, client: TestClient
334 ) -> None:
335 status_code, body = _bench_get(
336 benchmark, client, "/sync/large-dict-with-response-model"
337 )
338 assert status_code == 200
339 assert body == _expected_large_payload_json_bytes()
340
341
342 def test_sync_return_large_model_without_response_model(
343 benchmark, client: TestClient
344 ) -> None:
345 status_code, body = _bench_get(
346 benchmark, client, "/sync/large-model-no-response-model"
347 )
348 assert status_code == 200
349 assert body == _expected_large_payload_json_bytes()
350
351
352 def test_sync_return_large_model_with_response_model(
353 benchmark, client: TestClient
354 ) -> None:
355 status_code, body = _bench_get(
356 benchmark, client, "/sync/large-model-with-response-model"
357 )
358 assert status_code == 200
359 assert body == _expected_large_payload_json_bytes()
360
361
362 def test_async_return_large_dict_without_response_model(
363 benchmark, client: TestClient
364 ) -> None:
365 status_code, body = _bench_get(
366 benchmark, client, "/async/large-dict-no-response-model"
367 )
368 assert status_code == 200
369 assert body == _expected_large_payload_json_bytes()
370
371
372 def test_async_return_large_dict_with_response_model(
373 benchmark, client: TestClient
374 ) -> None:
375 status_code, body = _bench_get(
376 benchmark, client, "/async/large-dict-with-response-model"
377 )
378 assert status_code == 200
379 assert body == _expected_large_payload_json_bytes()
380
381
382 def test_async_return_large_model_without_response_model(
383 benchmark, client: TestClient
384 ) -> None:
385 status_code, body = _bench_get(
386 benchmark, client, "/async/large-model-no-response-model"
387 )
388 assert status_code == 200
389 assert body == _expected_large_payload_json_bytes()
390
391
392 def test_async_return_large_model_with_response_model(
393 benchmark, client: TestClient
394 ) -> None:
395 status_code, body = _bench_get(
396 benchmark, client, "/async/large-model-with-response-model"
397 )
398 assert status_code == 200
399 assert body == _expected_large_payload_json_bytes()