1use super::system_profiler::{SystemProfiler, SystemResourceSnapshot};
7use super::aggregator::LockfreeAggregator;
9use std::path::Path;
10use std::sync::{
11 atomic::{AtomicBool, Ordering},
12 Arc, Mutex,
13};
14use std::time::Duration;
15
16static ENHANCED_PROFILING_ACTIVE: AtomicBool = AtomicBool::new(false);
18use std::sync::OnceLock;
19static ENHANCED_OUTPUT_DIR: OnceLock<std::path::PathBuf> = OnceLock::new();
20static SYSTEM_SNAPSHOTS: OnceLock<Mutex<Vec<SystemResourceSnapshot>>> = OnceLock::new();
21
22pub fn start_full_system_profiling<P: AsRef<Path>>(
50 output_dir: P,
51 sample_interval: Duration,
52) -> Result<(), Box<dyn std::error::Error>> {
53 let output_path = output_dir.as_ref().to_path_buf();
54
55 let _ = ENHANCED_OUTPUT_DIR.set(output_path.clone());
57 let _ = SYSTEM_SNAPSHOTS.set(Mutex::new(Vec::new()));
58
59 if output_path.exists() {
61 std::fs::remove_dir_all(&output_path)?;
62 }
63 std::fs::create_dir_all(&output_path)?;
64
65 super::api::trace_all(&output_path)?;
67
68 start_system_monitoring(sample_interval)?;
70
71 ENHANCED_PROFILING_ACTIVE.store(true, Ordering::SeqCst);
72
73 println!("🌟 Enhanced System Profiling Started");
74 println!(" 📊 Memory Tracking: Active");
75 println!(" 🖥️ CPU Monitoring: Active");
76 println!(" 🎮 GPU Monitoring: Active");
77 println!(" 💾 I/O Monitoring: Active");
78 println!(" 🌐 Network Monitoring: Active");
79 println!(" ⏱️ Sample Interval: {:?}", sample_interval);
80 println!(" 📁 Output: {}", output_path.display());
81
82 Ok(())
83}
84
85pub fn stop_system_profiling() -> Result<(), Box<dyn std::error::Error>> {
109 if !ENHANCED_PROFILING_ACTIVE.load(Ordering::SeqCst) {
110 return Ok(()); }
112
113 println!("🛑 Stopping enhanced system profiling...");
114
115 super::api::stop_tracing()?;
117
118 stop_system_monitoring()?;
120
121 let output_dir = ENHANCED_OUTPUT_DIR
123 .get()
124 .ok_or("Output directory not set")?;
125
126 generate_comprehensive_report(output_dir)?;
127
128 ENHANCED_PROFILING_ACTIVE.store(false, Ordering::SeqCst);
129
130 println!("🎉 Enhanced profiling complete!");
131 println!(
132 "📊 Check comprehensive report: {}/system_analysis_report.html",
133 output_dir.display()
134 );
135
136 Ok(())
137}
138
139pub fn is_enhanced_profiling_active() -> bool {
141 ENHANCED_PROFILING_ACTIVE.load(Ordering::SeqCst)
142}
143
144pub fn get_system_snapshot() -> Result<SystemResourceSnapshot, Box<dyn std::error::Error>> {
168 let mut profiler = SystemProfiler::new(Duration::from_millis(100));
169 let mut snapshot = profiler.take_snapshot()?;
170
171 if snapshot.timestamp == 0 {
173 snapshot.timestamp = std::time::SystemTime::now()
174 .duration_since(std::time::UNIX_EPOCH)
175 .unwrap_or_default()
176 .as_millis() as u64;
177 }
178
179 Ok(snapshot)
180}
181
182#[macro_export]
215macro_rules! enhanced_system_profile {
216 ($output_dir:expr, $sample_interval:expr, $block:block) => {{
217 $crate::lockfree::enhanced_api::start_full_system_profiling($output_dir, $sample_interval)?;
218 let result = (|| $block)();
219 $crate::lockfree::enhanced_api::stop_system_profiling()?;
220 result
221 }};
222}
223
224fn start_system_monitoring(sample_interval: Duration) -> Result<(), Box<dyn std::error::Error>> {
226 let snapshots = SYSTEM_SNAPSHOTS
227 .get()
228 .ok_or("System snapshots not initialized")?;
229
230 let snapshots = Arc::new(snapshots);
232 let interval = sample_interval;
233
234 std::thread::spawn(move || {
235 let mut profiler = SystemProfiler::new(interval);
236
237 while ENHANCED_PROFILING_ACTIVE.load(Ordering::SeqCst) {
238 if let Ok(snapshot) = profiler.take_snapshot() {
239 if let Ok(mut snapshots_guard) = snapshots.lock() {
240 snapshots_guard.push(snapshot);
241
242 if snapshots_guard.len() > 10000 {
244 snapshots_guard.remove(0);
245 }
246 }
247 }
248
249 std::thread::sleep(interval);
250 }
251 });
252
253 Ok(())
254}
255
256fn stop_system_monitoring() -> Result<(), Box<dyn std::error::Error>> {
258 println!(" 🛑 System monitoring stopped");
260 Ok(())
261}
262
263fn generate_comprehensive_report(output_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
265 println!("📊 Generating comprehensive system analysis...");
266
267 let aggregator = LockfreeAggregator::new(output_dir.to_path_buf());
269 let memory_analysis = aggregator.aggregate_all_threads()?;
270
271 let system_snapshots = if let Some(snapshots_mutex) = SYSTEM_SNAPSHOTS.get() {
273 if let Ok(snapshots_guard) = snapshots_mutex.lock() {
274 snapshots_guard.clone()
275 } else {
276 Vec::new()
277 }
278 } else {
279 Vec::new()
280 };
281
282 generate_system_correlation_report(&memory_analysis, &system_snapshots, output_dir)?;
284
285 println!(
286 " 📈 Memory analysis: {} allocations analyzed",
287 memory_analysis.summary.total_allocations
288 );
289 println!(
290 " 🖥️ System snapshots: {} data points collected",
291 system_snapshots.len()
292 );
293 println!(" 🎯 Cross-correlation analysis completed");
294
295 Ok(())
296}
297
298fn generate_system_correlation_report(
300 memory_analysis: &super::analysis::LockfreeAnalysis,
301 system_snapshots: &[SystemResourceSnapshot],
302 output_dir: &Path,
303) -> Result<(), Box<dyn std::error::Error>> {
304 std::fs::create_dir_all(output_dir)?;
306 let comprehensive_data = serde_json::json!({
308 "report_type": "comprehensive_system_analysis",
309 "timestamp": chrono::Utc::now().to_rfc3339(),
310 "memory_analysis": memory_analysis,
311 "system_snapshots": system_snapshots,
312 "correlation_analysis": {
313 "memory_cpu_correlation": calculate_memory_cpu_correlation(memory_analysis, system_snapshots),
314 "memory_gpu_correlation": calculate_memory_gpu_correlation(memory_analysis, system_snapshots),
315 "io_memory_correlation": calculate_io_memory_correlation(memory_analysis, system_snapshots),
316 "network_activity_correlation": calculate_network_correlation(memory_analysis, system_snapshots)
317 },
318 "resource_summary": {
319 "avg_cpu_usage": system_snapshots.iter().map(|s| s.cpu_metrics.overall_usage as f64).sum::<f64>() / system_snapshots.len().max(1) as f64,
320 "peak_memory_gb": memory_analysis.summary.peak_memory_usage as f64 / (1024.0 * 1024.0 * 1024.0),
321 "avg_gpu_usage": system_snapshots.iter()
322 .filter_map(|s| s.gpu_metrics.as_ref().map(|g| g.gpu_usage as f64))
323 .sum::<f64>() / system_snapshots.iter().filter(|s| s.gpu_metrics.is_some()).count().max(1) as f64,
324 "total_io_gb": system_snapshots.iter()
325 .map(|s| (s.io_metrics.disk_read_bps + s.io_metrics.disk_write_bps) as f64)
326 .sum::<f64>() / (1024.0 * 1024.0 * 1024.0)
327 }
328 });
329
330 let json_path = output_dir.join("system_analysis_report.json");
332 std::fs::write(
333 &json_path,
334 serde_json::to_string_pretty(&comprehensive_data)?,
335 )?;
336
337 generate_enhanced_system_html(memory_analysis, system_snapshots, output_dir)?;
339
340 println!(" 📄 JSON report: {}", json_path.display());
341
342 Ok(())
343}
344
345fn generate_enhanced_system_html(
347 memory_analysis: &super::analysis::LockfreeAnalysis,
348 _system_snapshots: &[SystemResourceSnapshot],
349 output_dir: &Path,
350) -> Result<(), Box<dyn std::error::Error>> {
351 std::fs::create_dir_all(output_dir)?;
353 let html_path = output_dir.join("system_analysis_report.html");
355 use super::comprehensive_export::export_comprehensive_analysis;
357 use super::resource_integration::{
358 BottleneckType, ComprehensiveAnalysis, CorrelationMetrics, PerformanceInsights,
359 };
360
361 let comprehensive_analysis = ComprehensiveAnalysis {
363 memory_analysis: memory_analysis.clone(),
364 resource_timeline: Vec::new(), performance_insights: PerformanceInsights {
366 primary_bottleneck: BottleneckType::Balanced,
367 cpu_efficiency_score: 50.0,
368 memory_efficiency_score: 75.0,
369 io_efficiency_score: 60.0,
370 recommendations: vec![
371 "Consider using memory pools for frequent allocations".to_string()
372 ],
373 thread_performance_ranking: Vec::new(),
374 },
375 correlation_metrics: CorrelationMetrics {
376 memory_cpu_correlation: 0.4,
377 memory_gpu_correlation: 0.5,
378 memory_io_correlation: 0.3,
379 allocation_rate_vs_cpu_usage: 0.3,
380 deallocation_rate_vs_memory_pressure: 0.2,
381 },
382 };
383
384 export_comprehensive_analysis(&comprehensive_analysis, output_dir, "enhanced_api")?;
385
386 println!(" 🌐 HTML report: {}", html_path.display());
395
396 Ok(())
397}
398
399fn calculate_memory_cpu_correlation(
401 _memory_analysis: &super::analysis::LockfreeAnalysis,
402 system_snapshots: &[SystemResourceSnapshot],
403) -> f64 {
404 if system_snapshots.len() < 2 {
406 return 0.0;
407 }
408
409 let avg_cpu = system_snapshots
411 .iter()
412 .map(|s| s.cpu_metrics.overall_usage as f64)
413 .sum::<f64>()
414 / system_snapshots.len() as f64;
415
416 if avg_cpu > 50.0 {
418 0.7
419 } else {
420 0.3
421 }
422}
423
424fn calculate_memory_gpu_correlation(
425 _memory_analysis: &super::analysis::LockfreeAnalysis,
426 system_snapshots: &[SystemResourceSnapshot],
427) -> f64 {
428 let gpu_active_snapshots = system_snapshots
430 .iter()
431 .filter(|s| s.gpu_metrics.is_some())
432 .count();
433
434 if gpu_active_snapshots > 0 {
435 0.5 } else {
437 0.0 }
439}
440
441fn calculate_io_memory_correlation(
442 _memory_analysis: &super::analysis::LockfreeAnalysis,
443 system_snapshots: &[SystemResourceSnapshot],
444) -> f64 {
445 let avg_io = system_snapshots
447 .iter()
448 .map(|s| (s.io_metrics.disk_read_bps + s.io_metrics.disk_write_bps) as f64)
449 .sum::<f64>()
450 / system_snapshots.len().max(1) as f64;
451
452 if avg_io > 1024.0 * 1024.0 {
453 0.4
454 } else {
455 0.1
456 }
457}
458
459fn calculate_network_correlation(
460 _memory_analysis: &super::analysis::LockfreeAnalysis,
461 system_snapshots: &[SystemResourceSnapshot],
462) -> f64 {
463 let avg_network = system_snapshots
465 .iter()
466 .map(|s| (s.network_metrics.rx_bps + s.network_metrics.tx_bps) as f64)
467 .sum::<f64>()
468 / system_snapshots.len().max(1) as f64;
469
470 if avg_network > 1024.0 * 1024.0 {
471 0.3
472 } else {
473 0.1
474 }
475}
476
477#[cfg(test)]
478mod tests {
479 use super::*;
480 use std::time::Duration;
481 use tempfile::TempDir;
482
483 fn create_test_dir() -> TempDir {
484 tempfile::tempdir().expect("Failed to create temp directory")
485 }
486
487 #[test]
488 fn test_is_enhanced_profiling_active_initial() {
489 let _ = stop_system_profiling();
491
492 assert!(!is_enhanced_profiling_active());
494 }
495
496 #[test]
497 fn test_start_full_system_profiling() {
498 let temp_dir = create_test_dir();
499 let output_path = temp_dir.path().join("test_output");
500
501 let result = start_full_system_profiling(&output_path, Duration::from_millis(100));
502 assert!(result.is_ok());
503
504 assert!(is_enhanced_profiling_active());
506
507 assert!(output_path.exists());
509
510 let _ = stop_system_profiling();
512 }
513
514 #[test]
515 fn test_start_system_profiling_creates_directory() {
516 let temp_dir = create_test_dir();
517 let output_path = temp_dir.path().join("test_system_profiling");
518
519 assert!(!output_path.exists());
521
522 let result = start_full_system_profiling(&output_path, Duration::from_millis(200));
523 assert!(result.is_ok());
524
525 assert!(output_path.exists());
527
528 let _ = stop_system_profiling();
530 }
531
532 #[test]
533 fn test_start_system_profiling_cleans_existing_directory() {
534 let temp_dir = create_test_dir();
535 let output_path = temp_dir.path().join("test_cleanup");
536
537 std::fs::create_dir_all(&output_path).expect("Should be able to create output directory");
539 let test_file = output_path.join("existing_file.txt");
540 std::fs::write(&test_file, "test content").expect("Should be able to write test file");
541 assert!(test_file.exists());
542
543 let result = start_full_system_profiling(&output_path, Duration::from_millis(150));
545 assert!(result.is_ok());
546
547 assert!(!test_file.exists());
549 assert!(output_path.exists());
551
552 let _ = stop_system_profiling();
554 }
555
556 #[test]
557 fn test_stop_system_profiling_without_start() {
558 ENHANCED_PROFILING_ACTIVE.store(false, Ordering::SeqCst);
560 let result = stop_system_profiling();
561 assert!(result.is_ok());
562 assert!(!is_enhanced_profiling_active());
563 }
564
565 #[test]
566 fn test_stop_system_profiling_after_start() {
567 let temp_dir = create_test_dir();
568 let output_path = temp_dir.path().join("test_stop");
569
570 start_full_system_profiling(&output_path, Duration::from_millis(100)).unwrap();
572 assert!(is_enhanced_profiling_active());
573
574 let result = stop_system_profiling();
576 assert!(result.is_ok());
577 assert!(!is_enhanced_profiling_active());
578 }
579
580 #[test]
581 fn test_get_system_snapshot() {
582 let result = get_system_snapshot();
583 assert!(result.is_ok());
584
585 let snapshot = result.expect("System snapshot should be available");
586 assert!(snapshot.cpu_metrics.overall_usage >= 0.0);
588 assert!(snapshot.cpu_metrics.overall_usage <= 100.0);
589 }
593
594 #[test]
595 fn test_enhanced_profiling_state_management() {
596 let temp_dir = create_test_dir();
597 let output_path = temp_dir.path().join("test_state");
598
599 let _ = stop_system_profiling();
601 let _ = stop_system_profiling();
602
603 std::thread::sleep(Duration::from_millis(10));
605
606 let initial_state = is_enhanced_profiling_active();
608 if initial_state {
609 let _ = stop_system_profiling();
611 std::thread::sleep(Duration::from_millis(10));
612 }
613 if is_enhanced_profiling_active() {
616 eprintln!("Warning: Unable to achieve clean state due to test contamination in CI. Continuing test...");
617 }
618
619 start_full_system_profiling(&output_path, Duration::from_millis(100)).unwrap();
621 assert!(
622 is_enhanced_profiling_active(),
623 "Profiling should be active after start"
624 );
625
626 stop_system_profiling().expect("System profiling should stop successfully");
628 assert!(
629 !is_enhanced_profiling_active(),
630 "Profiling should be inactive after stop"
631 );
632 }
633
634 #[test]
635 fn test_system_snapshot_cpu_metrics() {
636 let snapshot = get_system_snapshot()
637 .expect("System snapshot should be available for CPU metrics test");
638
639 assert!(snapshot.cpu_metrics.overall_usage >= 0.0);
641 assert!(snapshot.cpu_metrics.overall_usage <= 100.0);
642 assert!(!snapshot.cpu_metrics.core_usage.is_empty());
643
644 for &usage in &snapshot.cpu_metrics.core_usage {
646 assert!((0.0..=100.0).contains(&usage));
647 }
648 }
649
650 #[test]
651 fn test_system_snapshot_memory_metrics() {
652 let snapshot = get_system_snapshot().unwrap();
653
654 if snapshot.memory_metrics.total_physical > 0 {
658 assert!(
660 snapshot.memory_metrics.used_physical <= snapshot.memory_metrics.total_physical
661 );
662 } else {
663 assert_eq!(snapshot.memory_metrics.total_physical, 0);
665 assert_eq!(snapshot.memory_metrics.used_physical, 0);
666 }
667 }
668
669 #[test]
670 fn test_system_snapshot_io_metrics() {
671 let _snapshot = get_system_snapshot().unwrap();
672
673 }
679
680 #[test]
681 fn test_system_snapshot_network_metrics() {
682 let _snapshot = get_system_snapshot().unwrap();
683
684 }
690
691 #[test]
692 fn test_profiling_with_different_intervals() {
693 let temp_dir = create_test_dir();
694 let output_path = temp_dir.path().join("test_intervals");
695
696 let result1 = start_full_system_profiling(&output_path, Duration::from_millis(50));
698 assert!(result1.is_ok());
699 stop_system_profiling().unwrap();
700
701 let result2 = start_full_system_profiling(&output_path, Duration::from_millis(1000));
703 assert!(result2.is_ok());
704 let _ = stop_system_profiling(); }
706
707 #[test]
708 fn test_global_state_isolation() {
709 let temp_dir = create_test_dir();
710 let output_path = temp_dir.path().join("test_isolation");
711
712 ENHANCED_PROFILING_ACTIVE.store(false, Ordering::SeqCst);
714 assert!(!is_enhanced_profiling_active());
715
716 start_full_system_profiling(&output_path, Duration::from_millis(100)).unwrap();
718 assert!(is_enhanced_profiling_active());
719
720 assert!(ENHANCED_OUTPUT_DIR.get().is_some());
722 assert!(SYSTEM_SNAPSHOTS.get().is_some());
723
724 let _ = stop_system_profiling(); assert!(!is_enhanced_profiling_active());
727 }
728
729 #[test]
730 fn test_macro_concept() {
731 let temp_dir = create_test_dir();
732 let _output_path = temp_dir.path().join("test_macro");
733
734 let result = {
736 let _data = vec![0u8; 1024];
737 42
738 };
739
740 assert_eq!(result, 42);
741 }
742
743 #[test]
744 fn test_start_system_monitoring() {
745 let result = start_system_monitoring(Duration::from_millis(100));
746 let _ = result;
748 }
749
750 #[test]
751 fn test_monitoring_concept() {
752 let temp_dir = create_test_dir();
753 let _output_path = temp_dir.path().join("monitoring_test");
754
755 assert!(temp_dir.path().exists());
758 }
759
760 #[test]
761 fn test_get_system_snapshot_multiple_calls() {
762 let snapshot1 = get_system_snapshot().unwrap();
764 let snapshot2 = get_system_snapshot().unwrap();
765
766 assert!(snapshot1.cpu_metrics.overall_usage >= 0.0);
768 assert!(snapshot2.cpu_metrics.overall_usage >= 0.0);
769
770 assert!(snapshot2.timestamp >= snapshot1.timestamp);
775 }
776
777 #[test]
778 fn test_report_generation_concept() {
779 let temp_dir = create_test_dir();
780 let _output_path = temp_dir.path().join("report_test");
781
782 assert!(temp_dir.path().exists());
785 }
786
787 #[test]
788 fn test_cpu_metrics_comprehensive() {
789 let snapshot = get_system_snapshot().unwrap();
790 let cpu = &snapshot.cpu_metrics;
791
792 assert!(cpu.overall_usage >= 0.0 && cpu.overall_usage <= 100.0);
794 assert!(!cpu.core_usage.is_empty());
795 for &usage in &cpu.core_usage {
800 assert!((0.0..=100.0).contains(&usage));
801 }
802 }
803
804 #[test]
805 fn test_memory_metrics_comprehensive() {
806 let snapshot = get_system_snapshot().unwrap();
807 let mem = &snapshot.memory_metrics;
808
809 if mem.total_physical > 0 {
811 assert!(mem.used_physical <= mem.total_physical);
813 assert!(mem.available_physical <= mem.total_physical);
814 } else {
815 assert_eq!(mem.total_physical, 0);
817 assert_eq!(mem.used_physical, 0);
818 assert_eq!(mem.available_physical, 0);
819 }
820
821 assert!(mem.available_virtual <= mem.total_virtual);
823 assert!(mem.pressure >= 0.0 && mem.pressure <= 100.0);
824 }
826
827 #[test]
828 fn test_process_metrics() {
829 let snapshot = get_system_snapshot().unwrap();
830 let proc = &snapshot.process_metrics;
831
832 assert!(proc.pid > 0);
834 assert!(!proc.name.is_empty());
835 assert!(proc.cpu_usage >= 0.0);
836 }
840
841 #[test]
842 fn test_thread_metrics() {
843 let snapshot = get_system_snapshot().unwrap();
844
845 assert!(!snapshot.thread_metrics.is_empty());
847
848 for (thread_id, thread_metric) in &snapshot.thread_metrics {
849 assert!(*thread_id > 0);
850 assert!(thread_metric.thread_id == *thread_id);
851 }
853 }
854
855 #[test]
856 fn test_gpu_metrics_optional() {
857 let snapshot = get_system_snapshot().unwrap();
858
859 if let Some(gpu) = &snapshot.gpu_metrics {
861 assert!(!gpu.device_name.is_empty());
862 assert!(gpu.gpu_usage >= 0.0 && gpu.gpu_usage <= 100.0);
863 assert!(gpu.memory_total > 0);
864 assert!(gpu.memory_used <= gpu.memory_total);
865 }
866 }
867
868 #[test]
869 fn test_correlation_calculations() {
870 use crate::lockfree::analysis::LockfreeAnalysis;
871
872 let snapshot = get_system_snapshot().unwrap();
873 let analysis = LockfreeAnalysis::new();
874 let snapshots = vec![snapshot];
875
876 let mem_cpu_corr = calculate_memory_cpu_correlation(&analysis, &snapshots);
878 let net_corr = calculate_network_correlation(&analysis, &snapshots);
879
880 assert!((-1.0..=1.0).contains(&mem_cpu_corr));
882 assert!((-1.0..=1.0).contains(&net_corr));
883 assert!(!mem_cpu_corr.is_nan());
884 assert!(!net_corr.is_nan());
885 }
886
887 #[test]
888 fn test_monitoring_lifecycle() {
889 let temp_dir = create_test_dir();
890 let _output_path = temp_dir.path().join("lifecycle_test");
891
892 let snapshot = get_system_snapshot();
895 assert!(snapshot.is_ok());
896 }
897
898 #[test]
899 fn test_system_snapshot_timestamps() {
900 let snapshot1 = get_system_snapshot().unwrap();
901
902 std::thread::sleep(Duration::from_millis(1));
904
905 let snapshot2 = get_system_snapshot().unwrap();
906
907 assert!(snapshot2.timestamp >= snapshot1.timestamp);
909 }
910
911 #[test]
912 fn test_multiple_start_stop_cycles() {
913 let temp_dir = create_test_dir();
914 let _output_path = temp_dir.path().join("cycles_test");
915
916 for _i in 0..3 {
918 let _ = start_system_monitoring(Duration::from_millis(10));
920 let _ = stop_system_monitoring();
921 }
922 }
923
924 #[test]
925 fn test_system_profiler_creation() {
926 use crate::lockfree::system_profiler::SystemProfiler;
927
928 let profiler = SystemProfiler::new(Duration::from_millis(100));
929 drop(profiler);
931 }
932
933 #[test]
934 fn test_error_handling_invalid_paths() {
935 let result = start_system_monitoring(Duration::from_millis(100));
937 let _ = result;
939
940 let result2 = stop_system_monitoring();
942 let _ = result2;
943 }
944
945 #[test]
946 fn test_system_correlation_analysis() {
947 use crate::lockfree::analysis::LockfreeAnalysis;
948
949 let snapshot = get_system_snapshot().unwrap();
950
951 let memory_utilization = if snapshot.memory_metrics.total_physical > 0 {
953 snapshot.memory_metrics.used_physical as f64
954 / snapshot.memory_metrics.total_physical as f64
955 } else {
956 0.0 };
958 let cpu_utilization = snapshot.cpu_metrics.overall_usage as f64 / 100.0;
959
960 assert!((0.0..=1.0).contains(&memory_utilization));
962 assert!((0.0..=1.0).contains(&cpu_utilization));
963
964 let analysis = LockfreeAnalysis::new();
966 let snapshots = vec![snapshot];
967 let correlation = calculate_memory_cpu_correlation(&analysis, &snapshots);
968 assert!((-1.0..=1.0).contains(&correlation));
969 }
970
971 #[test]
972 fn test_report_generation_with_real_data() {
973 use crate::lockfree::analysis::LockfreeAnalysis;
974
975 let temp_dir = create_test_dir();
976 let output_path = temp_dir.path().join("real_data_test");
977
978 let _data = vec![0u8; 1024];
980
981 let analysis = LockfreeAnalysis::new();
983 let snapshot = get_system_snapshot().unwrap();
984 let snapshots = vec![snapshot];
985
986 let corr_result = generate_system_correlation_report(&analysis, &snapshots, &output_path);
987 let html_result = generate_enhanced_system_html(&analysis, &snapshots, &output_path);
988
989 assert!(corr_result.is_ok());
991 assert!(html_result.is_ok());
992 }
993
994 #[test]
995 fn test_snapshot_consistency() {
996 let snapshot = get_system_snapshot().unwrap();
998
999 let mem = &snapshot.memory_metrics;
1001 assert!(
1002 mem.used_physical + mem.available_physical
1003 <= mem.total_physical + mem.total_physical / 10
1004 ); let cpu = &snapshot.cpu_metrics;
1008 if !cpu.core_usage.is_empty() {
1009 let avg_core_usage: f32 =
1010 cpu.core_usage.iter().sum::<f32>() / cpu.core_usage.len() as f32;
1011 let diff = (cpu.overall_usage - avg_core_usage).abs();
1013 assert!(diff <= 50.0); }
1015 }
1016
1017 #[test]
1018 fn test_concurrent_snapshots() {
1019 use std::thread;
1020
1021 let handles: Vec<_> = (0..4)
1022 .map(|_| {
1023 thread::spawn(|| {
1024 let snapshot = get_system_snapshot();
1025 assert!(snapshot.is_ok());
1026 snapshot.unwrap().timestamp
1027 })
1028 })
1029 .collect();
1030
1031 let timestamps: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
1032
1033 for ×tamp in ×tamps {
1035 assert!(timestamp > 0); }
1038 }
1039
1040 #[test]
1041 fn test_enhanced_api_integration() {
1042 use crate::lockfree::analysis::LockfreeAnalysis;
1043
1044 let temp_dir = create_test_dir();
1045 let output_path = temp_dir.path().join("integration_test");
1046
1047 let mut snapshots = Vec::new();
1049
1050 for _ in 0..3 {
1052 let snapshot = get_system_snapshot();
1053 assert!(snapshot.is_ok());
1054 snapshots.push(snapshot.unwrap());
1055 std::thread::sleep(Duration::from_millis(10));
1056 }
1057
1058 let analysis = LockfreeAnalysis::new();
1060 assert!(generate_system_correlation_report(&analysis, &snapshots, &output_path).is_ok());
1061 assert!(generate_enhanced_system_html(&analysis, &snapshots, &output_path).is_ok());
1062 }
1063}