File: timer.rs

package info (click to toggle)
rust-async-io 2.3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 388 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Benchmarks for registering timers.

use async_io::Timer;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use futures_lite::future;
use std::time::Duration;

/// Create a new `Timer` and poll it once to register it into the timer wheel.
fn make_timer() -> Timer {
    let mut timer = Timer::after(Duration::from_secs(1));
    future::block_on(future::poll_once(&mut timer));
    timer
}

/// Benchmark the time it takes to register and deregister a timer.
fn register_timer(c: &mut Criterion) {
    let mut group = c.benchmark_group("register_timer");
    for prev_timer_count in [0, 1_000_000] {
        // Add timers to the timer wheel.
        let mut timers = Vec::new();
        for _ in 0..prev_timer_count {
            timers.push(make_timer());
        }

        // Benchmark registering a timer.
        group.bench_function(
            format!("register_timer.({} previous timers)", prev_timer_count),
            |b| {
                b.iter(|| {
                    let timer = make_timer();
                    black_box(timer);
                });
            },
        );
    }
}

criterion_group!(benches, register_timer);
criterion_main!(benches);