Introduce a ZED_MEASUREMENTS env var and use it to measure frame time

This commit is contained in:
Antonio Scandurra 2024-01-22 11:37:14 +01:00
parent 10ca33ce02
commit de64de22a3
3 changed files with 37 additions and 6 deletions

View file

@ -7,19 +7,21 @@ pub mod paths;
#[cfg(any(test, feature = "test-support"))]
pub mod test;
pub use backtrace::Backtrace;
use futures::Future;
use lazy_static::lazy_static;
use rand::{seq::SliceRandom, Rng};
use std::{
borrow::Cow,
cmp::{self, Ordering},
env,
ops::{AddAssign, Range, RangeInclusive},
panic::Location,
pin::Pin,
task::{Context, Poll},
time::Instant,
};
pub use backtrace::Backtrace;
use futures::Future;
use rand::{seq::SliceRandom, Rng};
pub use take_until::*;
#[macro_export]
@ -133,6 +135,24 @@ pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut se
}
}
pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
lazy_static! {
pub static ref ZED_MEASUREMENTS: bool = env::var("ZED_MEASUREMENTS")
.map(|measurements| measurements == "1" || measurements == "true")
.unwrap_or(false);
}
if *ZED_MEASUREMENTS {
let start = Instant::now();
let result = f();
let elapsed = start.elapsed();
eprintln!("{}: {:?}", label, elapsed);
result
} else {
f()
}
}
pub trait ResultExt<E> {
type Ok;