radiate_core/stats/
time_statistic.rs1use crate::Statistic;
2use std::time::Duration;
3
4#[derive(Clone, PartialEq, Default)]
5pub struct TimeStatistic {
6 pub statistic: Statistic,
7 pub last_time: Duration,
8}
9
10impl TimeStatistic {
11 pub fn new(initial_val: Duration) -> Self {
12 let mut result = TimeStatistic::default();
13 result.add(initial_val);
14 result
15 }
16
17 pub fn add(&mut self, value: Duration) {
18 self.statistic.add(value.as_secs_f32());
19 self.last_time = value;
20 }
21
22 pub fn last_time(&self) -> Duration {
23 self.last_time
24 }
25
26 pub fn count(&self) -> i32 {
27 self.statistic.count()
28 }
29
30 pub fn mean(&self) -> Duration {
31 Duration::from_secs_f32(self.statistic.mean())
32 }
33
34 pub fn variance(&self) -> Duration {
35 Duration::from_secs_f32(self.statistic.variance())
36 }
37
38 pub fn standard_deviation(&self) -> Duration {
39 Duration::from_secs_f32(self.statistic.std_dev())
40 }
41
42 pub fn min(&self) -> Duration {
43 Duration::from_secs_f32(self.statistic.min())
44 }
45
46 pub fn max(&self) -> Duration {
47 Duration::from_secs_f32(self.statistic.max())
48 }
49
50 pub fn sum(&self) -> Duration {
51 Duration::from_secs_f32(self.statistic.sum())
52 }
53
54 pub fn clear(&mut self) {
55 self.statistic.clear();
56 }
57}
58
59impl From<Duration> for TimeStatistic {
60 fn from(value: Duration) -> Self {
61 TimeStatistic::new(value)
62 }
63}