|
34 | 34 | <li><a href="#uv-knowledge-and-inspirations">UV knowledge and inspirations</a></li> |
35 | 35 | <li><a href="#large-language-model">Integration with local LLM</a></li> |
36 | 36 | <li><a href="#ha-sample-with-nginx-as-load-balancer">High Availability sample with nginx as load balancer</a></li> |
| 37 | + <li><a href="#performance-profiling-with-pyinstrument">Performance Profiling with Pyinstrument</a></li> |
37 | 38 | </ul> |
38 | 39 | </li> |
39 | 40 | <li><a href="#acknowledgments">Acknowledgments</a></li> |
@@ -193,6 +194,35 @@ make docker-up-ha |
193 | 194 | <p align="right">(<a href="#readme-top">back to top</a>)</p> |
194 | 195 |
|
195 | 196 |
|
| 197 | +### Performance Profiling with Pyinstrument |
| 198 | +To help identify performance bottlenecks and analyze request handling, this project integrates `pyinstrument` for on-demand profiling. |
| 199 | +The `ProfilingMiddleware` allows you to profile any endpoint by simply adding a query parameter to your request. |
| 200 | + |
| 201 | +When profiling is enabled for a request, `pyinstrument` will monitor the execution, and the server will respond with a detailed HTML report that you can download and view in your browser. |
| 202 | +This report provides a visual breakdown of where time is spent within your code. |
| 203 | + |
| 204 | +To enable profiling for an endpoint, you need to: |
| 205 | +1. Add a `pyprofile` query parameter to the endpoint's signature. This makes the functionality discoverable through the API documentation. |
| 206 | +2. Make a request to the endpoint with the query parameter `?pyprofile=true`. |
| 207 | + |
| 208 | +Here is an example from the `redis_check` health endpoint: |
| 209 | +```python |
| 210 | +from typing import Annotated |
| 211 | +from fastapi import Query |
| 212 | + |
| 213 | +@router.get("/redis", status_code=status.HTTP_200_OK) |
| 214 | +async def redis_check( |
| 215 | + request: Request, |
| 216 | + pyprofile: Annotated[ |
| 217 | + bool, Query(description="Enable profiler for this request") |
| 218 | + ] = False, |
| 219 | +): |
| 220 | + # ... endpoint logic |
| 221 | +``` |
| 222 | + |
| 223 | +<p align="right">(<a href="#readme-top">back to top</a>)</p> |
| 224 | + |
| 225 | + |
196 | 226 | ### UV knowledge and inspirations |
197 | 227 | - https://docs.astral.sh/uv/ |
198 | 228 | - https://hynek.me/articles/docker-uv/ |
@@ -226,7 +256,8 @@ I've included a few of my favorites to kick things off! |
226 | 256 | <details> |
227 | 257 | <summary>2026 (1 change)</summary> |
228 | 258 | <ul> |
229 | | - <li>[JAN 11 2026] refactor test fixture infrastructure to improve test isolation :test_tube:</li> |
| 259 | + <li>[FEB 5 2026] add profiler middleware :crystal_ball:</li> |
| 260 | + <li>[JAN 11 2026] refactor test fixture infrastructure to improve test isolation :test_tube:</li> |
230 | 261 | </ul> |
231 | 262 | </details> |
232 | 263 | <details> |
|
0 commit comments