#![cfg_attr(coverage_nightly, coverage(off))]
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationProfile {
pub operation_id: String,
pub operation_type: String,
#[serde(skip, default = "Instant::now")]
pub start_time: Instant,
#[serde(skip)]
pub end_time: Option<Instant>,
pub duration_ms: Option<f64>,
pub memory_before_mb: f64,
pub memory_after_mb: Option<f64>,
pub cpu_time_ms: f64,
pub io_wait_ms: f64,
pub children: Vec<OperationProfile>,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlameGraphNode {
pub name: String,
pub value: f64, pub children: Vec<FlameGraphNode>,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallFrame {
pub function_name: String,
pub file_path: String,
pub line_number: u32,
pub duration_ms: f64,
pub self_time_ms: f64,
pub call_count: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemorySample {
#[serde(skip, default = "Instant::now")]
pub timestamp: Instant,
pub heap_used_mb: f64,
pub heap_total_mb: f64,
pub stack_used_mb: f64,
pub gc_count: u32,
pub gc_pause_ms: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bottleneck {
pub location: String,
pub bottleneck_type: BottleneckType,
pub severity: BottleneckSeverity,
pub impact_ms: f64,
pub occurrence_count: u32,
pub recommendation: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum BottleneckType {
CpuBound,
IoBound,
MemoryBound,
LockContention,
NetworkLatency,
DatabaseQuery,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum BottleneckSeverity {
Low,
Medium,
High,
Critical,
}
pub struct PerformanceProfiler {
active_profiles: Arc<RwLock<HashMap<String, OperationProfile>>>,
completed_profiles: Arc<RwLock<Vec<OperationProfile>>>,
#[allow(dead_code)]
call_stacks: Arc<RwLock<Vec<Vec<CallFrame>>>>,
memory_samples: Arc<RwLock<Vec<MemorySample>>>,
bottlenecks: Arc<RwLock<Vec<Bottleneck>>>,
config: ProfilerConfig,
}
#[derive(Debug, Clone)]
pub struct ProfilerConfig {
pub enable_cpu_profiling: bool,
pub enable_memory_profiling: bool,
pub enable_io_profiling: bool,
pub sample_interval_ms: u64,
pub max_stack_depth: usize,
pub max_profiles_retained: usize,
}
impl Default for ProfilerConfig {
fn default() -> Self {
Self {
enable_cpu_profiling: true,
enable_memory_profiling: true,
enable_io_profiling: true,
sample_interval_ms: 100,
max_stack_depth: 50,
max_profiles_retained: 1000,
}
}
}
include!("profiler_operations.rs");
include!("profiler_tests.rs");