ruvector_memopt/bench/
metrics.rs

1//! Optimization metrics tracking
2
3use std::collections::VecDeque;
4use std::time::Instant;
5
6#[derive(Debug, Clone)]
7pub struct OptimizationMetrics {
8    pub freed_mb: f64,
9    pub processes_trimmed: usize,
10    pub duration_ms: u64,
11    pub aggressive: bool,
12    pub confidence: f32,
13}
14
15pub struct BenchmarkMetrics {
16    history: VecDeque<OptimizationMetrics>,
17    start_time: Instant,
18    total_freed_mb: f64,
19    total_optimizations: usize,
20    max_history: usize,
21}
22
23impl BenchmarkMetrics {
24    pub fn new() -> Self {
25        Self {
26            history: VecDeque::new(),
27            start_time: Instant::now(),
28            total_freed_mb: 0.0,
29            total_optimizations: 0,
30            max_history: 1000,
31        }
32    }
33    
34    pub fn record_optimization(&mut self, metrics: &OptimizationMetrics) {
35        self.total_freed_mb += metrics.freed_mb;
36        self.total_optimizations += 1;
37        
38        if self.history.len() >= self.max_history {
39            self.history.pop_front();
40        }
41        self.history.push_back(metrics.clone());
42    }
43    
44    pub fn summary(&self) -> MetricsSummary {
45        let avg_freed = if self.total_optimizations > 0 {
46            self.total_freed_mb / self.total_optimizations as f64
47        } else { 0.0 };
48        
49        let avg_duration = if !self.history.is_empty() {
50            self.history.iter().map(|m| m.duration_ms).sum::<u64>() / self.history.len() as u64
51        } else { 0 };
52        
53        MetricsSummary {
54            total_freed_mb: self.total_freed_mb,
55            total_optimizations: self.total_optimizations,
56            avg_freed_mb: avg_freed,
57            avg_duration_ms: avg_duration,
58            uptime_secs: self.start_time.elapsed().as_secs(),
59        }
60    }
61}
62
63#[derive(Debug, Clone)]
64pub struct MetricsSummary {
65    pub total_freed_mb: f64,
66    pub total_optimizations: usize,
67    pub avg_freed_mb: f64,
68    pub avg_duration_ms: u64,
69    pub uptime_secs: u64,
70}
71
72impl Default for BenchmarkMetrics {
73    fn default() -> Self { Self::new() }
74}