scirs2-core 0.4.2

Core utilities and common functionality for SciRS2 (scirs2-core)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//! Enhanced Memory Metrics System Example
//!
//! This example demonstrates the comprehensive memory monitoring, analysis,
//! and profiling capabilities of the enhanced memory metrics system.

#[cfg(not(feature = "memory_management"))]
#[allow(dead_code)]
fn main() {
    println!("This example requires the 'memory_management' feature to be enabled.");
    println!("Run with: cargo run --example enhanced_memory_metrics_example --features memory_management");
}

#[cfg(feature = "memory_management")]
use chrono::Utc;
#[cfg(feature = "memory_management")]
use scirs2_core::memory::metrics::{
    LeakDetectionConfig, MemoryAnalytics, MemoryEvent, MemoryEventType, MemoryProfiler,
    MemoryProfilerConfig, RiskAssessment,
};
#[cfg(feature = "memory_management")]
use std::thread;
#[cfg(feature = "memory_management")]
use std::time::{Duration, Instant};

#[cfg(feature = "memory_management")]
#[allow(dead_code)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Enhanced Memory Metrics System Demonstration ===");

    // Demo 1: Basic Memory Analytics
    demo_memory_analytics()?;

    // Demo 2: Memory Leak Detection
    demo_leak_detection()?;

    // Demo 3: Memory Profiler with Real-time Monitoring
    demo_memory_profiler()?;

    // Demo 4: Pattern Analysis and Optimization Recommendations
    demo_pattern_analysis()?;

    // Demo 5: Performance Impact Analysis
    demo_performance_analysis()?;

    println!("\n=== Enhanced Memory Metrics Demo Complete ===");
    Ok(())
}

/// Demonstrate basic memory analytics capabilities
#[cfg(feature = "memory_management")]
#[allow(dead_code)]
fn demo_memory_analytics() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n1. Basic Memory Analytics");
    println!("=========================");

    let mut analytics = MemoryAnalytics::new(LeakDetectionConfig::default());

    // Simulate memory allocation patterns
    let components = ["DataProcessor", "NetworkClient", "Cache"];
    let mut addresses = [0x1000, 0x2000, 0x3000];

    println!("Simulating memory allocation patterns...");

    for i in 0..50 {
        for (idx, component) in components.iter().enumerate() {
            let size = 1024 + (i * 100) + (idx * 512);
            let address = addresses[idx];

            // Record allocation
            let event = MemoryEvent::new(MemoryEventType::Allocation, *component, size, address);
            analytics.record_event(event);

            // Update address for next allocation
            addresses[idx] += size;

            // Occasionally deallocate some memory
            if i % 10 == 0 && i > 0 {
                let dealloc_size = size / 2;
                let dealloc_event = MemoryEvent::new(
                    MemoryEventType::Deallocation,
                    *component,
                    dealloc_size,
                    address,
                );
                analytics.record_event(dealloc_event);
            }
        }

        // Small delay to simulate time passing
        thread::sleep(Duration::from_millis(10));
    }

    // Analyze patterns for each component
    for component in &components {
        if let Some(analysis) = analytics.analyze_patterns(component) {
            println!("\nAnalysis for {}:", component);
            println!("  Patterns detected: {}", analysis.patterns.len());
            println!(
                "  Allocation frequency: {:.2}/sec",
                analysis.efficiency.allocation_frequency
            );
            println!(
                "  Memory reuse ratio: {:.2}",
                analysis.efficiency.reuse_ratio
            );
            println!(
                "  Fragmentation estimate: {:.2}",
                analysis.efficiency.fragmentation_estimate
            );
            println!("  Issues found: {}", analysis.potential_issues.len());
            println!("  Recommendations: {}", analysis.recommendations.len());

            // Show specific recommendations
            for (i, recommendation) in analysis.recommendations.iter().enumerate() {
                match recommendation {
                    scirs2_core::memory::metrics::OptimizationRecommendation::UseBufferPooling { expected_savings, .. } => {
                        println!("    {}. Use buffer pooling (saves ~{} bytes)", i + 1, expected_savings);
                    }
                    scirs2_core::memory::metrics::OptimizationRecommendation::BatchAllocations { suggested_batch_size, .. } => {
                        println!("    {}. Batch allocations (batch size: {})", i + 1, suggested_batch_size);
                    }
                    scirs2_core::memory::metrics::OptimizationRecommendation::PreAllocateMemory { suggested_size, .. } => {
                        println!("    {}. Pre-allocate {} bytes", i + 1, suggested_size);
                    }
                    _ => {
                        println!("    {}. Other optimization available", i + 1);
                    }
                }
            }
        }
    }

    Ok(())
}

