crabscore_core/
metrics.rs

1//! Performance, energy, and cost metrics for CrabScore
2
3use serde::{Deserialize, Serialize};
4
5/// Performance-related metrics
6#[derive(Debug, Clone, Serialize, Deserialize, Default)]
7pub struct PerformanceMetrics {
8    /// Latency measurements
9    pub latency: LatencyMetrics,
10    /// Throughput measurements
11    pub throughput: ThroughputMetrics,
12    /// Resource usage metrics
13    pub resource_usage: ResourceMetrics,
14    /// Scalability metrics
15    pub scalability: ScalabilityMetrics,
16}
17
18/// Latency measurements
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct LatencyMetrics {
21    /// 50th percentile latency in milliseconds
22    pub p50_ms: f64,
23    /// 95th percentile latency in milliseconds
24    pub p95_ms: f64,
25    /// 99th percentile latency in milliseconds
26    pub p99_ms: f64,
27    /// Cold start latency in milliseconds
28    pub cold_start_ms: f64,
29    /// Time to first byte in milliseconds
30    pub ttfb_ms: f64,
31}
32
33/// Throughput measurements
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct ThroughputMetrics {
36    /// Requests per second
37    pub requests_per_second: f64,
38    /// Data throughput in MB/s
39    pub mb_per_second: f64,
40    /// Number of concurrent connections
41    pub concurrent_connections: u64,
42    /// Queue depth
43    pub queue_depth: f64,
44}
45
46/// Resource usage metrics
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct ResourceMetrics {
49    /// CPU efficiency (operations per cycle)
50    pub cpu_efficiency: f64,
51    /// Memory bandwidth in GB/s
52    pub memory_bandwidth_gb_s: f64,
53    /// I/O operations per second
54    pub io_operations_per_sec: f64,
55    /// Cache hit rate (0.0 to 1.0)
56    pub cache_hit_rate: f64,
57}
58
59/// Scalability metrics
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct SafetyMetrics {
62    /// Total number of `unsafe` blocks
63    pub unsafe_blocks: u32,
64    /// Total Clippy warnings (opt-level = pedantic)
65    pub clippy_warnings: u32,
66    /// Average cyclomatic complexity per function
67    pub avg_cyclomatic: f64,
68}
69
70impl Default for SafetyMetrics {
71    fn default() -> Self {
72        Self {
73            unsafe_blocks: 0,
74            clippy_warnings: 0,
75            avg_cyclomatic: 1.0,
76        }
77    }
78}
79
80/// Scalability metrics for concurrent and parallel Rust workloads.
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct ScalabilityMetrics {
83    /// Linear scaling factor (1.0 = perfect linear scaling)
84    pub linear_scaling_factor: f64,
85    /// Performance at different concurrency levels (threads, performance_ratio)
86    pub degradation_curve: Vec<(u32, f64)>,
87    /// Bottleneck score (0.0 to 1.0, higher is worse)
88    pub bottleneck_score: f64,
89    /// Elasticity coefficient (0.0 to 1.0, higher is better)
90    pub elasticity_coefficient: f64,
91}
92
93/// Energy-related metrics
94#[derive(Debug, Clone, Serialize, Deserialize, Default)]
95pub struct EnergyMetrics {
96    /// Direct power consumption measurements
97    pub direct_consumption: PowerConsumption,
98    /// Carbon efficiency metrics
99    pub carbon_efficiency: CarbonEfficiency,
100    /// Hardware lifecycle metrics
101    pub hardware_lifecycle: HardwareLifecycle,
102    /// Algorithmic efficiency metrics
103    pub algorithmic_efficiency: AlgorithmEfficiency,
104}
105
106/// Power consumption metrics
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct PowerConsumption {
109    /// Average power consumption in watts
110    pub average_watts: f64,
111    /// Peak power consumption in watts
112    pub peak_watts: f64,
113    /// Idle power consumption in watts
114    pub idle_watts: f64,
115    /// Energy per operation in joules
116    pub joules_per_operation: f64,
117}
118
119/// Carbon efficiency metrics
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct CarbonEfficiency {
122    /// Grams of CO2 per operation
123    pub co2_per_operation: f64,
124    /// Carbon intensity of energy source (gCO2/kWh)
125    pub carbon_intensity: f64,
126    /// Renewable energy percentage (0.0 to 1.0)
127    pub renewable_percentage: f64,
128}
129
130/// Hardware lifecycle metrics
131#[derive(Debug, Clone, Serialize, Deserialize)]
132pub struct HardwareLifecycle {
133    /// Thermal efficiency (0.0 to 1.0)
134    pub thermal_efficiency: f64,
135    /// Component stress score (0.0 to 1.0, lower is better)
136    pub component_stress: f64,
137    /// Expected hardware lifespan in years
138    pub expected_lifespan_years: f64,
139}
140
141/// Algorithm efficiency metrics
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub struct AlgorithmEfficiency {
144    /// Time complexity (O(n), O(n log n), etc.)
145    pub time_complexity: String,
146    /// Space complexity (O(1), O(n), etc.)
147    pub space_complexity: String,
148    /// Actual observed time complexity coefficient
149    pub actual_time_coefficient: f64,
150    /// Actual observed space coefficient
151    pub actual_space_coefficient: f64,
152}
153
154/// Cost-related metrics
155#[derive(Debug, Clone, Serialize, Deserialize, Default)]
156pub struct CostMetrics {
157    /// Infrastructure costs
158    pub infrastructure: InfrastructureCosts,
159    /// Operational costs
160    pub operations: OperationalCosts,
161    /// Development costs
162    pub development: DevelopmentCosts,
163    /// Business impact metrics
164    pub business_impact: BusinessImpact,
165}
166
167/// Infrastructure cost metrics
168#[derive(Debug, Clone, Serialize, Deserialize)]
169pub struct InfrastructureCosts {
170    /// Cloud compute costs per month in USD
171    pub cloud_compute_usd: f64,
172    /// Storage costs per month in USD
173    pub storage_usd: f64,
174    /// Network egress costs per month in USD
175    pub network_egress_usd: f64,
176    /// Cost per million operations in USD
177    pub cost_per_million_ops: f64,
178}
179
180/// Operational cost metrics
181#[derive(Debug, Clone, Serialize, Deserialize)]
182pub struct OperationalCosts {
183    /// Mean time to resolve incidents in minutes
184    pub mttr_minutes: f64,
185    /// Number of incidents per month
186    pub incidents_per_month: f64,
187    /// Operational overhead percentage (0.0 to 1.0)
188    pub overhead_percentage: f64,
189    /// Monitoring and alerting costs per month in USD
190    pub monitoring_usd: f64,
191}
192
193/// Development cost metrics
194#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct DevelopmentCosts {
196    /// Lines of code
197    pub loc: u64,
198    /// Cyclomatic complexity
199    pub cyclomatic_complexity: f64,
200    /// Code churn (lines changed per month)
201    pub code_churn: f64,
202    /// Time to onboard new developer in days
203    pub onboarding_days: f64,
204}
205
206/// Business impact metrics
207#[derive(Debug, Clone, Serialize, Deserialize)]
208pub struct BusinessImpact {
209    /// Revenue impact per 100ms latency improvement in USD
210    pub revenue_per_100ms_latency: f64,
211    /// Customer satisfaction score (0-100)
212    pub csat_score: f64,
213    /// SLA compliance percentage (0.0 to 1.0)
214    pub sla_compliance: f64,
215    /// Competitive advantage score (0-10)
216    pub competitive_advantage: f64,
217}
218
219// -----------------------------------------------------------------------------
220// Default implementations for high-level metric structs (temporary placeholders)
221// -----------------------------------------------------------------------------
222
223impl Default for LatencyMetrics {
224    fn default() -> Self {
225        Self {
226            p50_ms: 0.0,
227            p95_ms: 0.0,
228            p99_ms: 0.0,
229            cold_start_ms: 0.0,
230            ttfb_ms: 0.0,
231        }
232    }
233}
234
235impl Default for ThroughputMetrics {
236    fn default() -> Self {
237        Self {
238            requests_per_second: 0.0,
239            mb_per_second: 0.0,
240            concurrent_connections: 0,
241            queue_depth: 0.0,
242        }
243    }
244}
245
246impl Default for ResourceMetrics {
247    fn default() -> Self {
248        Self {
249            cpu_efficiency: 0.0,
250            memory_bandwidth_gb_s: 0.0,
251            io_operations_per_sec: 0.0,
252            cache_hit_rate: 0.0,
253        }
254    }
255}
256
257impl Default for ScalabilityMetrics {
258    fn default() -> Self {
259        Self {
260            linear_scaling_factor: 1.0,
261            degradation_curve: Vec::new(),
262            bottleneck_score: 0.0,
263            elasticity_coefficient: 0.0,
264        }
265    }
266}
267
268impl Default for PowerConsumption {
269    fn default() -> Self {
270        Self {
271            average_watts: 0.0,
272            peak_watts: 0.0,
273            idle_watts: 0.0,
274            joules_per_operation: 0.0,
275        }
276    }
277}
278
279impl Default for CarbonEfficiency {
280    fn default() -> Self {
281        Self {
282            co2_per_operation: 0.0,
283            carbon_intensity: 0.0,
284            renewable_percentage: 0.0,
285        }
286    }
287}
288
289impl Default for HardwareLifecycle {
290    fn default() -> Self {
291        Self {
292            thermal_efficiency: 0.0,
293            component_stress: 0.0,
294            expected_lifespan_years: 0.0,
295        }
296    }
297}
298
299impl Default for AlgorithmEfficiency {
300    fn default() -> Self {
301        Self {
302            time_complexity: String::new(),
303            space_complexity: String::new(),
304            actual_time_coefficient: 0.0,
305            actual_space_coefficient: 0.0,
306        }
307    }
308}
309
310impl Default for InfrastructureCosts {
311    fn default() -> Self {
312        Self {
313            cloud_compute_usd: 0.0,
314            storage_usd: 0.0,
315            network_egress_usd: 0.0,
316            cost_per_million_ops: 0.0,
317        }
318    }
319}
320
321impl Default for OperationalCosts {
322    fn default() -> Self {
323        Self {
324            mttr_minutes: 0.0,
325            incidents_per_month: 0.0,
326            overhead_percentage: 0.0,
327            monitoring_usd: 0.0,
328        }
329    }
330}
331
332impl Default for DevelopmentCosts {
333    fn default() -> Self {
334        Self {
335            loc: 0,
336            cyclomatic_complexity: 0.0,
337            code_churn: 0.0,
338            onboarding_days: 0.0,
339        }
340    }
341}
342
343impl Default for BusinessImpact {
344    fn default() -> Self {
345        Self {
346            revenue_per_100ms_latency: 0.0,
347            csat_score: 0.0,
348            sla_compliance: 0.0,
349            competitive_advantage: 0.0,
350        }
351    }
352}