use std::time::Duration;
#[derive(Debug)]
pub struct ProfileSummary {
pub entries: Vec<ProfileEntry>,
}
impl ProfileSummary {
pub fn has_slow_operations(&self, threshold: Duration) -> bool {
self.entries.iter().any(|e| e.average > threshold)
}
pub fn slow_operations(&self, threshold: Duration) -> Vec<&ProfileEntry> {
self.entries
.iter()
.filter(|e| e.average > threshold)
.collect()
}
}
#[derive(Debug)]
pub struct CounterSummary {
pub entries: Vec<CounterEntry>,
}
impl CounterSummary {
pub fn total(&self, name: &str) -> u64 {
self.entries
.iter()
.find(|entry| entry.name == name)
.map(|entry| entry.total)
.unwrap_or(0)
}
pub fn total_matching(&self, prefix: &str, suffix: &str) -> u64 {
self.entries
.iter()
.filter(|entry| entry.name.starts_with(prefix) && entry.name.ends_with(suffix))
.map(|entry| entry.total)
.sum()
}
}
#[derive(Debug)]
pub struct CounterEntry {
pub name: &'static str,
pub samples: u64,
pub total: u64,
pub average: f64,
pub min: u64,
pub max: u64,
}
impl std::fmt::Display for ProfileSummary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Profile Summary:")?;
writeln!(
f,
"{:<30} {:>8} {:>12} {:>12} {:>12} {:>12} {:>12} {:>12}",
"Operation", "Count", "Total ms", "Self ms", "Avg ms", "P95 ms", "Min ms", "Max ms"
)?;
writeln!(f, "{}", "-".repeat(114))?;
for entry in &self.entries {
writeln!(
f,
"{:<30} {:>8} {:>12.3} {:>12.3} {:>12.3} {:>12.3} {:>12.3} {:>12.3}",
entry.name,
entry.count,
duration_ms(entry.total),
duration_ms(entry.self_total),
duration_ms(entry.average),
duration_ms(entry.p95),
duration_ms(entry.min),
duration_ms(entry.max)
)?;
}
Ok(())
}
}
#[inline]
fn duration_ms(duration: Duration) -> f64 {
duration.as_secs_f64() * 1000.0
}
#[derive(Debug)]
pub struct ProfileEntry {
pub name: &'static str,
pub count: u64,
pub total: Duration,
pub self_total: Duration,
pub child_total: Duration,
pub average: Duration,
pub self_average: Duration,
pub min: Duration,
pub max: Duration,
pub self_min: Duration,
pub self_max: Duration,
pub p50: Duration,
pub p95: Duration,
pub p99: Duration,
pub samples_over_1ms: u64,
pub samples_over_10ms: u64,
pub samples_over_100ms: u64,
}