Skip to content
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

Merged
merged 9 commits into from Jun 22, 2022
Merged

Conversation

yezz123
Copy link
Owner

@yezz123 yezz123 commented Jun 20, 2022

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.

Pyinstrument is a Python profiler. A profiler is a tool to help you optimize your code - make it faster.

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.

from pyinstrument import Profiler


PROFILING = True  # Set this from a settings model

if PROFILING:
    @app.middleware("http")
    async def profile_request(request: Request, call_next):
        profiling = request.query_params.get("profile", False)
        if profiling:
            profiler = Profiler(interval=settings.profiling_interval, async_mode="enabled")
            profiler.start()
            await call_next(request)
            profiler.stop()
            return HTMLResponse(profiler.output_html())
        else:
            return await call_next(request)

To invoke, make any request to your application with the GET parameter profile=1 and it will print the HTML result from pyinstrument.

AuthX's Support

With AuthX the abstract of profiling is easy, it's just about calling the ProfilerMiddleware 's class and calling it in add_middleware(ProfilerMiddleware) func that FastAPI provides.

References:

TODO

  • Add Function of Exporting results locally.
  • Add Tests for testing the ProfilerMiddleware's class.
  • Add Documentation for this Part of Middleware's support
  • Add a Little example of how we can use it to Profile our FastAPI application

@yezz123 yezz123 added documentation Improvements or additions to documentation enhancement New feature or request labels Jun 20, 2022
@yezz123 yezz123 self-assigned this Jun 20, 2022
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 20, 2022

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.16%.

Quality metrics Before After Change
Complexity 0.00 ⭐ 0.00 ⭐ 0.00
Method Length 31.00 ⭐ 33.50 ⭐ 2.50 👎
Working memory 1.00 ⭐ 1.00 ⭐ 0.00
Quality 96.31% 96.15% -0.16% 👎
Other metrics Before After Change
Lines 32 34 2
Changed files Quality Before Quality After Quality Change
authx/init.py 92.62% ⭐ 92.31% ⭐ -0.31% 👎
authx/middleware/init.py 100.00% ⭐ 100.00% ⭐ 0.00%

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

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
Copy link

codecov bot commented Jun 20, 2022

Codecov Report

Merging #240 (65f08f8) into main (203cd10) will increase coverage by 0.20%.
The diff coverage is 94.73%.

@@            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     
Impacted Files Coverage Δ
tests/middleware/test_middleware.py 94.33% <94.33%> (ø)
authx/middleware/profiler.py 94.82% <94.82%> (ø)
authx/__init__.py 100.00% <100.00%> (ø)
authx/middleware/__init__.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 203cd10...65f08f8. Read the comment docs.

@WP-LKL
Copy link

WP-LKL commented Jun 20, 2022

Have you considered OpenTelemetry + Digma?

image

@yezz123
Copy link
Owner Author

yezz123 commented Jun 20, 2022

Have you considered OpenTelemetry + Digma?

image

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

@yezz123 yezz123 marked this pull request as ready for review June 21, 2022 01:07
@yezz123 yezz123 merged commit 1e404e7 into main Jun 22, 2022
@yezz123 yezz123 deleted the feat/profiler branch June 22, 2022 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants