/puffin

🐦 Simple instrumentation profiler for Rust 🦀

Primary LanguageRustApache License 2.0Apache-2.0

🐦 Puffin - The Friendly Little Profiler

Embark Embark Crates.io Docs dependency status Build Status

Puffin photo by Richard Bartz

Puffin is an instrumentation profiler written in Rust where you opt-in to profile parts of your code:

fn my_function() {
    puffin::profile_function!();
    ...
    if ... {
        puffin::profile_scope_data!("load_image", image_name);
        ...
    }
}

The Puffin macros write data to a thread-local data stream. When the outermost scope of a thread is closed, the data stream is sent to a global profiler collector. The scopes are pretty light-weight, costing around 100-200 nanoseconds.

You have to turn on the profiler before it captures any data with a call to puffin::set_scopes_on(true);. When the profiler is off the profiler scope macros only has an overhead of 1-2 ns (and some stack space);

Once per frame you need to call puffin::GlobalProfiler::lock().new_frame();.

UI

If you want to view a nice flamegraph of your profile data, you can use the crate puffin-imgui in this repo. It looks like this:

Puffin Flamegraph UI

A full example is something like this:

fn main() {
    puffin::set_scopes_on(true); // you may want to control this with a flag
    let mut puffin_ui = puffin_imgui::ProfilerUi::default();

    // game loop
    loop {
        puffin::GlobalProfiler::lock().new_frame();

        {
            puffin::profile_scope!("slow_code");
            slow_code();
        }

        puffin_ui.window(ui);
    }
}

Other

Also check out the crate profiling which provides a unifying layer of abstraction on top of puffin and other profiling crates. It also has a working example of puffin-imgui.

Contributing

Contributor Covenant

We welcome community contributions to this project.

Please read our Contributor Guide for more information on how to get started.

License

Licensed under either of

at your option.

Puffin photo by Richard Bartz

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.