summaryrefslogtreecommitdiff
path: root/engine/src/performance.rs
blob: ffc5c27dfde3525f6802271f6505dfb0ed35f525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::time::Instant;

use ecs::component::local::Local;
use ecs::system::{Into, System};
use ecs::Component;

use crate::event::PostPresent as PostPresentEvent;

#[derive(Debug, Default)]
#[non_exhaustive]
pub struct Extension {}

impl ecs::extension::Extension for Extension
{
    fn collect(self, mut collector: ecs::extension::Collector<'_>)
    {
        collector.add_system(
            PostPresentEvent,
            log_perf.into_system().initialize((State::default(),)),
        );
    }
}

#[cfg(feature = "debug")]
macro_rules! log_perf {
    ($($tt: tt)*) => {
        tracing::info!($($tt)*);
    };
}

#[cfg(not(feature = "debug"))]
macro_rules! log_perf {
    ($($tt: tt)*) => {
        println!($($tt)*);
    };
}

fn log_perf(mut state: Local<State>)
{
    let Some(last_time) = state.last_time else {
        state.last_time = Some(Instant::now());
        return;
    };

    let time_now = Instant::now();

    state.last_time = Some(time_now);

    log_perf!(
        "Frame time: {}us",
        time_now.duration_since(last_time).as_micros()
    );
}

#[derive(Debug, Default, Component)]
struct State
{
    last_time: Option<Instant>,
}