pub mod metrics;
pub mod monitor;
pub mod profiler;
pub mod optimizer;
pub mod cache;
pub mod memory_pool;
pub mod async_optimizer;
pub use metrics::*;
pub use monitor::*;
pub use profiler::*;
pub use optimizer::*;
pub use cache::*;
pub use memory_pool::*;
pub use async_optimizer::*;
use std::time::{Duration, Instant};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
static GLOBAL_PERFORMANCE_TRACKER: once_cell::sync::Lazy<PerformanceTracker> =
once_cell::sync::Lazy::new(|| PerformanceTracker::new());
pub struct PerformanceTargets {
pub speed_multiplier: f64,
pub max_wasm_size_mb: f64,
pub memory_reduction_target: f64,
pub max_tool_latency_ms: u64,
pub max_llm_latency_ms: u64,
}
impl Default for PerformanceTargets {
fn default() -> Self {
Self {
speed_multiplier: 2.0,
max_wasm_size_mb: 5.0,
memory_reduction_target: 0.5,
max_tool_latency_ms: 100,
max_llm_latency_ms: 5000,
}
}
}
pub struct PerformanceTracker {
metrics: Arc<MetricsCollector>,
monitor: Arc<SystemMonitor>,
profiler: Arc<Profiler>,
optimizer: Arc<Optimizer>,
}
impl PerformanceTracker {
pub fn new() -> Self {
Self {
metrics: Arc::new(MetricsCollector::new()),
monitor: Arc::new(SystemMonitor::new()),
profiler: Arc::new(Profiler::new()),
optimizer: Arc::new(Optimizer::new()),
}
}
pub fn global() -> &'static PerformanceTracker {
&GLOBAL_PERFORMANCE_TRACKER
}
pub fn start_operation(&self, operation_type: &str) -> OperationTracker {
OperationTracker::new(operation_type, &self.metrics)
}
pub fn record_measurement(&self, metric_name: &str, value: f64, unit: MetricUnit) {
self.metrics.record(metric_name, value, unit);
}
pub fn get_performance_report(&self) -> PerformanceReport {
PerformanceReport {
metrics: self.metrics.get_summary(),
system_stats: self.monitor.get_current_stats(),
memory_usage: self.get_memory_usage(),
targets: PerformanceTargets::default(),
timestamp: Instant::now(),
}
}
pub fn validate_targets(&self) -> TargetValidationResult {
let targets = PerformanceTargets::default();
let current_metrics = self.metrics.get_summary();
TargetValidationResult {
speed_target_met: self.check_speed_target(&targets, ¤t_metrics),
memory_target_met: self.check_memory_target(&targets),
latency_targets_met: self.check_latency_targets(&targets, ¤t_metrics),
overall_score: self.calculate_overall_score(&targets, ¤t_metrics),
}
}
fn get_memory_usage(&self) -> MemoryUsage {
use memory_stats::memory_stats;
let stats = memory_stats().unwrap_or_default();
MemoryUsage {
physical_mem: stats.physical_mem,
virtual_mem: stats.virtual_mem,
}
}
fn check_speed_target(&self, targets: &PerformanceTargets, metrics: &MetricsSummary) -> bool {
if let Some(avg_latency) = metrics.get_average_latency("tool_execution") {
avg_latency < (1000.0 / targets.speed_multiplier) } else {
false
}
}
fn check_memory_target(&self, targets: &PerformanceTargets) -> bool {
let current_memory = self.get_memory_usage();
current_memory.physical_mem < (100 * 1024 * 1024) }
fn check_latency_targets(&self, targets: &PerformanceTargets, metrics: &MetricsSummary) -> bool {
let tool_latency_ok = metrics.get_average_latency("tool_execution")
.map(|lat| lat < targets.max_tool_latency_ms as f64)
.unwrap_or(false);
let llm_latency_ok = metrics.get_average_latency("llm_request")
.map(|lat| lat < targets.max_llm_latency_ms as f64)
.unwrap_or(false);
tool_latency_ok && llm_latency_ok
}
fn calculate_overall_score(&self, targets: &PerformanceTargets, metrics: &MetricsSummary) -> f64 {
let mut score = 0.0;
let mut components = 0;
if self.check_speed_target(targets, metrics) {
score += 40.0;
}
components += 1;
if self.check_memory_target(targets) {
score += 30.0;
}
components += 1;
if self.check_latency_targets(targets, metrics) {
score += 30.0;
}
components += 1;
score
}
}
pub struct OperationTracker {
operation_type: String,
start_time: Instant,
metrics_collector: Arc<MetricsCollector>,
}
impl OperationTracker {
fn new(operation_type: &str, metrics_collector: &Arc<MetricsCollector>) -> Self {
Self {
operation_type: operation_type.to_string(),
start_time: Instant::now(),
metrics_collector: metrics_collector.clone(),
}
}
pub fn finish(self) {
let duration = self.start_time.elapsed();
self.metrics_collector.record(
&format!("{}_duration", self.operation_type),
duration.as_millis() as f64,
MetricUnit::Milliseconds,
);
}
pub fn finish_with_metadata(self, metadata: std::collections::HashMap<String, String>) {
let duration = self.start_time.elapsed();
self.metrics_collector.record_with_metadata(
&format!("{}_duration", self.operation_type),
duration.as_millis() as f64,
MetricUnit::Milliseconds,
metadata,
);
}
}
#[derive(Debug, Clone)]
pub struct TargetValidationResult {
pub speed_target_met: bool,
pub memory_target_met: bool,
pub latency_targets_met: bool,
pub overall_score: f64,
}
#[derive(Debug, Clone, Default)]
pub struct MemoryUsage {
pub physical_mem: usize,
pub virtual_mem: usize,
}
#[derive(Debug, Clone)]
pub struct PerformanceReport {
pub metrics: MetricsSummary,
pub system_stats: SystemStats,
pub memory_usage: MemoryUsage,
pub targets: PerformanceTargets,
pub timestamp: Instant,
}
impl PerformanceReport {
pub fn to_string(&self) -> String {
format!(
"Performance Report ({})\n\
=================================\n\
Tool Operations: {:.2}ms avg\n\
LLM Requests: {:.2}ms avg\n\
Memory Usage: {:.2}MB\n\
CPU Usage: {:.1}%\n\
=================================",
chrono::Utc::now().format("%Y-%m-%d %H:%M:%S"),
self.metrics.get_average_latency("tool_execution").unwrap_or(0.0),
self.metrics.get_average_latency("llm_request").unwrap_or(0.0),
self.memory_usage.physical_mem as f64 / (1024.0 * 1024.0),
self.system_stats.cpu_usage
)
}
}
#[macro_export]
macro_rules! time_operation {
($operation_type:expr, $block:block) => {{
let tracker = $crate::performance::PerformanceTracker::global()
.start_operation($operation_type);
let result = $block;
tracker.finish();
result
}};
}
#[macro_export]
macro_rules! time_async_operation {
($operation_type:expr, $async_block:expr) => {{
let tracker = $crate::performance::PerformanceTracker::global()
.start_operation($operation_type);
let result = $async_block.await;
tracker.finish();
result
}};
}