Skip to content

Commit f95287c

Browse files
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.
1 parent 88e73f8 commit f95287c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

crates/bevy_diagnostic/src/diagnostic.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ impl Diagnostic {
293293
/// Clear the history of this diagnostic.
294294
pub fn clear_history(&mut self) {
295295
self.history.clear();
296+
self.sum = 0.0;
297+
self.ema = 0.0;
296298
}
297299
}
298300

@@ -420,3 +422,31 @@ impl RegisterDiagnostic for App {
420422
self
421423
}
422424
}
425+
426+
#[cfg(test)]
427+
mod tests {
428+
use super::*;
429+
430+
#[test]
431+
fn test_clear_history() {
432+
const MEASUREMENT: f64 = 20.0;
433+
434+
let mut diagnostic =
435+
Diagnostic::new(DiagnosticPath::new("test")).with_max_history_length(5);
436+
let mut now = Instant::now();
437+
438+
for _ in 0..3 {
439+
for _ in 0..5 {
440+
diagnostic.add_measurement(DiagnosticMeasurement {
441+
time: now,
442+
value: MEASUREMENT,
443+
});
444+
// Increase time to test smoothed average.
445+
now += Duration::from_secs(1);
446+
}
447+
assert!((diagnostic.average().unwrap() - MEASUREMENT).abs() < 0.1);
448+
assert!((diagnostic.smoothed().unwrap() - MEASUREMENT).abs() < 0.1);
449+
diagnostic.clear_history();
450+
}
451+
}
452+
}

0 commit comments

Comments
 (0)