created on 2020-08-08, edited on 2022-07-10

#computer-science #rust #performance

Profiling Rust on macOS

https://nnethercote.github.io/perf-book/profiling.html It has been some time since I've started to use Rust for my hobby projects. As you know when the projects get more complex, performance may become an issue. Here, I'm writing down the available options for profiling Rust programs on macOS for future reference but I'm sure most of the crates and tools are also usable in other operating systems.

Xcode

This is probably the only option you can find only on macOS. You can use Xcode to get a broad view of how your program behaves. You can see how much CPU time your program takes, how much memory it uses etc.

Go to "All Processes" on the top left of the screen and find your program. Press record and you should see the CPU time your program takes.

puffin

I'm very happy using puffin for profiling modul. Puffin is opensource is and code is available on github. It is very simple to add puffin to your project and there is also a egui plugin so that you can have a live flamegraph in your program.

The simplest integration is like this. First you need to set scopes on at start up.

puffin::set_scopes_on(true);

I'm using miniquad and egui in my program so rendering the window is as simple as adding just a line.

self.egui_mq.run(ctx, |egui_ctx| {
    draw_ui(&mut self.windows, egui_ctx, &mut self.modul);
    puffin_egui::profiler_window(egui_ctx); <-- this line only
});

And once per frame you need to call

fn update() {
    ...
    puffin::GlobalProfiler::lock().new_frame();
}

You can then profile the scope or the function using puffin macros and also assign a name for easier analysis.

puffin::profile_function!("function");

or

puffin::profile_scope!("scope");

pprof

pprof is a very useful tool for profiling your program's CPU footprint.

Using 'criterion'