springql_core/stream_engine/autonomous_executor/performance_metrics/
calculation.rs1use std::{
4 cmp::max,
5 ops::{Add, Mul, Sub},
6};
7
8pub fn next_avg<T>(current_avg: T, current_n: u64, next_val: T) -> T
9where
10 T: Add<T, Output = T> + Sub<T, Output = T> + Mul<f32, Output = T> + Copy,
11{
12 current_avg + (next_val - current_avg) * (1.0 / ((current_n + 1) as f32))
13}
14
15pub fn floor0<T>(v: T) -> u64
16where
17 T: Into<i64>,
18{
19 max(v.into(), 0i64) as u64
20}
21
22#[cfg(test)]
23mod tests {
24 use float_cmp::approx_eq;
25
26 use crate::stream_engine::time::{SpringDuration, WallClockDuration};
27
28 use super::*;
29
30 #[test]
31 fn test_next_avg() {
32 assert!(approx_eq!(f32, next_avg(1.0, 10000, 1.0), 1.0));
33 assert!(approx_eq!(f32, next_avg(1.5, 2, 3.0), 2.0));
34 assert!(approx_eq!(
35 f32,
36 next_avg(
37 WallClockDuration::from_millis(1500),
38 2,
39 WallClockDuration::from_millis(3000)
40 )
41 .as_secs_f64() as f32,
42 2.0
43 ));
44 }
45}