| 1 |
// Used by generated code from the macro `codspeed_divan_compat_macros::bench_compat` |
| 2 |
#[doc(hidden)] |
| 3 |
pub mod __private { |
| 4 |
pub use super::{ |
| 5 |
bench::{BenchArgs, BenchOptions}, |
| 6 |
entry::{ |
| 7 |
BenchEntry, BenchEntryRunner, EntryConst, EntryList, EntryLocation, EntryMeta, |
| 8 |
EntryType, GenericBenchEntry, GroupEntry, BENCH_ENTRIES, GROUP_ENTRIES, |
| 9 |
}, |
| 10 |
}; |
| 11 |
|
| 12 |
pub use divan::__private::{shrink_array, Arg, ToStringHelper}; |
| 13 |
} |
| 14 |
|
| 15 |
mod bench; |
| 16 |
mod cli; |
| 17 |
mod config; |
| 18 |
mod entry; |
| 19 |
mod uri; |
| 20 |
mod util; |
| 21 |
|
| 22 |
pub use bench::*; |
| 23 |
|
| 24 |
// Counter types (placeholder implementations) |
| 25 |
pub mod counter { |
| 26 |
/// Process N bytes. |
| 27 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 28 |
pub struct BytesCount { |
| 29 |
count: u64, |
| 30 |
} |
| 31 |
|
| 32 |
/// Process N [`char`s](char). |
| 33 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 34 |
pub struct CharsCount { |
| 35 |
count: u64, |
| 36 |
} |
| 37 |
|
| 38 |
/// Process N cycles, displayed as Hertz. |
| 39 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 40 |
pub struct CyclesCount { |
| 41 |
count: u64, |
| 42 |
} |
| 43 |
|
| 44 |
/// Process N items. |
| 45 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 46 |
pub struct ItemsCount { |
| 47 |
count: u64, |
| 48 |
} |
| 49 |
|
| 50 |
impl BytesCount { |
| 51 |
/// Count N bytes. |
| 52 |
#[inline] |
| 53 |
pub fn new<N>(count: N) -> Self |
| 54 |
where |
| 55 |
N: TryInto<u64>, |
| 56 |
N::Error: std::fmt::Debug, |
| 57 |
{ |
| 58 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 59 |
Self { |
| 60 |
count: count.try_into().unwrap(), |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/// Counts the size of a type with [`size_of`]. |
| 65 |
#[inline] |
| 66 |
pub fn of<T>() -> Self { |
| 67 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 68 |
Self { |
| 69 |
count: std::mem::size_of::<T>() as u64, |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/// Counts the size of multiple instances of a type with [`size_of`]. |
| 74 |
#[inline] |
| 75 |
pub fn of_many<T>(n: usize) -> Self { |
| 76 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 77 |
Self { |
| 78 |
count: (std::mem::size_of::<T>() * n) as u64, |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/// Counts the size of a value with [`size_of_val`]. |
| 83 |
#[inline] |
| 84 |
pub fn of_val<T: ?Sized>(val: &T) -> Self { |
| 85 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 86 |
Self { |
| 87 |
count: std::mem::size_of_val(val) as u64, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/// Counts the bytes of a [`&str`]. |
| 92 |
#[inline] |
| 93 |
pub fn of_str<S: ?Sized + AsRef<str>>(s: &S) -> Self { |
| 94 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 95 |
Self::of_val(s.as_ref()) |
| 96 |
} |
| 97 |
|
| 98 |
/// Counts the bytes of a [slice](prim@slice). |
| 99 |
#[inline] |
| 100 |
pub fn of_slice<T, S: ?Sized + AsRef<[T]>>(s: &S) -> Self { |
| 101 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 102 |
Self::of_val(s.as_ref()) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl CharsCount { |
| 107 |
/// Count N [`char`s](char). |
| 108 |
#[inline] |
| 109 |
pub fn new<N>(count: N) -> Self |
| 110 |
where |
| 111 |
N: TryInto<u64>, |
| 112 |
N::Error: std::fmt::Debug, |
| 113 |
{ |
| 114 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 115 |
Self { |
| 116 |
count: count.try_into().unwrap(), |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/// Counts the [`char`s](prim@char) of a [`&str`](prim@str). |
| 121 |
#[inline] |
| 122 |
pub fn of_str<S: ?Sized + AsRef<str>>(s: &S) -> Self { |
| 123 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 124 |
Self::new(s.as_ref().chars().count() as u64) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
impl CyclesCount { |
| 129 |
/// Count N cycles. |
| 130 |
#[inline] |
| 131 |
pub fn new<N>(count: N) -> Self |
| 132 |
where |
| 133 |
N: TryInto<u64>, |
| 134 |
N::Error: std::fmt::Debug, |
| 135 |
{ |
| 136 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 137 |
Self { |
| 138 |
count: count.try_into().unwrap(), |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
impl ItemsCount { |
| 144 |
/// Count N items. |
| 145 |
#[inline] |
| 146 |
pub fn new<N>(count: N) -> Self |
| 147 |
where |
| 148 |
N: TryInto<u64>, |
| 149 |
N::Error: std::fmt::Debug, |
| 150 |
{ |
| 151 |
eprintln!("Warning: Counter feature is not yet supported by codspeed-divan-compat"); |
| 152 |
Self { |
| 153 |
count: count.try_into().unwrap(), |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
use codspeed::codspeed::CodSpeed; |
| 159 |
use config::Filter; |
| 160 |
use entry::AnyBenchEntry; |
| 161 |
use regex::Regex; |
| 162 |
use std::{cell::RefCell, rc::Rc}; |
| 163 |
|
| 164 |
pub fn main() { |
| 165 |
// Outlined steps of original divan::main and their equivalent in codspeed instrumented mode |
| 166 |
// 1. Get registered entries |
| 167 |
let group_entries = &entry::GROUP_ENTRIES; |
| 168 |
|
| 169 |
let generic_bench_entries = group_entries.iter().flat_map(|group| { |
| 170 |
group |
| 171 |
.generic_benches_iter() |
| 172 |
.map(AnyBenchEntry::GenericBench) |
| 173 |
}); |
| 174 |
|
| 175 |
let bench_entries = entry::BENCH_ENTRIES |
| 176 |
.iter() |
| 177 |
.map(AnyBenchEntry::Bench) |
| 178 |
.chain(generic_bench_entries); |
| 179 |
|
| 180 |
// TODO: Manage non generic bench groups |
| 181 |
|
| 182 |
// 2. Build an execution tree |
| 183 |
// No need, we do not manage detailed tree printing like original divan, and we extract |
| 184 |
// codspeed URI from entry metadata directly. |
| 185 |
|
| 186 |
// 3. Filtering |
| 187 |
let should_run_benchmark_from_filters = { |
| 188 |
let mut command = cli::command(); |
| 189 |
let matches = command.get_matches_mut(); |
| 190 |
let is_exact = matches.get_flag("exact"); |
| 191 |
|
| 192 |
let parse_filter = |filter: &String| { |
| 193 |
if is_exact { |
| 194 |
Filter::Exact(filter.to_owned()) |
| 195 |
} else { |
| 196 |
match Regex::new(filter) { |
| 197 |
Ok(r) => Filter::Regex(r), |
| 198 |
Err(error) => { |
| 199 |
let kind = clap::error::ErrorKind::ValueValidation; |
| 200 |
command.error(kind, error).exit(); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
}; |
| 205 |
|
| 206 |
let filters: Option<Vec<Filter>> = matches |
| 207 |
.get_many::<String>("filter") |
| 208 |
.map(|arg_filters| arg_filters.map(parse_filter).collect()); |
| 209 |
|
| 210 |
move |uri: &str| { |
| 211 |
if let Some(filters) = filters.as_ref() { |
| 212 |
filters.iter().any(|filter| filter.is_match(uri)) |
| 213 |
} else { |
| 214 |
true |
| 215 |
} |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
// 4. Scan the tree and execute benchmarks |
| 220 |
let codspeed = Rc::new(RefCell::new(CodSpeed::new())); |
| 221 |
for entry in bench_entries { |
| 222 |
let runner = entry.bench_runner(); |
| 223 |
let meta = entry.meta(); |
| 224 |
|
| 225 |
if let Some(options) = &meta.bench_options { |
| 226 |
if let Some(true) = options.ignore { |
| 227 |
let uri = uri::generate(&entry, entry.display_name()); |
| 228 |
println!("Skipped: {uri}"); |
| 229 |
continue; |
| 230 |
} |
| 231 |
} |
| 232 |
match runner { |
| 233 |
entry::BenchEntryRunner::Plain(bench_fn) => { |
| 234 |
let uri = uri::generate(&entry, entry.display_name()); |
| 235 |
|
| 236 |
if !should_run_benchmark_from_filters(&uri) { |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
bench_fn(bench::Bencher::new(&codspeed, uri)); |
| 241 |
} |
| 242 |
entry::BenchEntryRunner::Args(bench_runner) => { |
| 243 |
let bench_runner = bench_runner(); |
| 244 |
|
| 245 |
for (arg_index, arg_name) in bench_runner.arg_names().iter().enumerate() { |
| 246 |
let uri = uri::generate(&entry, arg_name); |
| 247 |
|
| 248 |
if !should_run_benchmark_from_filters(&uri) { |
| 249 |
continue; |
| 250 |
} |
| 251 |
|
| 252 |
let bencher = bench::Bencher::new(&codspeed, uri); |
| 253 |
|
| 254 |
bench_runner.bench(bencher, arg_index); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |