use latency_buckets::Histogram;
use std::time::Duration;
#[test]
fn empty_is_zero() {
let h = Histogram::new();
assert_eq!(h.count(), 0);
assert_eq!(h.percentile(0.5), Duration::ZERO);
}
#[test]
fn single_sample_returns_within_its_bucket() {
let mut h = Histogram::new();
h.record(Duration::from_millis(100));
let p = h.percentile(0.5);
assert!(p.as_millis() >= 50 && p.as_millis() <= 200, "got {p:?}");
}
#[test]
fn p50_increases_with_higher_workload() {
let mut h = Histogram::new();
for ms in [10, 10, 10, 1000, 1000, 1000] {
h.record(Duration::from_millis(ms));
}
let p50 = h.percentile(0.5);
assert!(p50.as_millis() > 10);
}
#[test]
fn p95_lands_in_tail() {
let mut h = Histogram::new();
for _ in 0..90 {
h.record(Duration::from_millis(10));
}
for _ in 0..10 {
h.record(Duration::from_millis(5000));
}
let p95 = h.percentile(0.95);
assert!(p95.as_millis() > 100, "p95 should reflect the tail; got {p95:?}");
}
#[test]
fn count_tracks_records() {
let mut h = Histogram::new();
for _ in 0..10 {
h.record(Duration::from_millis(1));
}
assert_eq!(h.count(), 10);
}