BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-base / src / measure.rs

measure.rs

eef6c0a4 History
measure.rs 1.2 KiB Download Raw
1use gpui::SharedString;
2
3#[inline]
4pub fn measurement_enabled() -> bool {
5 std::env::var("ZED_MEASUREMENTS").is_ok() || std::env::var("GPUI_MEASUREMENTS").is_ok()
6}
7
8/// Measures `f` when `if_` is true and measurement logging is enabled.
9#[inline]
10#[track_caller]
11pub fn measure_if(name: impl Into<SharedString>, if_: bool, f: impl FnOnce()) {
12 if if_ && measurement_enabled() {
13 let measure = Measure::new(name);
14 f();
15 measure.end();
16 } else {
17 f();
18 }
19}
20
21/// Measures `f` when measurement logging is enabled.
22#[inline]
23#[track_caller]
24pub fn measure(name: impl Into<SharedString>, f: impl FnOnce()) {
25 measure_if(name, true, f);
26}
27
28/// An elapsed-time measurement emitted through `tracing` when ended.
29pub struct Measure {
30 name: SharedString,
31 start: instant::Instant,
32}
33
34impl Measure {
35 #[track_caller]
36 pub fn new(name: impl Into<SharedString>) -> Self {
37 Self {
38 name: name.into(),
39 start: instant::Instant::now(),
40 }
41 }
42
43 #[track_caller]
44 pub fn end(self) {
45 let duration = self.start.elapsed();
46 tracing::trace!("{} in {:?}", self.name, duration);
47 }
48}