/// Demonstrate memory leak detection
#[cfg(feature = "memory_management")]
#[allow(dead_code)]
fn demo_leak_detection() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n2. Memory Leak Detection");
    println!("========================");

    let mut analytics = MemoryAnalytics::new(LeakDetectionConfig {
        leak_threshold_bytes: 1024, // Lower threshold for demo
        leak_threshold_duration: Duration::from_secs(1),
        growth_rate_threshold: 500.0, // 500 bytes/sec
        ..Default::default()
    });

    // Simulate a component with a memory leak
    println!("Simulating memory leak in 'LeakyComponent'...");

    let mut address = 0x10000;
    for i in 0..30 {
        let size = 1000 + (i * 50); // Growing allocation size

        let event = MemoryEvent::new(MemoryEventType::Allocation, "LeakyComponent", size, address);
        analytics.record_event(event);

        address += size;
        thread::sleep(Duration::from_millis(100));
    }

    // Also simulate a healthy component for comparison
    println!("Simulating healthy component 'HealthyComponent'...");

    let mut healthy_address = 0x20000;
    for i in 0..30 {
        let size = 1000; // Constant allocation size

        // Allocate
        let alloc_event = MemoryEvent::new(
            MemoryEventType::Allocation,
            "HealthyComponent",
            size,
            healthy_address,
        );
        analytics.record_event(alloc_event);

        // Deallocate after a few iterations
        if i > 5 {
            let dealloc_event = MemoryEvent::new(
                MemoryEventType::Deallocation,
                "HealthyComponent",
                size,
                healthy_address - (5 * size),
            );
            analytics.record_event(dealloc_event);
        }

        healthy_address += size;
        thread::sleep(Duration::from_millis(100));
    }

    // Perform leak detection
    let leak_results = analytics.get_leak_detection_results();

    println!("\nLeak Detection Results:");
    for result in leak_results {
        println!("\nComponent: {}", result.component);
        println!("  Leak detected: {}", result.leak_detected);
        println!("  Growth rate: {:.2} bytes/sec", result.growth_rate);
        println!("  Confidence: {:.2}", result.confidence);
        println!("  Current usage: {} bytes", result.current_usage);

        if result.leak_detected {
            println!("  🚨 LEAK ALERT! 🚨");
            println!(
                "  Projected usage in 1 hour: {} bytes",
                result.projected_usage_1h
            );
            println!(
                "  Projected usage in 24 hours: {} bytes",
                result.projected_usage_24h
            );
        } else {
            println!("  ✅ No leak detected");
        }
    }

    Ok(())
}

