diff --git a/Cargo.lock b/Cargo.lock index eaa53ada..ad6d603b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,17 +224,6 @@ dependencies = [ "cc", ] -[[package]] -name = "average" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04c5a79a6c36c527e1c31bc0effa93af02e7278b7369b83e0674255290b6c1b0" -dependencies = [ - "easy-cast", - "float-ord", - "num-traits", -] - [[package]] name = "aws-lc-rs" version = "1.16.3" @@ -889,15 +878,6 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" -[[package]] -name = "easy-cast" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23f40539c229fc2e4674bdecdf24bfcc2cb83631ca911c78a035fa9f2381c32b" -dependencies = [ - "libm", -] - [[package]] name = "educe" version = "0.6.0" @@ -1018,12 +998,6 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" -[[package]] -name = "float-ord" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce81f49ae8a0482e4c55ea62ebbd7e5a686af544c00b9d090bba3ff9be97b3d" - [[package]] name = "flume" version = "0.12.0" @@ -1567,12 +1541,6 @@ dependencies = [ "windows-link 0.2.1", ] -[[package]] -name = "libm" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" - [[package]] name = "lightway-app-utils" version = "0.1.0" @@ -1695,7 +1663,6 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", - "average", "bytes", "bytesize", "clap", @@ -2073,7 +2040,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", - "libm", ] [[package]] diff --git a/lightway-server/Cargo.toml b/lightway-server/Cargo.toml index 89627cc4..6bfafa5f 100644 --- a/lightway-server/Cargo.toml +++ b/lightway-server/Cargo.toml @@ -19,7 +19,6 @@ workspace = true [dependencies] anyhow.workspace = true async-trait.workspace = true -average = "0.17.0" bytes.workspace = true bytesize.workspace = true clap.workspace = true diff --git a/lightway-server/src/ip_manager/ip_pool.rs b/lightway-server/src/ip_manager/ip_pool.rs index 2c8e673f..b8269a2c 100644 --- a/lightway-server/src/ip_manager/ip_pool.rs +++ b/lightway-server/src/ip_manager/ip_pool.rs @@ -368,8 +368,6 @@ mod tests { #[test] fn test_shuffle() { - use average::Mean; - let mut pool = get_ip_pool(); pool.shuffle_ips(); @@ -386,15 +384,19 @@ mod tests { // chances of this coming out as less than 512 in practice are // miniscule. let mut previous = pool.allocate_ip().unwrap().to_bits(); - let m: Mean = std::iter::from_fn(|| { + let (count, total_differences) = std::iter::from_fn(|| { let ip = pool.allocate_ip()?.to_bits(); let delta = ip.abs_diff(previous); previous = ip; Some(delta as f64) }) - .collect(); - assert_gt!(m.mean(), 512.0); + .fold((0, 0.0f64), |(mut count, mut total_differences), v| { + count += 1; + total_differences += v; + (count, total_differences) + }); + assert_gt!(total_differences / count as f64, 512.0); } } diff --git a/lightway-server/src/main.rs b/lightway-server/src/main.rs index 4b403d40..0f624718 100644 --- a/lightway-server/src/main.rs +++ b/lightway-server/src/main.rs @@ -50,24 +50,25 @@ async fn metrics_debug() { metrics_util::debugging::DebugValue::Gauge(value) => { trace!("metric: {} {labels:?} = Guage({value:?})", name.as_str()) } - metrics_util::debugging::DebugValue::Histogram(values) => { - // TODO: https://docs.rs/average/latest/average/macro.concatenate.html for min/max and avg? - - use average::{Estimate, Max, Mean, Min, concatenate}; - - concatenate!(Stats, [Min, min], [Mean, mean], [Max, max]); - let len = values.len(); - let s: Stats = values.into_iter().map(|f| f.into_inner()).collect(); - + metrics_util::debugging::DebugValue::Histogram(values) if !values.is_empty() => { + let (sum, maximum, minimum) = values.iter().fold( + (0.0f64, f64::NEG_INFINITY, f64::INFINITY), + |(mut sum, maximum, minimum), v| { + let v = v.into_inner(); + sum += v; + (sum, maximum.max(v), minimum.min(v)) + }, + ); trace!( "metric: {} {labels:?} = Histogram({} samples min/avg/max {:.2}/{:.2}/{:.2})", name.as_str(), - len, - s.min(), - s.mean(), - s.max(), + values.len(), + minimum, + sum / values.len() as f64, + maximum, ) } + _ => (), }; } }