summaryrefslogtreecommitdiff
path: root/client/src/interfaces/profiler.rs
blob: c6256b649204e2bbc25017bd8661e3b65302e0ed (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
    wearechat - generic multiplayer game with voip
    Copyright (C) 2025 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
use super::InterfaceData;
use egui::{Grid, Widget};
use std::{sync::Arc, time::Instant};

pub struct Profiler {
    pub idata: Arc<InterfaceData>,
}

impl Widget for &mut Profiler {
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        ui.collapsing("Scene", |ui| {
            self.idata.scene_prepare.ui(ui);
        });
        ui.collapsing("Download", |ui| {
            ui.add(&*self.idata.downloader);
        });
        ui.collapsing("Render", |ui| {
            ui.add(&*self.idata.render_timing.lock().unwrap());
        });
        ui.response()
    }
}

pub struct TimingProfiler {
    last_cp: Instant,
    cur_cp: &'static str,
    checkpoints: Vec<(&'static str, f32)>,
}

impl Default for TimingProfiler {
    fn default() -> Self {
        Self {
            last_cp: Instant::now(),
            checkpoints: Default::default(),
            cur_cp: "none",
        }
    }
}
impl TimingProfiler {
    pub fn begin(&mut self, name: &'static str) {
        self.checkpoints.clear();
        self.last_cp = Instant::now();
        self.cur_cp = name;
    }
    pub fn checkpoint(&mut self, name: &'static str) {
        let now = Instant::now();
        let dur = (now - self.last_cp).as_secs_f32();
        self.last_cp = now;
        self.checkpoints.push((self.cur_cp, dur));
        self.cur_cp = name;
    }
}
impl Widget for &TimingProfiler {
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        Grid::new("tp")
            .num_columns(2)
            .show(ui, |ui| {
                for (name, dur) in &self.checkpoints {
                    ui.label(*name);
                    ui.label(format!("{:.02}ms", dur * 1000.));
                    ui.end_row();
                }
            })
            .response
    }
}