mod vhost_tests { use super::*; #[test] fn exclusive_bins_reconstruct_exact_prometheus_cumulative_boundaries() { let m = VhostMetrics::default(); m.active.store(5, Ordering::Relaxed); for micros in [1, 5_000, 5_001, 25_000, 5_000_001] { m.complete(200, false, std::time::Duration::from_micros(micros), 0, 1040); } let (cumulative, total) = m.duration_histogram(); assert_eq!(total, 5); assert_eq!(cumulative, [2, 3, 4, 4, 4, 4, 4, 4, 4, 4]); assert_eq!(n(&m.buckets[0]), 2); assert_eq!(n(&m.buckets[1]), 1); assert_eq!(n(&m.buckets[2]), 1); assert_eq!(n(&m.buckets[10]), 1); assert_eq!(n(&m.completed), 5); assert_eq!(n(&m.active), 0); } #[test] fn streaming_completion_and_histogram() { let m = VhostMetrics::default(); m.active.store(1, Ordering::Relaxed); m.complete(503, false, std::time::Duration::from_millis(20), 10, 50); assert_eq!(n(&m.active), 0); assert_eq!(n(&m.errors), 1); assert_eq!(n(&m.status[4]), 1); assert_eq!(n(&m.buckets[1]), 0); assert_eq!(n(&m.buckets[2]), 1); assert_eq!(n(&m.buckets[10]), 0); // exclusive +Inf bin let (buckets, count) = m.duration_histogram(); assert_eq!(buckets[1], 0); assert_eq!(buckets[2], 1); assert_eq!(buckets[9], 1); assert_eq!(count, 1); let view = m.view(); assert_eq!(view.requests, 0); assert_eq!(view.completed, 1); assert_eq!(view.errors, 1); assert!(view.ewma_latency_ms >= 20.0); } } mod compression_counter_tests { use super::*; use crate::compression::AppliedEncoding; #[test] fn each_codec_has_an_independent_counter() { let metrics = Metrics::default(); for encoding in [ AppliedEncoding::Gzip, AppliedEncoding::Brotli, AppliedEncoding::Zstd, AppliedEncoding::Deflate, ] { metrics.record_compression(encoding); } let view = metrics.view(); assert_eq!(view.compression_gzip, 1); assert_eq!(view.compression_br, 1); assert_eq!(view.compression_zstd, 1); assert_eq!(view.compression_deflate, 1); } }