question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Profiling calls to TestClient

See original GitHub issue

I’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:

Screen Shot 2019-10-23 at 6 35 23 AM

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:

  1. 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
  2. Use a different profiler that works with async (all I’ve found is django-silk)
  3. 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)
  4. 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:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tomchristiecommented, Oct 24, 2019

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/httpxprof

It 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!

0reactions
dmontagucommented, Nov 12, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing - FastAPI
Create a TestClient by passing your FastAPI application to it. ... If you want to call async functions in your tests apart from...
Read more >
How to login using the test_client and use app_context of that ...
I am writing pytests for testing the API calls. ... to test your application by exposing # the Werkzeug test Client and handling...
Read more >
Profiling and improving the runtime of a large pytest test suite
In this post I summarize the things I learned when trying to improve the runtime of the test suite for one of our...
Read more >
How to test client connections using the bptestbpcd command
Beginning with NetBackup 6.0, there is a new command to test connections to clients. This command is the bptestbpcd command.
Read more >
FastAPI Tips & Tricks: Testing a Database - DEV Community ‍ ‍
Let's configure it in a file called database.py : ... To solve our problem we'll make use of SQL Transactions. Essentially
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found