scirs2_sparse/realtime_performance_monitor/
mod.rs1pub mod alerts;
70pub mod config;
71pub mod history;
72pub mod metrics;
73pub mod monitor;
74
75pub use alerts::{
77 Alert, AlertCondition, AlertManager, AlertRule, AlertSeverity, AlertStats, NotificationChannel,
78};
79pub use config::{PerformanceMonitorConfig, UseCase};
80pub use history::{PerformanceHistory, PerformanceTrend, ProcessorSummary};
81pub use metrics::{
82 AggregatedMetrics, ExecutionTimer, PerformanceSample, ProcessorType, SystemMetrics,
83};
84pub use monitor::{
85 HybridProcessorMonitor, MemoryCompressorMonitor, MonitoringSummary, NeuralProcessorMonitor,
86 QuantumProcessorMonitor, RealTimePerformanceMonitor,
87};
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn test_config_creation() {
95 let config = PerformanceMonitorConfig::default();
96 assert!(config.validate().is_ok());
97 assert_eq!(config.monitoring_interval_ms, 100);
98 assert!(config.adaptive_tuning);
99 }
100
101 #[test]
102 fn test_config_builder_pattern() {
103 let config = PerformanceMonitorConfig::new()
104 .with_monitoring_interval_ms(200)
105 .with_adaptive_tuning(false)
106 .with_alerts(true)
107 .with_prediction(false);
108
109 assert!(config.validate().is_ok());
110 assert_eq!(config.monitoring_interval_ms, 200);
111 assert!(!config.adaptive_tuning);
112 assert!(config.enable_alerts);
113 assert!(!config.enable_prediction);
114 }
115
116 #[test]
117 fn test_predefined_configurations() {
118 let lightweight = PerformanceMonitorConfig::lightweight();
119 assert!(lightweight.validate().is_ok());
120 assert_eq!(lightweight.monitoring_interval_ms, 500);
121 assert!(!lightweight.adaptive_tuning);
122
123 let high_perf = PerformanceMonitorConfig::high_performance();
124 assert!(high_perf.validate().is_ok());
125 assert_eq!(high_perf.monitoring_interval_ms, 50);
126 assert!(high_perf.adaptive_tuning);
127
128 let debug = PerformanceMonitorConfig::debug();
129 assert!(debug.validate().is_ok());
130 assert_eq!(debug.monitoring_interval_ms, 10);
131 assert!(debug.adaptive_tuning);
132 }
133
134 #[test]
135 fn test_use_case_configurations() {
136 let production = PerformanceMonitorConfig::recommended_for_use_case(UseCase::Production);
137 assert!(production.validate().is_ok());
138
139 let development = PerformanceMonitorConfig::recommended_for_use_case(UseCase::Development);
140 assert!(development.validate().is_ok());
141
142 let testing = PerformanceMonitorConfig::recommended_for_use_case(UseCase::Testing);
143 assert!(testing.validate().is_ok());
144
145 let benchmarking =
146 PerformanceMonitorConfig::recommended_for_use_case(UseCase::Benchmarking);
147 assert!(benchmarking.validate().is_ok());
148
149 let low_resource = PerformanceMonitorConfig::recommended_for_use_case(UseCase::LowResource);
150 assert!(low_resource.validate().is_ok());
151 }
152
153 #[test]
154 fn test_processor_type_enumeration() {
155 let all_types = ProcessorType::all();
156 assert_eq!(all_types.len(), 4);
157 assert!(all_types.contains(&ProcessorType::QuantumInspired));
158 assert!(all_types.contains(&ProcessorType::NeuralAdaptive));
159 assert!(all_types.contains(&ProcessorType::QuantumNeuralHybrid));
160 assert!(all_types.contains(&ProcessorType::MemoryCompression));
161 }
162
163 #[test]
164 fn test_processor_type_capabilities() {
165 assert!(ProcessorType::QuantumInspired.supports_quantum_metrics());
166 assert!(!ProcessorType::QuantumInspired.supports_neural_metrics());
167 assert!(!ProcessorType::QuantumInspired.supports_compression_metrics());
168
169 assert!(!ProcessorType::NeuralAdaptive.supports_quantum_metrics());
170 assert!(ProcessorType::NeuralAdaptive.supports_neural_metrics());
171 assert!(!ProcessorType::NeuralAdaptive.supports_compression_metrics());
172
173 assert!(ProcessorType::QuantumNeuralHybrid.supports_quantum_metrics());
174 assert!(ProcessorType::QuantumNeuralHybrid.supports_neural_metrics());
175 assert!(!ProcessorType::QuantumNeuralHybrid.supports_compression_metrics());
176
177 assert!(!ProcessorType::MemoryCompression.supports_quantum_metrics());
178 assert!(!ProcessorType::MemoryCompression.supports_neural_metrics());
179 assert!(ProcessorType::MemoryCompression.supports_compression_metrics());
180 }
181
182 #[test]
183 fn test_performance_sample_creation() {
184 let sample =
185 PerformanceSample::new(ProcessorType::QuantumInspired, "test-processor".to_string())
186 .with_execution_time(100.0)
187 .with_throughput(500.0)
188 .with_cache_hit_ratio(0.8);
189
190 assert_eq!(sample.processor_type, ProcessorType::QuantumInspired);
191 assert_eq!(sample.processor_id, "test-processor");
192 assert_eq!(sample.execution_time_ms, 100.0);
193 assert_eq!(sample.throughput_ops_per_sec, 500.0);
194 assert_eq!(sample.cache_hit_ratio, 0.8);
195 }
196
197 #[test]
198 fn test_execution_timer() {
199 let timer = ExecutionTimer::new();
200 std::thread::sleep(std::time::Duration::from_millis(10));
201
202 let elapsed = timer.elapsed_ms();
203 assert!(elapsed >= 10.0);
204
205 let sample = timer.to_sample(ProcessorType::QuantumInspired, "test".to_string());
206 assert!(sample.execution_time_ms >= 10.0);
207 }
208
209 #[test]
210 fn test_performance_history() {
211 let mut history = PerformanceHistory::new(100);
212
213 let sample = PerformanceSample::new(ProcessorType::QuantumInspired, "test".to_string())
214 .with_execution_time(100.0);
215
216 history.add_sample(sample);
217 assert_eq!(history.samples.len(), 1);
218 assert_eq!(history.aggregated_metrics.sample_count, 1);
219
220 let recent = history.get_recent_samples(10);
221 assert_eq!(recent.len(), 1);
222 }
223
224 #[test]
225 fn test_alert_severity_ordering() {
226 assert!(AlertSeverity::Critical > AlertSeverity::Error);
227 assert!(AlertSeverity::Error > AlertSeverity::Warning);
228 assert!(AlertSeverity::Warning > AlertSeverity::Info);
229 }
230
231 #[test]
232 fn test_alert_manager() {
233 let manager = AlertManager::new(1000);
234 assert!(!manager.alert_rules.is_empty()); let stats = manager.get_alert_stats();
237 assert_eq!(stats.total_alerts, 0);
238 assert_eq!(stats.active_alerts, 0);
239 }
240
241 #[test]
242 fn test_monitor_creation_and_summary() {
243 let config = PerformanceMonitorConfig::default();
244 let monitor = RealTimePerformanceMonitor::new(config);
245
246 assert!(!monitor.is_monitoring_active());
247
248 let summary = monitor.get_monitoring_summary();
249 assert!(!summary.monitoring_active);
250 assert_eq!(summary.total_samples, 0);
251 assert_eq!(summary.active_alerts, 0);
252 assert_eq!(summary.registered_processors, 0);
253 }
254
255 #[test]
256 fn test_system_metrics() {
257 let metrics = SystemMetrics::new();
258 assert_eq!(metrics.cpu_usage, 0.0);
259 assert_eq!(metrics.memory_usage, 0.0);
260 assert!(metrics.timestamp > 0);
261
262 let health_score = metrics.health_score();
263 assert!((0.0..=1.0).contains(&health_score));
264 }
265
266 #[test]
267 fn test_aggregated_metrics() {
268 let mut metrics = AggregatedMetrics::new();
269
270 let sample = PerformanceSample::new(ProcessorType::QuantumInspired, "test".to_string())
271 .with_execution_time(100.0)
272 .with_throughput(500.0);
273
274 metrics.update_with_sample(&sample);
275 assert_eq!(metrics.sample_count, 1);
276 assert_eq!(metrics.avg_execution_time, 100.0);
277 assert_eq!(metrics.avg_throughput, 500.0);
278 }
279
280 #[test]
281 fn test_performance_trend_display() {
282 assert_eq!(PerformanceTrend::Improving.to_string(), "Improving");
283 assert_eq!(PerformanceTrend::Stable.to_string(), "Stable");
284 assert_eq!(PerformanceTrend::Degrading.to_string(), "Degrading");
285 assert_eq!(
286 PerformanceTrend::Insufficient.to_string(),
287 "Insufficient Data"
288 );
289 }
290
291 #[test]
292 fn test_memory_usage_estimation() {
293 let config = PerformanceMonitorConfig::default();
294 let memory_usage = config.estimated_memory_usage();
295 assert!(memory_usage > 0);
296
297 let lightweight = PerformanceMonitorConfig::lightweight();
298 let lightweight_memory = lightweight.estimated_memory_usage();
299 assert!(lightweight_memory < memory_usage);
300 }
301
302 #[test]
303 fn test_realtime_suitability() {
304 let config = PerformanceMonitorConfig::default();
305 assert!(config.is_realtime_suitable());
306
307 let debug_config = PerformanceMonitorConfig::debug();
308 let _is_suitable = debug_config.is_realtime_suitable();
310 }
311}