/// Demonstrate memory profiler with real-time monitoring
#[cfg(feature = "memory_management")]
#[allow(dead_code)]
fn demo_memory_profiler() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n3. Memory Profiler with Real-time Monitoring");
    println!("===========================================");

    let config = MemoryProfilerConfig {
        enabled: true,
        profiling_interval: Duration::from_secs(2),
        auto_leak_detection: true,
        auto_recommendations: true,
        save_to_file: false,
        ..Default::default()
    };

    let profiler = MemoryProfiler::new(config);

    // Start profiling session
    let sessionid =
        profiler.start_session(Some(format!("demo_session_{}", Utc::now().timestamp())));
    println!("Started profiling session: {}", sessionid);

    // Simulate various memory operations
    println!("Simulating memory operations...");

    let components = ["WebServer", "Database", "Cache", "Logger"];
    let mut addresses: Vec<usize> = (0..components.len())
        .map(|i| 0x10000 + (i * 0x10000))
        .collect();

    for iteration in 0..20 {
        for (idx, component) in components.iter().enumerate() {
            // Different allocation patterns for each component
            let size = match *component {
                "WebServer" => 2048 + (iteration * 100), // Growing
                "Database" => 8192,                      // Constant
                "Cache" => {
                    if iteration % 3 == 0 {
                        16384
                    } else {
                        0
                    }
                } // Periodic
                "Logger" => 512 + (iteration * iteration * 10), // Accelerating
                _ => 1024,
            };

            if size > 0 {
                let event = MemoryEvent::new(
                    MemoryEventType::Allocation,
                    *component,
                    size,
                    addresses[idx],
                );
                profiler.record_event(event);
                addresses[idx] += size;
            }

            // Occasional deallocations
            if iteration % 5 == 0 && iteration > 0 {
                let dealloc_size = size / 2;
                if dealloc_size > 0 {
                    let dealloc_event = MemoryEvent::new(
                        MemoryEventType::Deallocation,
                        *component,
                        dealloc_size,
                        addresses[idx] - size,
                    );
                    profiler.record_event(dealloc_event);
                }
            }
        }

        // Check health status periodically
        if iteration % 5 == 0 {
            let health = profiler.health_check();
            println!(
                "Health check at iteration {}: Score {:.2}",
                iteration, health.health_score
            );

            match health.risk_assessment {
                RiskAssessment::Low => println!("  Status: 🟢 Low risk"),
                RiskAssessment::Medium { ref issues } => {
                    println!("  Status: 🟡 Medium risk ({} issues)", issues.len());
                }
                RiskAssessment::High {
                    ref critical_issues,
                } => {
                    println!(
                        "  Status: 🔴 High risk ({} critical issues)",
                        critical_issues.len()
                    );
                }
            }
        }

        thread::sleep(Duration::from_millis(200));
    }

    // End profiling session and get results
    println!("\nEnding profiling session...");
    if let Some(result) = profiler.end_session() {
        println!("\nProfiling Results:");
        println!("==================");
        println!("Session ID: {}", result.session.id);
        println!("Duration: {} micros", result.session.duration_micros);
        println!("Events recorded: {}", result.session.event_count);
        println!("Components tracked: {}", result.session.component_count);
        println!(
            "Peak memory usage: {} bytes ({:.2} MB)",
            result.session.peak_memory_usage,
            result.session.peak_memory_usage as f64 / (1024.0 * 1024.0)
        );
        println!("Leaks detected: {}", result.session.leaks_detected);

        println!("\nPerformance Impact:");
        println!(
            "  Total allocation time: {:?}",
            result.performance_impact.total_allocation_time
        );
        println!(
            "  Performance bottlenecks: {}",
            result.performance_impact.performance_bottlenecks
        );
        println!(
            "  Memory bandwidth utilization: {:.2}%",
            result.performance_impact.memorybandwidth_utilization * 100.0
        );

        println!("\nSummary:");
        println!("  Health score: {:.2}/1.0", result.summary.health_score);
        println!("  Key insights: {}", result.summary.key_insights.len());
        for insight in &result.summary.key_insights {
            println!("{}", insight);
        }

        println!(
            "  Priority recommendations: {}",
            result.summary.priority_recommendations.len()
        );
        for recommendation in &result.summary.priority_recommendations {
            println!("{}", recommendation);
        }
    }

    Ok(())
}

