use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Percentile {
P50,
P90,
P95,
P99,
P999,
}
impl Percentile {
pub fn quantile(&self) -> f64 {
match self {
Percentile::P50 => 0.5,
Percentile::P90 => 0.9,
Percentile::P95 => 0.95,
Percentile::P99 => 0.99,
Percentile::P999 => 0.999,
}
}
pub fn as_str(&self) -> &'static str {
match self {
Percentile::P50 => "p50",
Percentile::P90 => "p90",
Percentile::P95 => "p95",
Percentile::P99 => "p99",
Percentile::P999 => "p999",
}
}
pub fn all() -> &'static [Percentile] {
&[
Percentile::P50,
Percentile::P90,
Percentile::P95,
Percentile::P99,
Percentile::P999,
]
}
}
impl fmt::Display for Percentile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
pub trait PercentileExt {
fn get_as_millis(&self, percentile: &Percentile) -> f64;
}
impl PercentileExt for std::collections::HashMap<Percentile, f64> {
fn get_as_millis(&self, percentile: &Percentile) -> f64 {
self.get(percentile).unwrap_or(&0.0) / 1_000_000.0
}
}