use batuta::oracle::rag::profiling::{span, Counter, Histogram, GLOBAL_METRICS};
use std::time::Duration;
fn main() {
println!("=== RAG Profiling Demo ===\n");
println!("1. Histogram for latency tracking");
let histogram = Histogram::new();
for latency_ms in [12, 15, 8, 22, 11, 9, 18, 14] {
histogram.observe(Duration::from_millis(latency_ms));
}
println!(" Observations: {}", histogram.count());
println!(" Sum: {:.2}ms", histogram.sum_ms());
println!(" p50: {:.2}ms", histogram.percentile(50.0));
println!(" p90: {:.2}ms", histogram.percentile(90.0));
println!(" p99: {:.2}ms", histogram.percentile(99.0));
println!("\n2. Counter for cache statistics");
let hits = Counter::new();
let misses = Counter::new();
hits.inc_by(45);
misses.inc_by(15);
let hit_rate = hits.get() as f64 / (hits.get() + misses.get()) as f64;
println!(" Cache hits: {}", hits.get());
println!(" Cache misses: {}", misses.get());
println!(" Hit rate: {:.1}%", hit_rate * 100.0);
println!("\n3. Timed span instrumentation");
{
let _span = span("bm25_search");
std::thread::sleep(Duration::from_millis(5));
}
{
let _span = span("tfidf_search");
std::thread::sleep(Duration::from_millis(3));
}
{
let _span = span("rrf_fusion");
std::thread::sleep(Duration::from_millis(1));
}
let summary = GLOBAL_METRICS.summary();
println!(" Recorded spans:");
for (name, stats) in &summary.spans {
println!(" {}: {:.2}ms (count: {})", name, stats.total_us as f64 / 1000.0, stats.count);
}
println!("\n4. Global metrics overview");
println!(" Cache hit rate: {:.1}%", GLOBAL_METRICS.cache_hit_rate() * 100.0);
println!("\n5. Typical profiling workflow:");
println!(" a. Create spans around operations");
println!(" b. Spans auto-record duration on drop");
println!(" c. Query GLOBAL_METRICS.summary() for stats");
println!(" d. Use --rag-profile flag for CLI output");
println!("\n=== Demo Complete ===");
println!("Use 'batuta oracle --rag \"query\" --rag-profile' for CLI profiling.");
}