use asupersync::io::IoCap;
use asupersync::io::cap::LabIoCap;
use std::sync::Arc;
use std::thread;
use std::time::Instant;
fn main() {
println!("IO Capability Budget Accounting Profiling");
let threads = 8;
let ops_per_thread = 1_000_000;
let total_ops = threads * ops_per_thread;
println!(
"Scenario: {} threads × {} ops = {} total operations",
threads, ops_per_thread, total_ops
);
let io_cap = Arc::new(LabIoCap::new_for_tests());
println!("=== PROFILING TARGET: ATOMIC BUDGET ACCOUNTING ===");
let start = Instant::now();
let handles: Vec<_> = (0..threads)
.map(|thread_id| {
let cap = Arc::clone(&io_cap);
thread::spawn(move || {
for i in 0..ops_per_thread {
cap.record_submit();
cap.record_complete();
if i % 100 == 0 {
let _stats = cap.stats(); }
}
println!(
"Thread {} completed {} I/O operations",
thread_id, ops_per_thread
);
})
})
.collect();
for handle in handles {
handle.join().unwrap();
}
let elapsed = start.elapsed();
let total_atomic_ops = total_ops * 2 + (total_ops / 100) * 2;
println!(
"*** BUDGET ACCOUNTING COMPLETED: {:.1}s ***",
elapsed.as_secs_f64()
);
println!("Total atomic operations: {}", total_atomic_ops);
println!(
"Throughput: {:.1} atomic ops/sec",
total_atomic_ops as f64 / elapsed.as_secs_f64()
);
println!(
"Latency: {:.1} ns/op",
elapsed.as_nanos() as f64 / total_atomic_ops as f64
);
let final_stats = io_cap.stats();
println!(
"Final stats: submitted={}, completed={}",
final_stats.submitted, final_stats.completed
);
let expected_ops = total_ops as u64;
assert!(
final_stats.submitted == expected_ops && final_stats.completed == expected_ops,
"Incorrect accounting! Expected {}, got submit={} complete={}",
total_ops,
final_stats.submitted,
final_stats.completed
);
println!("✓ Budget accounting correctness verified");
}