/// Demonstrate pattern analysis and optimization recommendations
#[cfg(feature = "memory_management")]
#[allow(dead_code)]
fn demo_pattern_analysis() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n4. Pattern Analysis and Optimization Recommendations");
    println!("===================================================");

    let mut analytics = MemoryAnalytics::new(LeakDetectionConfig::default());

    // Simulate different allocation patterns
    println!("Simulating various allocation patterns...");

    // Pattern 1: Burst allocations (e.g., batch processing)
    println!("  Simulating burst allocation pattern...");
    for burst in 0..3 {
        // Burst phase
        for i in 0..10 {
            let size = 16384; // 16KB allocations
            let event = MemoryEvent::new(
                MemoryEventType::Allocation,
                "BatchProcessor",
                size,
                0x100000 + (burst * 200000) + (i * size),
            );
            analytics.record_event(event);
            thread::sleep(Duration::from_millis(20));
        }

        // Quiet phase
        thread::sleep(Duration::from_millis(500));

        // Deallocate some memory
        for i in 0..5 {
            let event = MemoryEvent::new(
                MemoryEventType::Deallocation,
                "BatchProcessor",
                16384,
                0x100000 + (burst * 200000) + (i * 16384),
            );
            analytics.record_event(event);
            thread::sleep(Duration::from_millis(10));
        }
    }

    // Pattern 2: Periodic cycles (e.g., garbage collection)
    println!("  Simulating periodic allocation cycles...");
    for cycle in 0..5 {
        // Growth phase
        for i in 0..8 {
            let size = 4096 + (i * 512);
            let event = MemoryEvent::new(
                MemoryEventType::Allocation,
                "ManagedHeap",
                size,
                0x200000 + (cycle * 100000) + (i * size),
            );
            analytics.record_event(event);
            thread::sleep(Duration::from_millis(50));
        }

        // Collection phase (deallocate most memory)
        for i in 0..6 {
            let size = 4096 + (i * 512);
            let event = MemoryEvent::new(
                MemoryEventType::Deallocation,
                "ManagedHeap",
                size,
                0x200000 + (cycle * 100000) + (i * size),
            );
            analytics.record_event(event);
            thread::sleep(Duration::from_millis(25));
        }
    }

    // Pattern 3: Steady growth (potential leak)
    println!("  Simulating steady growth pattern...");
    for i in 0..30 {
        let size = 2048 + (i * 64); // Steadily increasing
        let event = MemoryEvent::new(
            MemoryEventType::Allocation,
            "GrowingCache",
            size,
            0x300000 + (i * size),
        );
        analytics.record_event(event);
        thread::sleep(Duration::from_millis(100));
    }

    // Analyze patterns
    let components = ["BatchProcessor", "ManagedHeap", "GrowingCache"];

    for component in &components {
        if let Some(analysis) = analytics.analyze_patterns(component) {
            println!("\n📊 Pattern Analysis for {}:", component);
            println!("   Detected Patterns:");

            for (i, pattern) in analysis.patterns.iter().enumerate() {
                match pattern {
                    scirs2_core::memory::metrics::AllocationPattern::SteadyGrowth {
                        rate,
                        confidence,
                    } => {
                        println!(
                            "     {}. Steady Growth: {:.2} bytes/sec (confidence: {:.2})",
                            i + 1,
                            rate,
                            confidence
                        );
                    }
                    scirs2_core::memory::metrics::AllocationPattern::PeriodicCycle {
                        cycle_duration,
                        peak_size,
                        confidence,
                    } => {
                        println!("     {}. Periodic Cycle: {:?} cycles, peak {} bytes (confidence: {:.2})", 
                                i + 1, cycle_duration, peak_size, confidence);
                    }
                    scirs2_core::memory::metrics::AllocationPattern::BurstAllocation {
                        burst_size,
                        burst_duration,
                        confidence,
                    } => {
                        println!(
                            "     {}. Burst Allocation: {} bytes in {:?} (confidence: {:.2})",
                            i + 1,
                            burst_size,
                            burst_duration,
                            confidence
                        );
                    }
                    scirs2_core::memory::metrics::AllocationPattern::Plateau {
                        size,
                        duration,
                        confidence,
                    } => {
                        println!(
                            "     {}. Plateau: {} bytes for {:?} (confidence: {:.2})",
                            i + 1,
                            size,
                            duration,
                            confidence
                        );
                    }
                }
            }

            println!("   Efficiency Metrics:");
            println!("     Reuse ratio: {:.2}", analysis.efficiency.reuse_ratio);
            println!(
                "     Allocation frequency: {:.2}/sec",
                analysis.efficiency.allocation_frequency
            );
            println!(
                "     Avg allocation lifetime: {:?}",
                analysis.efficiency.avg_allocation_lifetime
            );
            println!(
                "     Fragmentation estimate: {:.2}",
                analysis.efficiency.fragmentation_estimate
            );

            if !analysis.potential_issues.is_empty() {
                println!("   🚨 Potential Issues:");
                for (i, issue) in analysis.potential_issues.iter().enumerate() {
                    match issue {
                        scirs2_core::memory::metrics::MemoryIssue::MemoryLeak {
                            growth_rate,
                            duration,
                            severity,
                        } => {
                            println!("     {}. Memory Leak: {:.2} bytes/sec growth over {:?} (severity: {:.2})", 
                                    i + 1, growth_rate, duration, severity);
                        }
                        scirs2_core::memory::metrics::MemoryIssue::HighAllocationFrequency {
                            frequency,
                            impact,
                        } => {
                            println!(
                                "     {}. High Allocation Frequency: {:.2}/sec - {}",
                                i + 1,
                                frequency,
                                impact
                            );
                        }
                        scirs2_core::memory::metrics::MemoryIssue::HighPeakUsage {
                            peak_size,
                            ..
                        } => {
                            println!(
                                "     {}. High Peak Usage: {} bytes ({:.2} MB)",
                                i + 1,
                                peak_size,
                                *peak_size as f64 / (1024.0 * 1024.0)
                            );
                        }
                        scirs2_core::memory::metrics::MemoryIssue::MemoryFragmentation {
                            fragmentation_ratio,
                            potential_waste,
                        } => {
                            println!("     {}. Memory Fragmentation: {:.2} ratio, {} bytes potential waste", 
                                    i + 1, fragmentation_ratio, potential_waste);
                        }
                        scirs2_core::memory::metrics::MemoryIssue::IneffientBufferPool {
                            efficiency,
                            pool_misses,
                        } => {
                            println!(
                                "     {}. Inefficient Buffer Pool: {:.2} efficiency, {} misses",
                                i + 1,
                                efficiency,
                                pool_misses
                            );
                        }
                    }
                }
            }

            if !analysis.recommendations.is_empty() {
                println!("   💡 Optimization Recommendations:");
                for (i, recommendation) in analysis.recommendations.iter().enumerate() {
                    match recommendation {
                        scirs2_core::memory::metrics::OptimizationRecommendation::UseBufferPooling { expected_savings, suggested_poolsizes } => {
                            println!("     {}. Use Buffer Pooling: Save ~{} bytes, pools: {:?}", 
                                    i + 1, expected_savings, suggested_poolsizes);
                        }
                        scirs2_core::memory::metrics::OptimizationRecommendation::BatchAllocations { current_frequency, suggested_batch_size } => {
                            println!("     {}. Batch Allocations: Reduce from {:.2}/sec, batch size: {}", 
                                    i + 1, current_frequency, suggested_batch_size);
                        }
                        scirs2_core::memory::metrics::OptimizationRecommendation::PreAllocateMemory { suggested_size, performance_gain } => {
                            println!("     {}. Pre-allocate {} bytes: {}", 
                                    i + 1, suggested_size, performance_gain);
                        }
                        scirs2_core::memory::metrics::OptimizationRecommendation::UseMemoryEfficientStructures { current_type, suggested_alternative, memory_reduction } => {
                            println!("     {}. Use {} instead of {}: Save {} bytes", 
                                    i + 1, suggested_alternative, current_type, memory_reduction);
                        }
                        scirs2_core::memory::metrics::OptimizationRecommendation::ImplementCompaction { fragmentation_reduction, suggested_frequency } => {
                            println!("     {}. Implement Compaction: {:.2} reduction every {:?}", 
                                    i + 1, fragmentation_reduction, suggested_frequency);
                        }
                    }
                }
            }
        }
    }

    Ok(())
}

