Skip to content

Profiling in FastAPI #576

Closed Answered by godlygeek
Partho asked this question in Q&A
Discussion options

You must be logged in to vote

Is there a way we can create a middleware where each API call will be introspected and a Memray stacktrace can be generated in HTML output (like Flamegraph), and shown in the browser?

Yes, absolutely - you can use the tracker API from a middleware to capture a Memray capture file. We don't have an API for programmatically producing a flame graph from that capture file, but you can easily use subprocess for that part, e.g.,

subprocess.check_call([sys.executable, "-m", "memray", "flamegraph", "-o", flamegraph_html_file_name, capture_file])

and then you can read that HTML and deliver it to your FastAPI client as a response.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Partho
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants