[QUESTION] Profiling calls to TestClient
See original GitHub issueI’m having trouble profiling my FastAPI app when I use starlette.testclient.TestClient
. The issue is that I get no visibility beyond the asyncronous calls to the test client (<method 'control' of 'select.kqueue' objects>
), see attached image.
This seems like a generic problem with profiling async applications, but maybe the creators of the best async framework have some advice?
My typical workflow for profiling has been to make a test that’s slow and then run it with pytest --profile-svg
which generates something like this:
What I’m used to with Flask/WSGI is I would be able to “see” into the API call and find how long the constituent parts took. My thoughts on possible solutions so far are:
- Somehow run my app as WSGI for testing or in general. I’m not using FastAPI for async, I’m using it for ergonomics so losing async would not be a problem at this time
- Use a different profiler that works with async (all I’ve found is django-silk)
- Set profiling hooks inside of my API calls (not very attractive because it would be more work and potentially I’d have to check many different calls in one test to solve a slow test)
- Maybe use a line profiler to find which API calls in my test are slow
How do you profile your Starlette/FastAPI/async applications? Is there anything I can do to make my existing workflow work, or another workflow I can adopt?
I love Starlette and FastAPI and am grateful for your work and any advice you might have.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Maybe have a dig into what the
httpx
team have used, and see if there’s anything useful there?.. https://github.com/encode/httpx/tree/master/tools/httpxprofIt might also be worth trying
httpx.AsyncClient(app=app)
and seeing if you get more visibility when using that as a test client instead of Starlette’s existing test client.Would love to hear more on this thread about any progress you make!
Here’s a couple (related) issues that might be interesting to anyone following this thread:
https://github.com/tiangolo/fastapi/issues/701 https://github.com/sumerc/yappi/issues/21
I think the approach discussed in the FastAPI issue above is pretty close to being able to power an ASGI profiling middleware; the main outstanding issue is getting yappi to track the wall time of asyncio calls properly.
The creator of yappi thinks it might be possible to follow a similar approach to what was used to build the Greenlet profiler; I’ll update here if we’re able to make progress.