New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
👷 Support Profiling for checking service performance #240
Conversation
Sourcery Code Quality Report❌ Merging this PR will decrease code quality in the affected files by 0.16%.
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 #240 +/- ##
==========================================
+ Coverage 91.20% 91.41% +0.20%
==========================================
Files 54 56 +2
Lines 1785 1898 +113
==========================================
+ Hits 1628 1735 +107
- Misses 157 163 +6
Continue to review full report at Codecov.
|
Hmmm, looks like really good tools to support, I'm gonna be involved in checking how they gonna fit with a FastAPI application, and give you feedback! thank you @WP-LKL |
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 the app 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=1
and 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.References:
TODO
ProfilerMiddleware
's class.