How is the fastest and efficient way to profile Rust backend API?

Does anyone know how to profile Rust backend API efficiently? Like I want to know which function takes most long time to proccess. I know Flamegraph but it show CPU related thing in trying, maybe I dont know how to use it yet :< Seeking for guide or recommendation article if there is any

I'm assuming you want to profile async code. The usual thing to do for that is structural logging. If you are using tokio, the tokio-console project can be helpful, but it is certainly not enough.

For structural logging, there are two main choices (AFAIK), and they have different tradeoffs.

  1. tracing can give you hierarchical (context-aware) wall-clock timing information for async function calls. It is the most popular option and has the largest share of the ecosystem.

  2. fastrace is an implementation of the same idea with performance-sensitivity in mind (e.g. you could potentially enable it in a production setting). It is much newer and optimizes performance over compatibility with the rest of the ecosystem (and thus lacks features).

1 Like

APIs are probably spending most of their time waiting for io, which requires good instrumentation to diagnose well (the tracing links above) but if you do have a lot of CPU time you'll want to look into how to use profiling tools, which isn't all that straightforward.

Profiling - The Rust Performance Book has a few links, but flamegraph is by far the easiest to start with.