mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-03-06 10:00:39 +03:00
feat: add profiling middleware and update README for performance profiling
This commit is contained in:
0
app/middleware/__init__.py
Normal file
0
app/middleware/__init__.py
Normal file
28
app/middleware/profiler.py
Normal file
28
app/middleware/profiler.py
Normal 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)
|
||||
Reference in New Issue
Block a user