benchmark_counters/
measurement.rs1pub struct Measurement {
2 min: u128,
3 max: u128,
4 total: u128,
5 measurement_count: u128,
6}
7
8impl Measurement {
9 pub fn new() -> Self {
10 Self::default()
11 }
12
13 pub fn measuring_point(&mut self, p: u128) {
14 if self.measurement_count == 0 {
15 self.min = p;
16 self.max = p;
17 self.total = p;
18 } else {
19 self.min = self.min.min(p);
20 self.max = self.max.max(p);
21 self.total += p;
22 }
23 self.measurement_count += 1;
24 }
25
26 pub fn reset(&mut self) {
27 self.min = 0;
28 self.max = 0;
29 self.total = 0;
30 self.measurement_count = 0;
31 }
32
33 pub fn avg(&self) -> f32 {
34 if self.measurement_count == 0 {
35 return 0.0;
36 }
37 self.total as f32 / self.measurement_count as f32
38 }
39
40 pub fn min_max_avg(&self) -> (u128, u128, f32) {
41 (self.min, self.max, self.avg())
42 }
43}
44
45impl Default for Measurement {
46 fn default() -> Self {
47 Self::new()
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_empty() {
57 let m = Measurement::new();
58 assert_eq!((0, 0, 0.0), m.min_max_avg());
59 }
60
61 #[test]
62 fn test_single() {
63 let mut m = Measurement::new();
64 m.measuring_point(10);
65 assert_eq!((10, 10, 10.0), m.min_max_avg());
66 }
67
68 #[test]
69 fn test_double_with_reset() {
70 let mut m = Measurement::new();
71 m.measuring_point(10);
72 m.measuring_point(20);
73 assert_eq!((10, 20, 15.0), m.min_max_avg());
74 m.reset();
75 assert_eq!((0, 0, 0.0), m.min_max_avg());
76 }
77}