Skip to main content

subms_timer_wheel/
recipe.rs

1//! `SubMsRecipe` impl.
2
3use subms::{
4    SubMsBenchParams, SubMsLcg, SubMsPerfHarness, SubMsRecipe, SubMsStageKind, SubMsTimer,
5};
6
7use crate::TimerWheel;
8
9pub struct TimerWheelRecipe;
10
11impl SubMsRecipe for TimerWheelRecipe {
12    fn name(&self) -> &str {
13        "timer-wheel"
14    }
15
16    fn run(&self, h: &mut SubMsPerfHarness, params: &SubMsBenchParams) {
17        let entries = params.entries;
18        let warmup = params.warmup;
19        let seed = params.seed;
20        let slots = 1024usize;
21        let mut w: TimerWheel<u32> = TimerWheel::new(slots);
22
23        let mut rng = SubMsLcg::new(seed);
24        for i in 0..warmup as u32 {
25            let _ = w.schedule(rng.bounded((slots * 4) as u32) as usize, i);
26        }
27
28        let s_sched = h
29            .stage("schedule", entries)
30            .with_kind(SubMsStageKind::HotPath);
31        let mut rng = SubMsLcg::new(seed.wrapping_add(1));
32        let mut ids = Vec::with_capacity(entries);
33        for i in 0..entries as u32 {
34            let delay = rng.bounded((slots * 4) as u32) as usize;
35            let t0 = SubMsTimer::tick();
36            let id = w.schedule(delay, i);
37            s_sched.record(t0.elapsed_ns());
38            ids.push(id);
39        }
40
41        // Cancel half of the scheduled timers.
42        let s_cancel = h
43            .stage("cancel", entries / 2)
44            .with_kind(SubMsStageKind::HotPath);
45        for &id in ids.iter().step_by(2) {
46            let t0 = SubMsTimer::tick();
47            let _ = w.cancel(id);
48            s_cancel.record(t0.elapsed_ns());
49        }
50
51        // Drain ticks.
52        let s_tick = h
53            .stage("tick", slots * 5)
54            .with_kind(SubMsStageKind::HotPath);
55        for _ in 0..(slots * 5) {
56            let t0 = SubMsTimer::tick();
57            let _ = w.tick();
58            s_tick.record(t0.elapsed_ns());
59        }
60
61        h.add_meta("slots", &slots.to_string());
62    }
63}