5 releases

0.1.0 Aug 19, 2021
0.0.3 Jul 18, 2018
0.0.2 Jul 16, 2018
0.0.1 Mar 10, 2018
0.0.0 Mar 10, 2018

#229 in Profiling

MIT license

14KB
191 lines

A library for profiling frame-based games.

After recording a profile, you can view it in the WhatTheFrame GUI.

Activating Profiling

By default, [Profiler::new_frame], [Profiler::profile_task], and [Profiler::end_profiling] will compile to no-ops.

To enable profiling, turn on the profile feature. You probably want to configure your game's Cargo.toml like so:

[features]
profile = ["wtf/profile"]
[dependencies]
wtf = "1.0"

And then run your game like so:

cargo run --features profile

API

The API consists of 4 functions:

  • [read_profile_data] - Used to read a .wtf profile.
  • [Profiler::new_frame] - Call at the start of your frame
  • [Profiler::profile_task] - Call at the top of each scope you want to profile
  • [Profiler::end_profiling] - Call once at the end of your game

Note that you must assign [Profiler::new_frame] and [Profiler::profile_task] to a variable (not _) like so:

let _profile = Profiler::new_frame();
let _profile = Profiler::new_task("foo");

Example with winit:

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use winit::event::Event;
use wtf::Profiler;

fn main() {
    let mut profiler_frame = None;

    let thread_should_quit = Arc::new(AtomicBool::new(false));
    let thread = thread::spawn({
        let thread_should_quit = Arc::clone(&thread_should_quit);

        move || {
            while !thread_should_quit.load(Ordering::SeqCst) {
                {
                    let _profile = Profiler::profile_task("thread_part_1");
                    thread_task_1();
                    thread_task_2();
                    thread_task_3();
                }

                {
                    let _profile = Profiler::profile_task("thread_part_2");
                    thread_task_4();
                    thread_task_5();
                }
            }
        }
    });

    event_loop.run(move |event, _, _| match &event {
        Event::NewEvents(_) => {
            profiler_frame = Some(Profiler::new_frame());
        }

        Event::MainEventsCleared => {
            let record = Profiler::profile_task("update_game");
            update_state();
            update_positions();
            drop(record);

            window.request_redraw();
        }

        Event::RedrawEventsCleared => {
            let frame = profiler_frame.take();
            drop(frame);
        }

        Event::LoopDestroyed => {
            thread_should_quit.store(true, Ordering::SeqCst);
            thread.join().unwrap();

            Profiler::end_profiling();
        }

        _ => {}
    });
}

Dependencies

~0.7–1.7MB
~35K SLoC