use std::collections::HashMap;
use std::time::{Duration, Instant};
#[derive(Debug, Clone)]
pub struct ProfilingInfo {
pub call_counts: HashMap<String, usize>,
pub execution_times: HashMap<String, Duration>,
pub allocations: HashMap<String, AllocationInfo>,
pub hot_spots: Vec<HotSpot>,
}
#[derive(Debug, Clone)]
pub struct AllocationInfo {
pub count: usize,
pub bytes: usize,
pub avg_size: f64,
}
#[derive(Debug, Clone)]
pub struct HotSpot {
pub function: String,
pub time_percentage: f64,
pub call_count: usize,
pub avg_time: Duration,
}
#[derive(Debug)]
pub struct Profiler {
profiling_info: ProfilingInfo,
start_time: Instant,
}
impl Profiler {
pub fn new() -> Self {
Self {
profiling_info: ProfilingInfo {
call_counts: HashMap::new(),
execution_times: HashMap::new(),
allocations: HashMap::new(),
hot_spots: Vec::new(),
},
start_time: Instant::now(),
}
}
pub fn record_call(&mut self, function_name: String, duration: Duration) {
*self.profiling_info.call_counts.entry(function_name.clone()).or_insert(0) += 1;
*self.profiling_info.execution_times.entry(function_name).or_insert(Duration::from_secs(0)) += duration;
}
pub fn get_results(&self) -> &ProfilingInfo {
&self.profiling_info
}
}
impl Default for Profiler {
fn default() -> Self {
Self::new()
}
}