Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling fastAPI endpoint with pyinstrument

I am trying to profile my fastapi endpoints with pyinstrument. After some searching online I see that starting from pyinstrument 4.0 there is async support.

When using the code snippet provided in the documentation of Pyinstrument:

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)

I see a nice callstack appear when calling an endpoint, however as soon as the actual (synchronous) code is run, I only see [await] in the stack trace...

enter image description here

Putting the Profiler inside the code that is being executed within the endpoints solves the issue, but I have many endpoints and would like a solution that is more generic.

like image 448
Erik Avatar asked Oct 15 '25 02:10

Erik


1 Answers

I had same problem, it was solved by adding async to endpoint function.

@app.get("/bad_profile")
def endpoint_bad_profiler():
    # some code
    return {"key":"val"}

@app.get("/good_profile")
async def endpoint_good_profiler():
    # some code
    return {"key":"val"}
like image 138
Motixa Avatar answered Oct 17 '25 15:10

Motixa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!