| 1 |
//! Handpicked stubs from [divan::bench](https://github.com/nvzqz/divan/blob/main/src/bench/mod.rs) |
| 2 |
//! Minimally reimplemented in an API compatible way to run the benches using codspeed intrumentation |
| 3 |
#![allow(clippy::needless_lifetimes)] // We keep explicit lifetime to be as close to original API as possible |
| 4 |
|
| 5 |
mod args; |
| 6 |
mod options; |
| 7 |
|
| 8 |
pub use self::{ |
| 9 |
args::{BenchArgs, BenchArgsRunner}, |
| 10 |
options::BenchOptions, |
| 11 |
}; |
| 12 |
|
| 13 |
use codspeed::codspeed::CodSpeed; |
| 14 |
use std::cell::RefCell; |
| 15 |
|
| 16 |
/// Using this in place of `()` for `GenI` prevents `Bencher::with_inputs` from |
| 17 |
/// working with `()` unintentionally. |
| 18 |
#[non_exhaustive] |
| 19 |
pub struct Unit; |
| 20 |
|
| 21 |
pub struct BencherConfig<GenI = Unit> { |
| 22 |
gen_input: RefCell<GenI>, |
| 23 |
} |
| 24 |
|
| 25 |
pub struct Bencher<'a, 'b, C = BencherConfig> { |
| 26 |
pub(crate) codspeed: &'a RefCell<CodSpeed>, |
| 27 |
pub(crate) uri: String, |
| 28 |
pub(crate) config: C, |
| 29 |
pub(crate) _marker: std::marker::PhantomData<&'b ()>, |
| 30 |
} |
| 31 |
|
| 32 |
impl<'a, 'b> Bencher<'a, 'b> { |
| 33 |
pub(crate) fn new(codspeed: &'a RefCell<CodSpeed>, uri: String) -> Self { |
| 34 |
Self { |
| 35 |
config: BencherConfig { |
| 36 |
gen_input: RefCell::new(Unit), |
| 37 |
}, |
| 38 |
codspeed, |
| 39 |
uri, |
| 40 |
_marker: std::marker::PhantomData, |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
pub fn with_inputs<G>(self, gen_input: G) -> Bencher<'a, 'b, BencherConfig<G>> { |
| 45 |
Bencher { |
| 46 |
config: BencherConfig { |
| 47 |
gen_input: RefCell::new(gen_input), |
| 48 |
}, |
| 49 |
codspeed: self.codspeed, |
| 50 |
uri: self.uri, |
| 51 |
_marker: self._marker, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/// Add a counter to this benchmark (placeholder implementation). |
| 56 |
/// |
| 57 |
/// Note: Counters are not yet supported by codspeed-divan-compat. |
| 58 |
/// This method is provided for API compatibility but does not affect benchmarking. |
| 59 |
pub fn counter<C>(self, _counter: C) -> Self { |
| 60 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 61 |
self |
| 62 |
} |
| 63 |
|
| 64 |
/// Add a counter based on input to this benchmark (placeholder implementation). |
| 65 |
/// |
| 66 |
/// Note: Counters are not yet supported by codspeed-divan-compat. |
| 67 |
/// This method is provided for API compatibility but does not affect benchmarking. |
| 68 |
pub fn input_counter<C, F>(self, _counter_fn: F) -> Self |
| 69 |
where |
| 70 |
F: Fn() -> C, |
| 71 |
{ |
| 72 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 73 |
self |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl<'a, 'b> Bencher<'a, 'b> { |
| 78 |
pub fn bench<O, B>(self, benched: B) |
| 79 |
where |
| 80 |
B: Fn() -> O + Sync, |
| 81 |
{ |
| 82 |
self.with_inputs(|| ()).bench_values(|_| benched()) |
| 83 |
} |
| 84 |
|
| 85 |
pub fn bench_local<O, B>(self, mut benched: B) |
| 86 |
where |
| 87 |
B: FnMut() -> O, |
| 88 |
{ |
| 89 |
self.with_inputs(|| ()).bench_local_values(|_| benched()) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl<'a, 'b, I, GenI> Bencher<'a, 'b, BencherConfig<GenI>> |
| 94 |
where |
| 95 |
GenI: FnMut() -> I, |
| 96 |
{ |
| 97 |
/// Add a counter to this benchmark (placeholder implementation). |
| 98 |
/// |
| 99 |
/// Note: Counters are not yet supported by codspeed-divan-compat. |
| 100 |
/// This method is provided for API compatibility but does not affect benchmarking. |
| 101 |
pub fn counter<C>(self, _counter: C) -> Self { |
| 102 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 103 |
self |
| 104 |
} |
| 105 |
|
| 106 |
/// Add a counter based on input to this benchmark (placeholder implementation). |
| 107 |
/// |
| 108 |
/// Note: Counters are not yet supported by codspeed-divan-compat. |
| 109 |
/// This method is provided for API compatibility but does not affect benchmarking. |
| 110 |
pub fn input_counter<C, F>(self, _counter_fn: F) -> Self |
| 111 |
where |
| 112 |
F: Fn(&I) -> C, |
| 113 |
{ |
| 114 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 115 |
self |
| 116 |
} |
| 117 |
pub fn bench_values<O, B>(self, benched: B) |
| 118 |
where |
| 119 |
B: Fn(I) -> O + Sync, |
| 120 |
GenI: Fn() -> I + Sync, |
| 121 |
{ |
| 122 |
self.bench_local_values(benched) |
| 123 |
} |
| 124 |
|
| 125 |
pub fn bench_refs<O, B>(self, benched: B) |
| 126 |
where |
| 127 |
B: Fn(&mut I) -> O + Sync, |
| 128 |
GenI: Fn() -> I + Sync, |
| 129 |
{ |
| 130 |
self.bench_local_refs(benched) |
| 131 |
} |
| 132 |
|
| 133 |
pub fn bench_local_values<O, B>(self, mut benched: B) |
| 134 |
where |
| 135 |
B: FnMut(I) -> O, |
| 136 |
{ |
| 137 |
let mut codspeed = self.codspeed.borrow_mut(); |
| 138 |
let mut gen_input = self.config.gen_input.borrow_mut(); |
| 139 |
let input = gen_input(); |
| 140 |
codspeed.start_benchmark(self.uri.as_str()); |
| 141 |
let output = benched(input); |
| 142 |
codspeed.end_benchmark(); |
| 143 |
divan::black_box(output); |
| 144 |
} |
| 145 |
|
| 146 |
pub fn bench_local_refs<O, B>(self, mut benched: B) |
| 147 |
where |
| 148 |
B: FnMut(&mut I) -> O, |
| 149 |
{ |
| 150 |
let mut codspeed = self.codspeed.borrow_mut(); |
| 151 |
let mut gen_input = self.config.gen_input.borrow_mut(); |
| 152 |
let mut input = gen_input(); |
| 153 |
|
| 154 |
codspeed.start_benchmark(self.uri.as_str()); |
| 155 |
let output = benched(&mut input); |
| 156 |
codspeed.end_benchmark(); |
| 157 |
divan::black_box(output); |
| 158 |
} |
| 159 |
} |