scribe_scaling/
metrics.rs

1//! Performance metrics and benchmarking for scaling optimizations.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::time::{Duration, Instant};
6
7/// Scaling performance metrics
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9pub struct ScalingMetrics {
10    pub files_processed: u64,
11    pub total_processing_time: Duration,
12    pub memory_peak: usize,
13    pub cache_hits: u64,
14    pub cache_misses: u64,
15    pub parallel_efficiency: f64,
16    pub streaming_overhead: Duration,
17}
18
19impl ScalingMetrics {
20    pub fn throughput(&self) -> f64 {
21        if self.total_processing_time.as_secs_f64() > 0.0 {
22            self.files_processed as f64 / self.total_processing_time.as_secs_f64()
23        } else {
24            0.0
25        }
26    }
27
28    pub fn cache_hit_ratio(&self) -> f64 {
29        let total = self.cache_hits + self.cache_misses;
30        if total > 0 {
31            self.cache_hits as f64 / total as f64
32        } else {
33            0.0
34        }
35    }
36}
37
38/// Performance tracker for scaling operations
39pub struct PerformanceTracker {
40    start_time: Instant,
41    metrics: ScalingMetrics,
42    checkpoints: HashMap<String, Instant>,
43}
44
45impl PerformanceTracker {
46    pub fn new() -> Self {
47        Self {
48            start_time: Instant::now(),
49            metrics: ScalingMetrics::default(),
50            checkpoints: HashMap::new(),
51        }
52    }
53
54    pub fn checkpoint(&mut self, name: &str) {
55        self.checkpoints.insert(name.to_string(), Instant::now());
56    }
57
58    pub fn record_files_processed(&mut self, count: u64) {
59        self.metrics.files_processed += count;
60    }
61
62    pub fn record_memory_peak(&mut self, memory: usize) {
63        self.metrics.memory_peak = self.metrics.memory_peak.max(memory);
64    }
65
66    pub fn record_cache_hit(&mut self) {
67        self.metrics.cache_hits += 1;
68    }
69
70    pub fn record_cache_miss(&mut self) {
71        self.metrics.cache_misses += 1;
72    }
73
74    pub fn finish(mut self) -> ScalingMetrics {
75        self.metrics.total_processing_time = self.start_time.elapsed();
76        self.metrics
77    }
78}
79
80impl Default for PerformanceTracker {
81    fn default() -> Self {
82        Self::new()
83    }
84}
85
86/// Benchmark result for performance testing
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct BenchmarkResult {
89    pub test_name: String,
90    pub duration: Duration,
91    pub memory_usage: usize,
92    pub throughput: f64,
93    pub success_rate: f64,
94}
95
96impl BenchmarkResult {
97    pub fn new(
98        test_name: String,
99        duration: Duration,
100        memory_usage: usize,
101        throughput: f64,
102        success_rate: f64,
103    ) -> Self {
104        Self {
105            test_name,
106            duration,
107            memory_usage,
108            throughput,
109            success_rate,
110        }
111    }
112}