jbuild 0.1.9

High-performance Java build tool supporting Maven and Gradle
Documentation
//! Performance tests for interactive functionality
//!
//! These tests verify that the interactive features perform well and don't degrade system performance

use std::time::Instant;
use std::thread;
use std::sync::Arc;
use std::sync::mpsc;
use jbuild::interactive::*;
use jbuild::build::BuildSystem;

#[test]
fn test_metrics_collection_performance() {
    println!("๐Ÿงช Testing metrics collection performance...");
    
    let start_time = Instant::now();
    let mut metrics = BuildMetrics::new();
    
    // Simulate rapid metrics updates
    for i in 0..1000 {
        metrics.processed_dependencies = i;
        metrics.total_dependencies = 1000;
        metrics.memory_usage_mb = 512 + (i as f64 * 0.1) as u64;
        metrics.cpu_usage_percent = (25.0 + (i as f64 * 0.05)) as f32;
        
        // Add phase timing
        if i % 100 == 0 {
            metrics.add_phase_time(format!("Phase {}", i), std::time::Duration::from_millis(10));
        }
    }
    
    let elapsed = start_time.elapsed();
    println!("Metrics collection for 1000 updates took: {:?}", elapsed);
    
    // Should be very fast (less than 100ms for 1000 updates)
    assert!(elapsed.as_millis() < 100, "Metrics collection should be fast");
    
    // Verify metrics were updated correctly
    assert_eq!(metrics.processed_dependencies, 999);
    assert_eq!(metrics.total_dependencies, 1000);
    assert_eq!(metrics.phase_times.len(), 10);
    
    println!("โœ… Metrics collection performance test passed");
}

#[test]
fn test_dashboard_rendering_performance() {
    println!("๐Ÿงช Testing dashboard rendering performance...");
    
    let mut metrics = BuildMetrics::new();
    let start_time = Instant::now();
    
    // Test multiple dashboard renders
    for i in 0..100 {
        let _result = display_dashboard(&metrics, &start_time, BuildSystem::Maven);
        
        // Update metrics slightly
        metrics.processed_dependencies = i % 100;
        metrics.memory_usage_mb = 512 + (i as f64 * 0.1) as u64;
    }
    
    let elapsed = start_time.elapsed();
    println!("100 dashboard renders took: {:?}", elapsed);
    
    // Should be fast (less than 500ms for 100 renders)
    assert!(elapsed.as_millis() < 500, "Dashboard rendering should be fast");
    
    println!("โœ… Dashboard rendering performance test passed");
}

#[test]
fn test_monitoring_throughput() {
    println!("๐Ÿงช Monitoring throughput performance test...");
    
    let mut metrics = BuildMetrics::new();
    let start_time = Instant::now();
    
    // Simulate high-frequency monitoring
    for i in 0..100 {
        // Update metrics
        metrics.processed_dependencies = i;
        metrics.total_dependencies = 100;
        metrics.memory_usage_mb = 512 + (i as f64 * 0.1) as u64;
        metrics.cpu_usage_percent = (25.0 + (i as f64 * 0.5)) as f32;
        
        // Simulate monitor display
        let _result = display_monitor(&metrics, &start_time, false);
        
        thread::sleep(std::time::Duration::from_millis(5));
    }
    
    let elapsed = start_time.elapsed();
    let updates_per_second = 100.0 / elapsed.as_secs_f64();
    println!("Monitoring throughput: {:.1} updates/second", updates_per_second);
    
    // Should maintain reasonable throughput (at least 10 updates per second)
    assert!(updates_per_second > 10.0, "Should maintain good monitoring throughput");
    
    println!("โœ… Monitoring throughput test passed");
}

