use quantum_pulse::{pause, profile, unpause, ProfileCollector, ProfileOp};
use std::thread;
use std::time::Duration;
#[derive(Debug, ProfileOp)]
enum AppOperation {
#[category(name = "Core", description = "Core business operations")]
CriticalWork,
#[category(name = "Maintenance", description = "Background maintenance tasks")]
BackgroundTask,
#[category(name = "Debug", description = "Debug and diagnostic operations")]
DiagnosticWork,
}
fn perform_critical_work() {
thread::sleep(Duration::from_millis(50));
println!(" β Critical work completed");
}
fn perform_background_task() {
thread::sleep(Duration::from_millis(30));
println!(" β Background task completed");
}
fn perform_diagnostic_work() {
thread::sleep(Duration::from_millis(20));
println!(" β Diagnostic work completed");
}
fn main() {
println!("π Demonstrating Pause/Unpause Profiling");
println!("==========================================\n");
ProfileCollector::clear_all();
println!("π Phase 1: Normal profiling (all operations recorded)");
profile!(AppOperation::CriticalWork, {
perform_critical_work();
});
profile!(AppOperation::BackgroundTask, {
perform_background_task();
});
profile!(AppOperation::DiagnosticWork, {
perform_diagnostic_work();
});
let stats = ProfileCollector::get_all_stats();
println!("π Operations recorded: {}", stats.len());
for (name, stat) in &stats {
println!(
" - {}: {} calls, avg {}ΞΌs",
name,
stat.count,
stat.mean().as_micros()
);
}
println!();
println!("βΈοΈ Phase 2: Paused profiling (no operations recorded)");
pause!();
println!(" Profiling paused: {}", ProfileCollector::is_paused());
profile!(AppOperation::CriticalWork, {
perform_critical_work();
});
profile!(AppOperation::BackgroundTask, {
perform_background_task();
});
profile!(AppOperation::DiagnosticWork, {
perform_diagnostic_work();
});
let stats_paused = ProfileCollector::get_all_stats();
println!("π Operations still recorded: {}", stats_paused.len());
for (name, stat) in &stats_paused {
println!(
" - {}: {} calls, avg {}ΞΌs",
name,
stat.count,
stat.mean().as_micros()
);
}
println!(" (Notice: call counts didn't increase during pause)\n");
println!("βΆοΈ Phase 3: Resumed profiling (operations recorded again)");
unpause!();
println!(" Profiling paused: {}", ProfileCollector::is_paused());
profile!(AppOperation::CriticalWork, {
perform_critical_work();
});
profile!(AppOperation::BackgroundTask, {
perform_background_task();
});
let final_stats = ProfileCollector::get_all_stats();
println!("π Final operations recorded: {}", final_stats.len());
for (name, stat) in &final_stats {
println!(
" - {}: {} calls, avg {}ΞΌs",
name,
stat.count,
stat.mean().as_micros()
);
}
println!();
println!("π‘ Use Cases for Pause/Unpause:");
println!(" - Exclude initialization/cleanup from measurements");
println!(" - Focus profiling on specific code sections");
println!(" - Reduce overhead during non-critical operations");
println!(" - Debug performance issues in targeted areas");
println!("\nπ― Example: Selective profiling in a loop");
ProfileCollector::clear_all();
for i in 0..5 {
if i == 2 {
println!(" Pausing profiling for iteration {}", i);
pause!();
} else if i == 4 {
println!(" Resuming profiling for iteration {}", i);
unpause!();
}
profile!(AppOperation::CriticalWork, {
thread::sleep(Duration::from_millis(10));
println!(" Iteration {} completed", i);
});
}
let loop_stats = ProfileCollector::get_stats("Core::CriticalWork");
if let Some(stats) = loop_stats {
println!(
"π Loop profiling results: {} calls recorded (should be 3, not 5)",
stats.count
);
println!(" (Iterations 2 and 3 were excluded due to pause)");
}
}