mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-04-03 21:40:44 +02:00
This commit is contained in:
parent
4e8d58d793
commit
b0fbe3c173
18 changed files with 1724 additions and 1808 deletions
63
src/time.rs
Normal file
63
src/time.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use crate::*;
|
||||
|
||||
/// Performance counter
|
||||
#[derive(Debug)]
|
||||
pub struct PerfModel {
|
||||
pub clock: quanta::Clock,
|
||||
/// Measurement has a small cost. Disable it here.
|
||||
pub enabled: bool,
|
||||
// In nanoseconds. Time used by last iteration.
|
||||
pub used: AtomicF64,
|
||||
// In microseconds. Max prescribed time for iteration (frame, chunk...).
|
||||
pub window: AtomicF64,
|
||||
}
|
||||
|
||||
impl_default!(PerfModel: Self {
|
||||
enabled: true,
|
||||
clock: quanta::Clock::new(),
|
||||
used: Default::default(),
|
||||
window: Default::default(),
|
||||
});
|
||||
|
||||
impl PerfModel {
|
||||
pub fn get_t0 (&self) -> Option<u64> {
|
||||
if self.enabled {
|
||||
Some(self.clock.raw())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub fn get_t1 (&self, t0: Option<u64>) -> Option<std::time::Duration> {
|
||||
if let Some(t0) = t0 {
|
||||
if self.enabled {
|
||||
Some(self.clock.delta(t0, self.clock.raw()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub fn update (&self, t0: Option<u64>, microseconds: f64) {
|
||||
if let Some(t0) = t0 {
|
||||
let t1 = self.clock.raw();
|
||||
self.used.store(self.clock.delta_as_nanos(t0, t1) as f64, Relaxed);
|
||||
self.window.store(microseconds, Relaxed,);
|
||||
}
|
||||
}
|
||||
pub fn percentage (&self) -> Option<f64> {
|
||||
let window = self.window.load(Relaxed) * 1000.0;
|
||||
if window > 0.0 {
|
||||
let used = self.used.load(Relaxed);
|
||||
Some(100.0 * used / window)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub fn cycle <F: Fn(&Self)->T, T> (&self, call: &F) -> T {
|
||||
let t0 = self.get_t0();
|
||||
let result = call(self);
|
||||
let _t1 = self.get_t1(t0).unwrap();
|
||||
result
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue