Repository navigation
🔖 release 0.6.0 - Supporting Profiling - #255
Conversation
Sourcery Code Quality ReportMerging this PR leaves code quality unchanged.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
Codecov Report
@@ Coverage Diff @@
## main #255 +/- ##
=======================================
Coverage 91.47% 91.47%
=======================================
Files 57 57
Lines 1936 1936
=======================================
Hits 1771 1771
Misses 165 165
Continue to review full report at Codecov.
|
Idea
Profiling is a technique to figure out how time is spent in a program. With
these statistics, we can find the “hot spot” of a program and think about ways
of improvement. Sometimes, a hot spot in an unexpected location may also hint at
a bug in the program.
Profile a web request in FastAPI
To profile call stacks in FastAPI, you can write a middleware extension for
pyinstrument.Create an async function and decorate it with
app.middleware('http')where theapp is the name of your FastAPI application instance.
Make sure you configure a setting to only make this available when required.
To invoke, make any request to your application with the GET parameter
profile=1and it will print the HTML result frompyinstrument.AuthX's Support
With AuthX the abstract of profiling is easy, it's just about calling the
ProfilerMiddleware's class and calling it inadd_middleware(ProfilerMiddleware)func that FastAPI provides.Example
References