cuprate_benchmark_lib/
benchmark.rs

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
40
41
42
43
44
45
//! Benchmarking trait.

use std::time::Duration;

/// A benchmarking function and its inputs.
pub trait Benchmark {
    /// The benchmark's name.
    ///
    /// This is automatically implemented
    /// as the name of the [`Self`] type.
    //
    // FIXME: use `const` instead of `fn` when stable
    // <https://github.com/rust-lang/rust/issues/63084>
    fn name() -> &'static str {
        std::any::type_name::<Self>()
    }

    /// Input to the main benchmarking function.
    ///
    /// This is passed to [`Self::MAIN`].
    type Input;

    /// Setup function to generate the input.
    ///
    /// This function is not timed.
    const SETUP: fn() -> Self::Input;

    /// The main function to benchmark.
    ///
    /// The start of the timer begins right before
    /// this function is called and ends after the
    /// function returns.
    const MAIN: fn(Self::Input);

    /// `cuprate-benchmark` will sleep for this [`Duration`] after
    /// creating the [`Self::Input`], but before starting [`Self::MAIN`].
    ///
    /// 1 second by default.
    const PRE_SLEEP_DURATION: Duration = Duration::from_secs(1);

    /// `cuprate-benchmark` will sleep for this [`Duration`] after [`Self::MAIN`].
    ///
    /// 1 second by default.
    const POST_SLEEP_DURATION: Duration = Duration::from_secs(1);
}