//! Performance test demonstration

use phprs::engine::{function_optimizer, jit, opcode_cache, perf_alloc};

fn main() {
    println!("phprs — performance-oriented interpreter components (demo)");
    println!("This demo exercises internal optimizations; compare to PHP only with your own benchmarks.\n");

    // Test optimized memory allocation
    println!("📊 Testing Memory Optimizations:");
    let mut builder = perf_alloc::StringBuilder::with_capacity(100);
    builder.push_str("Hello");
    builder.push_str(" ");
    builder.push_str("World");
    println!("  ✓ Optimized string builder: {}", builder.as_str());

    // Test fast concatenation
    let result = phprs::engine::string::string_concat2("Hello", " World");
    println!("  ✓ Fast concatenation: {}", result.as_str());

    // Test JIT functionality
    println!("\n⚡ Testing JIT Compiler:");
    let should_jit = jit::increment_execution_counter("test_function");
    println!("  ✓ JIT execution tracking: {}", should_jit);

    // Test opcode cache
    println!("\n💾 Testing Opcode Cache:");
    let _cache = opcode_cache::get_opcode_cache();
    println!("  ✓ Cache initialized successfully");

    // Test function optimizer
    println!("\n🔧 Testing Function Optimizer:");
    let optimizer = function_optimizer::get_function_optimizer();
    let _optimizer = optimizer.read().unwrap();
    println!("  ✓ Function optimizer initialized");

    // Test array operations
    println!("\n📋 Testing Array Operations:");
    let mut array = phprs::engine::array_ops::OptimizedArray::new();
    array.push(phprs::engine::types::Val::new(
        phprs::engine::types::PhpValue::Long(42),
        phprs::engine::types::PhpType::Long,
    ));
    println!("  ✓ Optimized array push: {} elements", array.len());

    println!("\n✅ Demo completed (optimizations exercised).");

    // Run a small benchmark demo
    println!("\n🏁 Running Mini Benchmark:");
    let start = std::time::Instant::now();

    // Simulate some operations
    for i in 0..100_000 {
        let _ = phprs::engine::string::string_concat2("test", &i.to_string());
    }

    let elapsed = start.elapsed();
    println!(
        "  ✓ String operations: {:.2} ops/sec",
        100_000.0 / elapsed.as_secs_f64()
    );

    println!("\n🚀 Performance optimizations implemented:");
    println!("  • JIT compilation for hot code paths");
    println!("  • Optimized VM instruction dispatch");
    println!("  • High-performance memory management");
    println!("  • Fast string operations");
    println!("  • Optimized array operations");
    println!("  • Function call optimizations and inlining");
    println!("  • Advanced opcode caching");
    println!("  • Comprehensive benchmarking suite");
}
