Skip to content

🔖 release 0.6.0 - Supporting Profiling - #255

Merged
yezz123 merged 2 commits into
mainfrom
release/0.6.0
Jul 23, 2022
Merged

yezz123 merged 2 commits into
mainfrom
release/0.6.0

Conversation

@yezz123

@yezz123 yezz123 commented Jul 23, 2022

Copy link
Copy Markdown
Owner

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.

Example

import os
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from authx import ProfilerMiddleware
app = FastAPI()
app.add_middleware(ProfilerMiddleware)
@app.get("/test")
async def normal_request():
    return JSONResponse({"retMsg": "Hello World!"})
if __name__ == '__main__':
    app_name = os.path.basename(__file__).replace(".py", "")
    uvicorn.run(app=f"{app_name}:app", host="0.0.0.0", port=8080, workers=1)

References

@yezz123 yezz123 self-assigned this Jul 23, 2022
@sourcery-ai

sourcery-ai Bot commented Jul 23, 2022 •

Copy link
Copy Markdown
Contributor

Sourcery Code Quality Report

Merging this PR leaves code quality unchanged.

Quality metrics Before After Change
Complexity 0.00 ⭐ 0.00 ⭐ 0.00
Method Length 31.67 ⭐ 31.67 ⭐ 0.00
Working memory 2.33 ⭐ 2.33 ⭐ 0.00
Quality 96.12% ⭐ 96.12% ⭐ 0.00%
Other metrics Before After Change
Lines 33 33 0
Changed files Quality Before Quality After Quality Change
authx/init.py 88.35% ⭐ 88.35% ⭐ 0.00%
tests/test_version.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

codecov Bot commented Jul 23, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #255 (a57d086) into main (1b949b1) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #255   +/-   ##
=======================================
  Coverage   91.47%   91.47%           
=======================================
  Files          57       57           
  Lines        1936     1936           
=======================================
  Hits         1771     1771           
  Misses        165      165           
Impacted Files Coverage Δ
authx/__init__.py 100.00% <100.00%> (ø)
tests/test_version.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 1b949b1...a57d086. Read the comment docs.

@yezz123
yezz123 merged commit 47a0af9 into main Jul 23, 2022
@yezz123
yezz123 deleted the release/0.6.0 branch July 23, 2022 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant