use codegraph_cache::{ReadAheadConfig, ReadAheadIntegration, ReadAheadOptimizer};
use codegraph_core::{CacheType, CompactCacheKey};
use std::time::{Duration, Instant};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("π CodeGraph Read-Ahead Optimizer Demo");
println!("=====================================\n");
// Create the read-ahead integration
let integration = ReadAheadIntegration::new();
// Run comprehensive demonstration
match integration.run_comprehensive_demo().await {
Ok(()) => {
println!("\nπ Demo completed successfully!");
// Show final metrics
let metrics = integration.optimizer.get_metrics().await;
print_detailed_metrics(&metrics);
}
Err(e) => {
println!("β Demo failed: {}", e);
}
}
Ok(())
}
fn print_detailed_metrics(metrics: &codegraph_cache::ReadAheadMetrics) {
println!("\nπ Detailed Performance Metrics:");
println!("βββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββ");
println!("β Metric β Value β");
println!("βββββββββββββββββββββββββββββββββββββββΌββββββββββββββ€");
println!(
"β Total Predictions β {:11} β",
metrics.total_predictions
);
println!(
"β Successful Predictions β {:11} β",
metrics.successful_predictions
);
println!(
"β Prediction Accuracy β {:8.2}% β",
metrics.prediction_accuracy
);
println!(
"β Cache Hits from Read-ahead β {:11} β",
metrics.cache_hits_from_readahead
);
println!(
"β Sequential Reads Detected β {:11} β",
metrics.sequential_reads_detected
);
println!(
"β Cache Warming Events β {:11} β",
metrics.cache_warming_events
);
println!(
"β Bytes Prefetched β {:11} β",
metrics.bytes_prefetched
);
println!(
"β I/O Reduction β {:8.2}% β",
metrics.io_reduction_percentage
);
println!(
"β Average Prediction Time β {:8.2}ms β",
metrics.average_prediction_time_ms
);
println!(
"β Pattern Recognition Success Rate β {:8.2}% β",
metrics.pattern_recognition_success_rate
);
println!("βββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββ");
}
/// Standalone example showing basic read-ahead functionality
async fn basic_readahead_example() -> Result<(), Box<dyn std::error::Error>> {
println!("π Basic Read-Ahead Example");
let config = ReadAheadConfig {
max_pattern_history: 1000,
prediction_window_size: 20,
sequential_threshold: 3,
cache_warming_interval: Duration::from_secs(30),
prefetch_depth: 10,
pattern_decay_factor: 0.9,
min_confidence_threshold: 0.6,
adaptive_learning_rate: 0.1,
};
let optimizer = ReadAheadOptimizer::new(config);
// Simulate sequential access pattern
println!("Simulating sequential access pattern...");
let start_time = Instant::now();
for i in 0..50 {
let key = CompactCacheKey {
hash: 1000 + i,
cache_type: CacheType::Node,
};
if let Some(_data) = optimizer.optimize_read(key).await? {
// Data retrieved successfully
}
}
let elapsed = start_time.elapsed();
println!("Sequential access completed in: {:?}", elapsed);
// Get final metrics
let metrics = optimizer.get_metrics().await;
println!("Predictions made: {}", metrics.total_predictions);
println!(
"Average prediction time: {:.2}ms",
metrics.average_prediction_time_ms
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_basic_example() {
assert!(basic_readahead_example().await.is_ok());
}
}