use std::time::{Duration, Instant};
use tokio::sync::mpsc;
#[tokio::test]
async fn test_event_channel_overhead() {
let (tx, mut rx) = mpsc::channel::<u32>(1000);
let consumer = tokio::spawn(async move {
let mut count = 0;
while let Some(_) = rx.recv().await {
count += 1;
}
count
});
let start = Instant::now();
for i in 0..1000 {
tx.send(i).await.unwrap();
}
drop(tx);
let count = consumer.await.unwrap();
let elapsed = start.elapsed();
println!("Channel operations (1000 messages): {:?}", elapsed);
println!("Average per message: {:?}", elapsed / 1000);
assert_eq!(count, 1000);
}
#[tokio::test]
async fn test_polling_vs_event_driven() {
let start_polling = Instant::now();
let mut iterations = 0;
let target_time = Instant::now() + Duration::from_millis(100);
while Instant::now() < target_time {
tokio::time::sleep(Duration::from_millis(10)).await;
iterations += 1;
}
let polling_elapsed = start_polling.elapsed();
println!(
"Polling approach: {} iterations in {:?}",
iterations, polling_elapsed
);
let start_event = Instant::now();
let (tx, mut rx) = mpsc::channel::<()>(1);
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
let _ = tx.send(()).await;
});
rx.recv().await;
let event_elapsed = start_event.elapsed();
println!("Event-driven approach: {:?}", event_elapsed);
assert!(
event_elapsed < Duration::from_millis(150),
"Event-driven took too long: {:?}",
event_elapsed
);
}