/// Demonstrate performance impact analysis
#[cfg(feature = "memory_management")]
#[allow(dead_code)]
fn demo_performance_analysis() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n5. Performance Impact Analysis");
    println!("==============================");

    let profiler = MemoryProfiler::new(MemoryProfilerConfig {
        enabled: false, // Disable background monitoring for this demo
        ..Default::default()
    });

    let sessionid = profiler.start_session(Some("performance_analysis".to_string()));
    println!("Started performance analysis session: {}", sessionid);

    // Simulate high-frequency allocation patterns that impact performance
    println!("Simulating performance-impacting allocation patterns...");

    let start_time = Instant::now();
    let mut allocation_count = 0;

    // High-frequency small allocations (performance impact)
    println!("  Phase 1: High-frequency small allocations");
    for i in 0..1000 {
        let size = 64 + (i % 128); // Small, variable size allocations
        let event = MemoryEvent::new(
            MemoryEventType::Allocation,
            "HighFrequencyComponent",
            size,
            0x400000 + (i * size),
        );
        profiler.record_event(event);
        allocation_count += 1;

        // Very short sleep to simulate high frequency
        thread::sleep(Duration::from_micros(100));
    }

    // Large allocations (memory pressure)
    println!("  Phase 2: Large allocations causing memory pressure");
    for i in 0..50 {
        let size = 1024 * 1024 + (i * 1024 * 512); // 1MB+ allocations
        let event = MemoryEvent::new(
            MemoryEventType::Allocation,
            "LargeAllocationComponent",
            size,
            0x800000 + (i * size),
        );
        profiler.record_event(event);
        allocation_count += 1;

        thread::sleep(Duration::from_millis(50));
    }

    // Fragmented allocation pattern
    println!("  Phase 3: Fragmented allocation pattern");
    for i in 0..200 {
        // Alternate between small and large allocations
        let size = if i % 2 == 0 { 128 } else { 8192 };
        let event = MemoryEvent::new(
            MemoryEventType::Allocation,
            "FragmentedComponent",
            size,
            0xC00000 + (i * 10000),
        );
        profiler.record_event(event);
        allocation_count += 1;

        // Deallocate every 4th allocation to create fragmentation
        if i % 4 == 0 && i > 0 {
            let dealloc_event = MemoryEvent::new(
                MemoryEventType::Deallocation,
                "FragmentedComponent",
                size,
                0xC00000 + ((i - 4) * 10000),
            );
            profiler.record_event(dealloc_event);
        }

        thread::sleep(Duration::from_millis(10));
    }

    let total_time = start_time.elapsed();

    // End session and analyze performance impact
    if let Some(result) = profiler.end_session() {
        println!("\n📈 Performance Impact Analysis Results:");
        println!("======================================");

        println!("Allocation Performance:");
        println!("  Total allocations: {}", allocation_count);
        println!("  Total time: {:?}", total_time);
        println!(
            "  Allocation rate: {:.2}/sec",
            allocation_count as f64 / total_time.as_secs_f64()
        );
        println!(
            "  Average allocation time: {:?}",
            result.performance_impact.avg_allocation_time
        );
        println!(
            "  Total allocation overhead: {:?}",
            result.performance_impact.total_allocation_time
        );

        println!("\nMemory System Impact:");
        println!(
            "  Performance bottlenecks detected: {}",
            result.performance_impact.performance_bottlenecks
        );
        println!(
            "  Memory bandwidth utilization: {:.2}%",
            result.performance_impact.memorybandwidth_utilization * 100.0
        );
        println!(
            "  Cache miss estimate: {:.2}%",
            result.performance_impact.cache_miss_estimate * 100.0
        );

        println!("\nMemory Usage Summary:");
        println!(
            "  Peak memory usage: {:.2} MB",
            result.session.peak_memory_usage as f64 / (1024.0 * 1024.0)
        );
        println!("  Components tracked: {}", result.session.component_count);

        // Show component-specific analysis
        println!("\nPer-Component Analysis:");
        for (component, stats) in &result.memory_report.component_stats {
            println!("  {}:", component);
            println!(
                "    Current usage: {:.2} KB",
                stats.current_usage as f64 / 1024.0
            );
            println!("    Peak usage: {:.2} KB", stats.peak_usage as f64 / 1024.0);
            println!("    Allocations: {}", stats.allocation_count);
            println!(
                "    Avg allocation size: {:.0} bytes",
                stats.avg_allocation_size
            );

            // Calculate efficiency metrics
            let reuse_ratio = if stats.peak_usage > 0 {
                stats.total_allocated as f64 / stats.peak_usage as f64
            } else {
                0.0
            };
            println!("    Memory reuse ratio: {:.2}", reuse_ratio);

            if stats.allocation_count > 0 {
                let alloc_rate = stats.allocation_count as f64 / total_time.as_secs_f64();
                println!("    Allocation rate: {:.2}/sec", alloc_rate);

                if alloc_rate > 100.0 {
                    println!("    ⚠️  High allocation frequency may impact performance");
                }
            }
        }

        // Overall recommendations
        println!("\n💡 Performance Optimization Recommendations:");
        if result.performance_impact.performance_bottlenecks > 0 {
            println!("  • Reduce allocation frequency using buffer pooling");
            println!("  • Batch small allocations to reduce overhead");
        }

        if result.performance_impact.cache_miss_estimate > 0.3 {
            println!("  • Optimize data locality to reduce cache misses");
            println!("  • Consider memory-efficient data structures");
        }

        if result.session.peak_memory_usage > 100 * 1024 * 1024 {
            println!("  • Monitor memory usage to prevent excessive consumption");
            println!("  • Implement memory usage limits and monitoring");
        }

        println!("  • Profile actual runtime performance to validate optimizations");
    }

    Ok(())
}