feat: add profiling middleware and update README for performance profiling

This commit is contained in:
grillazz
2026-02-05 11:46:45 +01:00
parent 59b673ed0e
commit 9353c4b3f8
8 changed files with 107 additions and 3 deletions

View File

@@ -0,0 +1,28 @@
from __future__ import annotations
from fastapi import Request
from pyinstrument import Profiler
from starlette.middleware.base import (
BaseHTTPMiddleware,
RequestResponseEndpoint,
)
from starlette.responses import HTMLResponse, Response
class ProfilingMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
if request.query_params.get("pyprofile") == "true":
profiler = Profiler(interval=0.001, async_mode="enabled")
profiler.start()
await call_next(request)
profiler.stop()
return HTMLResponse(
profiler.output_html(),
headers={"Content-Disposition": "attachment; filename=profile.html"},
)
return await call_next(request)