use async_inspect::config::Config;
use async_inspect::export::{CsvExporter, JsonExporter};
use async_inspect::prelude::*;
use async_inspect::runtime::tokio::spawn_tracked;
use colored::Colorize;
use std::time::Duration;
use tokio::time::sleep;
#[async_inspect::trace]
async fn api_request(endpoint: &str, id: u32) -> std::result::Result<String, String> {
sleep(Duration::from_millis(50 + id as u64 * 10)).await;
if id % 20 == 0 {
Err(format!("Failed to fetch {}", endpoint))
} else {
Ok(format!("Response from {} (id={})", endpoint, id))
}
}
#[async_inspect::trace]
async fn db_query(query: &str, _id: u32) -> Vec<String> {
sleep(Duration::from_millis(30)).await;
vec![format!("Result for: {}", query)]
}
#[async_inspect::trace]
async fn cache_get(key: &str) -> Option<String> {
sleep(Duration::from_millis(5)).await;
if key.len() % 2 == 0 {
Some(format!("Cached: {}", key))
} else {
None
}
}
#[async_inspect::trace]
async fn handle_request(request_id: u32) -> std::result::Result<String, String> {
let cache_key = format!("request_{}", request_id);
if let Some(cached) = cache_get(&cache_key).await {
return Ok(cached);
}
let api_data = api_request("/users", request_id).await?;
let db_data = db_query("SELECT * FROM users", request_id).await;
Ok(format!("Processed: {} with {}", api_data, db_data.len()))
}
async fn production_workload() {
println!("๐ Starting production workload simulation...\n");
let mut handles = vec![];
for i in 1..=1000 {
let handle = spawn_tracked(format!("request_{}", i), async move {
let _ = handle_request(i).await;
});
handles.push(handle);
if i % 10 == 0 {
sleep(Duration::from_millis(5)).await;
}
}
for handle in handles {
let _ = handle.await;
}
println!("โ
Workload complete!\n");
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!(
"โ {} - Production-Ready Example โ",
"[async-inspect]".on_purple().white().bold()
);
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!();
let config = Config::global();
config.production_mode();
println!(
"{}",
"[STATS] Production Configuration:".on_blue().white().bold()
);
println!(" Sampling rate: 1 in {}", config.sampling_rate());
println!(" Max events: {}", config.max_events());
println!(" Max tasks: {}", config.max_tasks());
println!(" Track awaits: {}", config.track_awaits());
println!(" Enable HTML: {}", config.enable_html());
println!();
production_workload().await;
sleep(Duration::from_millis(100)).await;
let overhead = config.overhead_stats();
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!("โ Overhead Analysis โ");
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!(" Total overhead: {:.2}ms", overhead.total_ms());
println!(" Instrumentation calls: {}", overhead.calls);
println!(" Average per call: {:.2}ยตs", overhead.avg_us());
println!(
" Overhead percentage: {:.4}%",
overhead.total_ms() / Inspector::global().stats().timeline_duration.as_secs_f64() / 10.0
);
println!();
let reporter = Reporter::global();
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!("โ Summary Report โ");
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
reporter.print_summary();
println!("\nโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!("โ Exporting Data โ");
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
let json_path = "production_export.json";
match JsonExporter::export_to_file(Inspector::global(), json_path) {
Ok(_) => println!(" โ
JSON exported to: {}", json_path),
Err(e) => println!(" โ JSON export failed: {}", e),
}
let csv_tasks_path = "production_tasks.csv";
match CsvExporter::export_tasks_to_file(Inspector::global(), csv_tasks_path) {
Ok(_) => println!(" โ
Tasks CSV exported to: {}", csv_tasks_path),
Err(e) => println!(" โ Tasks CSV export failed: {}", e),
}
let csv_events_path = "production_events.csv";
match CsvExporter::export_events_to_file(Inspector::global(), csv_events_path) {
Ok(_) => println!(" โ
Events CSV exported to: {}", csv_events_path),
Err(e) => println!(" โ Events CSV export failed: {}", e),
}
println!();
let profiler = Inspector::global().build_profiler();
let perf_reporter = async_inspect::profile::PerformanceReporter::new(&profiler);
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!("โ Performance Analysis (Sampled Data) โ");
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
perf_reporter.print_report();
println!("\nโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!("โ Production Recommendations โ");
println!("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
println!();
println!("โ
Production Mode Benefits:");
println!(" โข Sampling reduces overhead by 99%");
println!(" โข Limited event/task storage prevents memory bloat");
println!(" โข Disabled HTML generation saves CPU cycles");
println!(" โข Still get statistical insights from sampled data");
println!();
println!("๐ Exported Data Can Be:");
println!(" โข Analyzed offline with tools like pandas, Excel");
println!(" โข Integrated with monitoring systems (Prometheus, Datadog)");
println!(" โข Stored in time-series databases for trending");
println!(" โข Used for post-mortem debugging");
println!();
println!("๐ฏ Tuning Recommendations:");
println!(" โข Adjust sampling rate based on traffic volume");
println!(" โข Monitor overhead percentage (should be < 0.1%)");
println!(" โข Export data periodically to external storage");
println!(" โข Use environment variables for runtime configuration");
println!();
Ok(())
}