use crate::core::performance::{
clear_background_compute, clear_persistent_cache, get_adaptive_thresholds,
get_background_compute_statistics, get_persistent_cache_statistics,
get_persistent_cached_result, get_profiler_statistics, predict_and_precompute,
record_performance, store_persistent_cached_result, submit_background_task, ComputePriority,
};
use crate::core::Expression;
use crate::expr;
use crate::simplify::Simplify;
use std::time::{Duration, Instant};
pub fn demonstrate_integration() {
println!("Testing Runtime Performance Profiling:");
simulate_performance_measurements();
let thresholds = get_adaptive_thresholds();
let profiler_stats = get_profiler_statistics();
println!(" Current Adaptive Thresholds:");
println!(
" SIMD Threshold: {} (confidence: {:.2})",
thresholds.simd_threshold, thresholds.confidence
);
println!(
" Parallel Threshold: {} (samples: {})",
thresholds.parallel_threshold, thresholds.sample_count
);
println!(" Profiler Statistics:");
println!(
" Total Measurements: {}",
profiler_stats.total_measurements
);
println!(
" Recent Measurements: {}",
profiler_stats.recent_measurements
);
println!("\nTesting Persistent Cache:");
clear_persistent_cache();
let test_expressions = [expr!(x + 1), expr!(y * 2), expr!(z ^ 2)];
for (i, expr) in test_expressions.iter().enumerate() {
let simplified = expr.simplify();
let hash = compute_simple_hash(expr);
store_persistent_cached_result(hash, &simplified);
println!(
" Stored expression {}: {} -> {}",
i + 1,
format_expr(expr),
format_expr(&simplified)
);
}
let cache_stats = get_persistent_cache_statistics();
println!(" Cache Statistics:");
println!(" Total Entries: {}", cache_stats.total_entries);
println!(
" Cache Directory: {}",
cache_stats.cache_directory.display()
);
println!(" File Size: {} bytes", cache_stats.cache_file_size);
let first_hash = compute_simple_hash(&test_expressions[0]);
if let Some(cached) = get_persistent_cached_result(first_hash) {
println!(
" Successfully retrieved cached result: {}",
format_expr(&cached)
);
} else {
println!(" Failed to retrieve cached result");
}
println!("\nTesting Background Precomputation:");
clear_background_compute();
let high_priority_expr = Expression::add(vec![
Expression::pow(Expression::symbol("x"), Expression::integer(2)),
Expression::mul(vec![Expression::integer(2), Expression::symbol("x")]),
Expression::integer(1),
]);
let medium_priority_expr = Expression::function("sin", vec![expr!(x)]);
let low_priority_expr = Expression::function("cos", vec![expr!(y)]);
let task1 = submit_background_task(high_priority_expr.clone(), ComputePriority::High, 0.9);
let task2 = submit_background_task(medium_priority_expr.clone(), ComputePriority::Medium, 0.6);
let task3 = submit_background_task(low_priority_expr.clone(), ComputePriority::Low, 0.3);
println!(" Submitted background tasks:");
println!(
" High Priority (ID {}): {}",
task1,
format_expr(&high_priority_expr)
);
println!(
" Medium Priority (ID {}): {}",
task2,
format_expr(&medium_priority_expr)
);
println!(
" Low Priority (ID {}): {}",
task3,
format_expr(&low_priority_expr)
);
let current_expr = Expression::mul(vec![expr!(a), expr!(b)]);
predict_and_precompute(¤t_expr);
println!(
" Triggered predictive precomputation for: {}",
format_expr(¤t_expr)
);
let bg_stats = get_background_compute_statistics();
println!(" Background Compute Statistics:");
println!(" Queue Size: {}", bg_stats.queue_size);
println!(" Cache Size: {}", bg_stats.cache_size);
println!(" Worker Running: {}", bg_stats.worker_running);
println!(
" Average Compute Time: {:.2}ms",
bg_stats.average_compute_time.as_millis()
);
println!("\nTesting Integrated Performance System:");
let complex_expr = Expression::add(vec![
Expression::mul(vec![Expression::integer(3), Expression::symbol("x")]),
Expression::pow(Expression::symbol("x"), Expression::integer(2)),
Expression::function("sin", vec![Expression::symbol("x")]),
Expression::integer(5),
]);
println!(
" Processing complex expression: {}",
format_expr(&complex_expr)
);
let start_time = Instant::now();
let result = complex_expr.simplify();
let duration = start_time.elapsed();
record_performance("integrated_simplify", 4, duration);
println!(" Result: {}", format_expr(&result));
println!(" Processing Time: {:.2}ms", duration.as_millis());
predict_and_precompute(&result);
println!("\nTesting System Integration:");
let final_thresholds = get_adaptive_thresholds();
let final_profiler_stats = get_profiler_statistics();
let final_cache_stats = get_persistent_cache_statistics();
let final_bg_stats = get_background_compute_statistics();
println!(" Final System State:");
println!(
" Adaptive Thresholds: SIMD={}, Parallel={}, Confidence={:.2}",
final_thresholds.simd_threshold,
final_thresholds.parallel_threshold,
final_thresholds.confidence
);
println!(
" Performance Measurements: {}",
final_profiler_stats.total_measurements
);
println!(
" Persistent Cache Entries: {}",
final_cache_stats.total_entries
);
println!(" Background Queue Size: {}", final_bg_stats.queue_size);
let integration_score = calculate_integration_score(
&final_thresholds,
&final_profiler_stats,
&final_cache_stats,
&final_bg_stats,
);
println!(" Integration Score: {:.1}/100", integration_score);
if integration_score >= 80.0 {
println!("\nPhase 3 Advanced Integration: EXCELLENT!");
println!(" Adaptive thresholds: Enabled");
println!(" Persistent cache: Enabled");
println!(" Background precomputation: Enabled");
println!(" System integration: Enabled");
} else if integration_score >= 60.0 {
println!("\nPhase 3 Advanced Integration: GOOD!");
} else {
println!("\nPhase 3 Advanced Integration: NEEDS IMPROVEMENT");
}
}
fn simulate_performance_measurements() {
for size in [10, 25, 50, 100, 200, 500].iter() {
let simd_time = Duration::from_micros((*size as u64) / 2); let sequential_time = Duration::from_micros(*size as u64);
record_performance("simd_add", *size, simd_time);
record_performance("sequential_add", *size, sequential_time);
}
for size in [100, 500, 1000, 2000, 5000].iter() {
let parallel_time = Duration::from_micros((*size as u64) / 4); let sequential_time = Duration::from_micros(*size as u64);
record_performance("parallel_multiply", *size, parallel_time);
record_performance("sequential_multiply", *size, sequential_time);
}
}
fn calculate_integration_score(
thresholds: &crate::core::performance::AdaptiveThresholds,
profiler_stats: &crate::core::performance::ProfilerStatistics,
cache_stats: &crate::core::performance::PersistentCacheStatistics,
bg_stats: &crate::core::performance::BackgroundComputeStatistics,
) -> f64 {
let mut score = 0.0;
if thresholds.sample_count > 0 {
score += 15.0;
if thresholds.confidence > 0.5 {
score += 10.0;
}
}
if profiler_stats.total_measurements > 0 {
score += 15.0;
if profiler_stats.total_measurements >= 10 {
score += 10.0;
}
}
if cache_stats.total_entries > 0 {
score += 15.0;
if cache_stats.total_entries >= 3 {
score += 10.0;
}
}
if bg_stats.worker_running {
score += 15.0;
if bg_stats.queue_size > 0 {
score += 10.0;
}
}
score
}
fn format_expr(expr: &Expression) -> String {
match expr {
Expression::Number(n) => format!("{:?}", n),
Expression::Symbol(s) => s.name.to_string(),
Expression::Add(terms) => {
if terms.len() <= 3 {
format!(
"({})",
terms
.iter()
.map(format_expr)
.collect::<Vec<_>>()
.join(" + ")
)
} else {
format!("({} terms)", terms.len())
}
}
Expression::Mul(factors) => {
if factors.len() <= 3 {
format!(
"({})",
factors
.iter()
.map(format_expr)
.collect::<Vec<_>>()
.join(" * ")
)
} else {
format!("({} factors)", factors.len())
}
}
Expression::Pow(base, exp) => format!("{}^{}", format_expr(base), format_expr(exp)),
Expression::Function { name, args } => {
if args.len() == 1 {
format!("{}({})", name, format_expr(&args[0]))
} else {
format!("{}({} args)", name, args.len())
}
}
_ => format!("{:?}", expr),
}
}
fn compute_simple_hash(expr: &Expression) -> u64 {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
std::mem::discriminant(expr).hash(&mut hasher);
hasher.finish()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_integration() {
clear_persistent_cache();
clear_background_compute();
record_performance("test_simd", 100, Duration::from_micros(50));
record_performance("test_sequential", 100, Duration::from_micros(100));
let thresholds = get_adaptive_thresholds();
assert!(thresholds.simd_threshold > 0);
assert!(thresholds.parallel_threshold > 0);
let expr = Expression::add(vec![Expression::symbol("test"), Expression::integer(42)]);
let simplified = expr.simplify();
let hash = compute_simple_hash(&expr);
store_persistent_cached_result(hash, &simplified);
let cached = get_persistent_cached_result(hash);
let _ = cached;
let task_id = submit_background_task(
Expression::mul(vec![Expression::symbol("x"), Expression::integer(2)]),
ComputePriority::High,
0.8,
);
assert!(task_id > 0);
let bg_stats = get_background_compute_statistics();
assert!(bg_stats.worker_running);
let profiler_stats = get_profiler_statistics();
let cache_stats = get_persistent_cache_statistics();
let score =
calculate_integration_score(&thresholds, &profiler_stats, &cache_stats, &bg_stats);
assert!(score > 0.0);
assert!(score <= 100.0);
}
#[test]
fn test_expression_formatting() {
let expr = Expression::add(vec![Expression::symbol("x"), Expression::integer(1)]);
let formatted = format_expr(&expr);
assert!(formatted.contains("x"));
assert!(formatted.contains("1"));
}
#[test]
fn test_simple_hash() {
let expr1 = Expression::symbol("test");
let expr2 = Expression::symbol("test");
let expr3 = Expression::integer(42);
let hash1 = compute_simple_hash(&expr1);
let hash2 = compute_simple_hash(&expr2);
let hash3 = compute_simple_hash(&expr3);
assert_eq!(hash1, hash2);
assert_ne!(hash1, hash3);
}
#[test]
fn test_performance_simulation() {
let initial_stats = get_profiler_statistics();
let initial_count = initial_stats.total_measurements;
simulate_performance_measurements();
let final_stats = get_profiler_statistics();
assert!(final_stats.total_measurements > initial_count);
}
}