#[test]
fn test_concurrent_monitoring_performance() {
    println!("๐Ÿงช Testing concurrent monitoring performance...");
    
    let start_time = Instant::now();
    
    let mut handles = vec![];
    
    // Spawn multiple monitoring threads
    for _thread_id in 0..5 {
        let start_time_clone = start_time.clone();
        
        let handle = thread::spawn(move || {
            let mut metrics = BuildMetrics::new();
            for i in 0..50 {
                metrics.processed_dependencies = i;
                metrics.memory_usage_mb = 512 + (i as f64 * 0.1) as u64;
                
                // Simulate monitoring display
                let _result = display_monitor(&metrics, &start_time_clone, false);
                
                thread::sleep(std::time::Duration::from_millis(10));
            }
        });
        
        handles.push(handle);
    }
    
    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }
    
    let elapsed = start_time.elapsed();
    println!("Concurrent monitoring completed in: {:?}", elapsed);
    
    // Should complete within a reasonable time (less than 5 seconds)
    assert!(elapsed.as_secs() < 5, "Concurrent monitoring should be fast");
    
    println!("โœ… Concurrent monitoring performance test passed");
}

#[test]
fn test_large_project_metrics_performance() {
    println!("๐Ÿงช Testing large project metrics performance...");
    
    let mut metrics = BuildMetrics::new();
    let start_time = Instant::now();
    
    // Simulate large project with many dependencies
    for i in 0..5000 {
        metrics.processed_dependencies = i;
        metrics.total_dependencies = 5000;
        metrics.memory_usage_mb = 512 + (i as f64 * 0.1) as u64;
        metrics.cpu_usage_percent = (25.0 + (i as f64 * 0.02)) as f32;
        
        // Add phase timing periodically
        if i % 500 == 0 {
            metrics.add_phase_time(
                format!("Large Phase {}", i), 
                std::time::Duration::from_millis(100)
            );
        }
    }
    
    let elapsed = start_time.elapsed();
    println!("Large project metrics processing took: {:?}", elapsed);
    
    // Should handle large projects efficiently (less than 1 second)
    assert!(elapsed.as_millis() < 1000, "Large project metrics should be fast");
    
    // Verify metrics were processed correctly
    assert_eq!(metrics.processed_dependencies, 4999);
    assert_eq!(metrics.total_dependencies, 5000);
    assert_eq!(metrics.phase_times.len(), 10);
    
    println!("โœ… Large project metrics performance test passed");
}

#[test]
fn test_memory_usage_tracking_performance() {
    println!("๐Ÿงช Testing memory usage tracking performance...");
    
    let mut metrics = BuildMetrics::new();
    let start_time = Instant::now();
    
    // Test memory tracking performance
    for i in 0..10000 {
        metrics.processed_dependencies = i;
        metrics.memory_usage_mb = 512 + (i as f64 * 0.1) as u64;
        
        // Simulate memory-heavy operations
        if i % 1000 == 0 {
            metrics.add_phase_time(
                format!("Memory Phase {}", i), 
                std::time::Duration::from_millis(50)
            );
        }
    }
    
    let elapsed = start_time.elapsed();
    println!("Memory tracking for 10000 updates took: {:?}", elapsed);
    
    // Should be very efficient (less than 500ms)
    assert!(elapsed.as_millis() < 500, "Memory tracking should be efficient");
    
    println!("โœ… Memory usage tracking performance test passed");
}

#[test]
fn test_profile_generation_performance() {
    println!("๐Ÿงช Testing profile generation performance...");
    
    let report = ProfileReport {
        build_time: std::time::Duration::from_secs(10),
        success: true,
        error_count: 5,
        phases: (0..100).map(|i| {
            let phase_name = format!("Phase {}", i);
            let duration = std::time::Duration::from_millis((i * 1000) as u64);
            (phase_name, duration)
        }).collect(),
        timestamp: chrono::Local::now(),
    };
    
    let start_time = Instant::now();
    
    // Test profile generation performance
    for _ in 0..100 {
        let _markdown = generate_markdown_report(&report);
        let _text = generate_text_report(&report);
    }
    
    let elapsed = start_time.elapsed();
    println!("100 profile generations took: {:?}", elapsed);
    
    // Should be fast (less than 1 second)
    assert!(elapsed.as_millis() < 1000, "Profile generation should be fast");
    
    println!("โœ… Profile generation performance test passed");
}

