use std::time::Instant;
use crate::lab::virtual_time_wheel::VirtualTimerWheel;
fn noop_waker() -> std::task::Waker {
use std::task::{RawWaker, RawWakerVTable, Waker};
const VTABLE: RawWakerVTable = RawWakerVTable::new(
|_| RawWaker::new(std::ptr::null(), &VTABLE),
|_| {},
|_| {},
|_| {},
);
unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VTABLE)) }
}
#[test]
#[ignore]
fn manual_cancel_storm_profile() {
let timer_count = 10_000;
let mut wheel = VirtualTimerWheel::new();
let waker = noop_waker();
let mut handles = Vec::with_capacity(timer_count);
for i in 0..timer_count {
let deadline = (i % 1000) as u64 + 1;
let handle = wheel.insert(deadline, waker.clone());
handles.push(handle);
}
let cancel_count = (timer_count * 9) / 10;
let cancel_start = Instant::now();
for handle in handles.into_iter().take(cancel_count) {
wheel.cancel(handle);
}
let cancel_duration = cancel_start.elapsed();
let advance_start = Instant::now();
let expired = wheel.advance_to(1000);
let advance_duration = advance_start.elapsed();
assert_eq!(expired.len(), timer_count - cancel_count);
let ratio = advance_duration.as_nanos() as f64 / cancel_duration.as_nanos() as f64;
#[cfg(debug_assertions)]
{
eprintln!("Performance profile - Advance/Cancel ratio: {:.2}x", ratio);
if ratio > 10.0 {
eprintln!("✓ Confirms advance_to() bottleneck under cancel storm");
} else {
eprintln!("? Unexpected timing ratio - investigate further");
}
}
}
#[test]
#[ignore]
fn manual_next_deadline_profile() {
let timer_count = 5_000;
let mut wheel = VirtualTimerWheel::new();
let waker = noop_waker();
let mut handles = Vec::with_capacity(timer_count);
for i in 0..timer_count {
let handle = wheel.insert(i as u64 + 1, waker.clone());
handles.push(handle);
}
let cancel_count = (timer_count * 9) / 10;
for handle in handles.into_iter().take(cancel_count) {
wheel.cancel(handle);
}
let start = Instant::now();
let deadline = wheel.next_deadline();
let duration = start.elapsed();
if let Some(d) = deadline {
let expected_deadline = cancel_count as u64 + 1;
assert!(d >= expected_deadline, "next_deadline() should find first non-cancelled timer");
}
#[cfg(debug_assertions)]
{
eprintln!("next_deadline() profile - Duration: {:?}, Found: {:?}", duration, deadline);
if duration.as_micros() > 100 {
eprintln!("✓ Confirms next_deadline() scanning bottleneck");
} else {
eprintln!("? Faster than expected - may need larger test case");
}
}
}