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();
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;
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);
assert!(elapsed.as_millis() < 100, "Metrics collection should be fast");
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();
for i in 0..100 {
let _result = display_dashboard(&metrics, &start_time, BuildSystem::Maven);
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);
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();
for i in 0..100 {
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;
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);
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![];
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;
let _result = display_monitor(&metrics, &start_time_clone, false);
thread::sleep(std::time::Duration::from_millis(10));
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
let elapsed = start_time.elapsed();
println!("Concurrent monitoring completed in: {:?}", elapsed);
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();
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;
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);
assert!(elapsed.as_millis() < 1000, "Large project metrics should be fast");
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();
for i in 0..10000 {
metrics.processed_dependencies = i;
metrics.memory_usage_mb = 512 + (i as f64 * 0.1) as u64;
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);
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();
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);
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();
for i in 0..10000 {
tx.send(format!("Progress update {}", i)).unwrap();
}
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);
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();
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;
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);
assert!(elapsed.as_millis() < 1000, "CPU tracking should be efficient");
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();
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);
assert!(elapsed.as_millis() < 500, "Phase timing should be efficient");
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();
for i in 0..1000 {
let _result = display_monitor(&metrics, &start_time, false);
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);
assert!(elapsed.as_secs() < 10, "Network simulation should be reasonable");
println!("โ
Network simulation performance test passed");
}
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!");
}