Diagnostic reset sum ema (#19337)
# Objective Fix incorrect average returned by `Diagnostic` after `clear_history` is called. ## Solution Reset sum and ema values in `Diagnostic::clear_history`. ## Testing I have added a cargo test for `Diagnostic::clear_history` that checks average and smoothed average. This test passes, and should not be platform dependent.
This commit is contained in:
parent
88e73f8eda
commit
f95287ca9b
@ -293,6 +293,8 @@ impl Diagnostic {
|
||||
/// Clear the history of this diagnostic.
|
||||
pub fn clear_history(&mut self) {
|
||||
self.history.clear();
|
||||
self.sum = 0.0;
|
||||
self.ema = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -420,3 +422,31 @@ impl RegisterDiagnostic for App {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_clear_history() {
|
||||
const MEASUREMENT: f64 = 20.0;
|
||||
|
||||
let mut diagnostic =
|
||||
Diagnostic::new(DiagnosticPath::new("test")).with_max_history_length(5);
|
||||
let mut now = Instant::now();
|
||||
|
||||
for _ in 0..3 {
|
||||
for _ in 0..5 {
|
||||
diagnostic.add_measurement(DiagnosticMeasurement {
|
||||
time: now,
|
||||
value: MEASUREMENT,
|
||||
});
|
||||
// Increase time to test smoothed average.
|
||||
now += Duration::from_secs(1);
|
||||
}
|
||||
assert!((diagnostic.average().unwrap() - MEASUREMENT).abs() < 0.1);
|
||||
assert!((diagnostic.smoothed().unwrap() - MEASUREMENT).abs() < 0.1);
|
||||
diagnostic.clear_history();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user