#[test]
fn test_progress_simulation_performance() {
    println!("๐Ÿงช Testing progress simulation performance...");
    
    let (tx, rx) = mpsc::channel();
    let start_time = Instant::now();
    
    // Test progress simulation with high frequency
    for i in 0..10000 {
        tx.send(format!("Progress update {}", i)).unwrap();
    }
    
    // Process all messages
    let mut count = 0;
    while let Ok(_message) = rx.try_recv() {
        count += 1;
    }
    
    let elapsed = start_time.elapsed();
    println!("Progress simulation for {} messages took: {:?}", count, elapsed);
    
    // Should be fast (less than 500ms)
    assert!(elapsed.as_millis() < 500, "Progress simulation should be fast");
    assert_eq!(count, 10000, "Should process all messages");
    
    println!("โœ… Progress simulation performance test passed");
}

#[test]
fn test_cpu_usage_tracking_performance() {
    println!("๐Ÿงช Testing CPU usage tracking performance...");
    
    let mut metrics = BuildMetrics::new();
    let start_time = Instant::now();
    
    // Test CPU tracking with many updates
    for i in 0..5000 {
        metrics.processed_dependencies = i;
        metrics.cpu_usage_percent = (25.0 + (i as f64 * 0.01)).min(95.0) as f32;
        
        // Simulate varying CPU load
        if i % 1000 == 0 {
            metrics.add_phase_time(
                format!("CPU Load Phase {}", i), 
                std::time::Duration::from_millis(200)
            );
        }
    }
    
    let elapsed = start_time.elapsed();
    println!("CPU tracking for 5000 updates took: {:?}", elapsed);
    
    // Should be efficient (less than 1 second)
    assert!(elapsed.as_millis() < 1000, "CPU tracking should be efficient");
    
    // Verify CPU values are within bounds
    assert!(metrics.cpu_usage_percent <= 100.0);
    assert!(metrics.cpu_usage_percent >= 0.0);
    
    println!("โœ… CPU usage tracking performance test passed");
}

#[test]
fn test_phase_timing_performance() {
    println!("๐Ÿงช Testing phase timing performance...");
    
    let mut metrics = BuildMetrics::new();
    let start_time = Instant::now();
    
    // Test phase timing with many phases
    for i in 0..1000 {
        let phase_duration = std::time::Duration::from_millis((i % 1000) as u64);
        metrics.add_phase_time(
            format!("Performance Test Phase {}", i), 
            phase_duration
        );
    }
    
    let elapsed = start_time.elapsed();
    println!("Phase timing for 1000 phases took: {:?}", elapsed);
    
    // Should be efficient (less than 500ms)
    assert!(elapsed.as_millis() < 500, "Phase timing should be efficient");
    
    // Verify all phases were added
    assert_eq!(metrics.phase_times.len(), 1000);
    
    println!("โœ… Phase timing performance test passed");
}

#[test]
fn test_network_simulation_performance() {
    println!("๐Ÿงช Testing network simulation performance...");
    
    let mut metrics = BuildMetrics::new();
    let start_time = Instant::now();
    
    // Simulate network-heavy monitoring
    for i in 0..1000 {
        // Simulate network calls
        let _result = display_monitor(&metrics, &start_time, false);
        
        // Simulate network data processing
        metrics.processed_dependencies = i;
        metrics.total_dependencies = 1000;
        
        thread::sleep(std::time::Duration::from_millis(1));
    }
    
    let elapsed = start_time.elapsed();
    println!("Network simulation took: {:?}", elapsed);
    
    // Should be reasonable (less than 10 seconds)
    assert!(elapsed.as_secs() < 10, "Network simulation should be reasonable");
    
    println!("โœ… Network simulation performance test passed");
}

// Performance benchmark runner
fn run_performance_tests() {
    println!("๐Ÿš€ Running performance tests for interactive functionality...");
    
    test_metrics_collection_performance();
    test_dashboard_rendering_performance();
    test_monitoring_throughput();
    test_concurrent_monitoring_performance();
    test_large_project_metrics_performance();
    test_memory_usage_tracking_performance();
    test_profile_generation_performance();
    test_progress_simulation_performance();
    test_cpu_usage_tracking_performance();
    test_phase_timing_performance();
    test_network_simulation_performance();
    
    println!("๐ŸŽ‰ All performance tests completed successfully!");
}