5个版本

0.1.0 2021年8月19日
0.0.3 2018年7月18日
0.0.2 2018年7月16日
0.0.1 2018年3月10日
0.0.0 2018年3月10日

#161性能分析

MIT 许可证

14KB
191

一个用于性能分析基于帧的游戏的库。

记录性能分析后,您可以在WhatTheFrame GUI中查看它。

激活性能分析

默认情况下, [Profiler::new_frame], [Profiler::profile_task], 和 [Profiler::end_profiling] 将编译为无操作。

要启用性能分析,打开 profile 功能。你可能需要像这样配置你的游戏的 Cargo.toml

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

然后像这样运行你的游戏

cargorun --featuresprofile

API

API由4个函数组成

  • [read_profile_data] - 用于读取 .wtf 性能分析文件。
  • [Profiler::new_frame] - 在帧的开始调用
  • [Profiler::profile_task] - 在你想分析的每个作用域的顶部调用
  • [Profiler::end_profiling] - 在游戏结束时只调用一次

请注意,您必须将 [Profiler::new_frame] 和 [Profiler::profile_task] 分配给一个变量(而不是 _),如下所示

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

使用 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();
        }

        _ => {}
    });
}

依赖

~0.8–1.8MB
~37K SLoC