31 releases (17 breaking)

0.19.0 Jan 17, 2024
0.18.1 Dec 11, 2023
0.18.0 Nov 21, 2023
0.16.0 May 24, 2023
0.3.1 Nov 9, 2020

#6 in Profiling

Download history 7268/week @ 2024-01-01 9687/week @ 2024-01-08 10600/week @ 2024-01-15 11837/week @ 2024-01-22 10879/week @ 2024-01-29 12295/week @ 2024-02-05 10782/week @ 2024-02-12 10405/week @ 2024-02-19 11306/week @ 2024-02-26 11759/week @ 2024-03-04 18021/week @ 2024-03-11 17005/week @ 2024-03-18 16949/week @ 2024-03-25 12815/week @ 2024-04-01 13575/week @ 2024-04-08 14393/week @ 2024-04-15

58,815 downloads per month
Used in 134 crates (41 directly)

MIT/Apache

110KB
2K SLoC

🐦 puffin

The friendly little instrumentation profiler for Rust

Puffin photo by Richard Bartz

(puffin photo by Richard Bartz)

Embark Embark Crates.io Docs dependency status Build Status

How to use

fn my_function() {
    puffin::profile_function!();
    ...
    if ... {
        puffin::profile_scope!("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 60 ns on an M1 MacBook Pro.

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 ns on an M1 MacBook Pro (plus some stack space).

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

Puffin Flamegraph using puffin_egui

Remote profiling

You can use puffin_http to send profile events over TCP to puffin_viewer. This is as easy as:

fn main() {
    let server_addr = format!("127.0.0.1:{}", puffin_http::DEFAULT_PORT);
    let _puffin_server = puffin_http::Server::new(&server_addr).unwrap();
    eprintln!("Run this to view profiling data:  puffin_viewer {server_addr}");
    puffin::set_scopes_on(true);// You also need to periodically call
    // `puffin::GlobalProfiler::lock().new_frame();`
    // to flush the profiling events.
}

egui integration

To view the profile data in-game you can use puffin_egui.

If you are using eframe you can look at this example.

Other

Also check out the crate profiling which provides a unifying layer of abstraction on top of puffin and other profiling crates.

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.

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.

Dependencies

~0.6–7.5MB
~33K SLoC