| 1 |
//! Benchmarks for `time`. |
| 2 |
//! |
| 3 |
//! These benchmarks are not very precise, but they're good enough to catch major performance |
| 4 |
//! regressions. Run them if you think that may be the case. CI **does not** run benchmarks. |
| 5 |
|
| 6 |
#![allow( |
| 7 |
clippy::std_instead_of_core, |
| 8 |
clippy::std_instead_of_alloc, |
| 9 |
clippy::alloc_instead_of_core, |
| 10 |
reason = "irrelevant for benchmarks" |
| 11 |
)] |
| 12 |
#![allow( |
| 13 |
clippy::missing_docs_in_private_items, |
| 14 |
reason = "may be removed in the future" |
| 15 |
)] |
| 16 |
|
| 17 |
#[cfg(not(all( |
| 18 |
feature = "default", |
| 19 |
feature = "alloc", |
| 20 |
feature = "formatting", |
| 21 |
feature = "large-dates", |
| 22 |
feature = "local-offset", |
| 23 |
feature = "macros", |
| 24 |
feature = "parsing", |
| 25 |
feature = "quickcheck", |
| 26 |
feature = "serde-human-readable", |
| 27 |
feature = "serde-well-known", |
| 28 |
feature = "std", |
| 29 |
feature = "rand", |
| 30 |
feature = "serde", |
| 31 |
bench, |
| 32 |
)))] |
| 33 |
compile_error!( |
| 34 |
"benchmarks must be run as `RUSTFLAGS=\"--cfg bench\" cargo criterion --all-features`" |
| 35 |
); |
| 36 |
|
| 37 |
macro_rules! setup_benchmark { |
| 38 |
( |
| 39 |
$group_prefix:literal, |
| 40 |
$( |
| 41 |
$(#[$fn_attr:meta])* |
| 42 |
fn $fn_name:ident ($bencher:ident : $bencher_type:ty) |
| 43 |
$code:block |
| 44 |
)* |
| 45 |
) => { |
| 46 |
$( |
| 47 |
$(#[$fn_attr])* |
| 48 |
fn $fn_name( |
| 49 |
c: &mut ::criterion::Criterion |
| 50 |
) { |
| 51 |
c.bench_function( |
| 52 |
concat!($group_prefix, ": ", stringify!($fn_name)), |
| 53 |
|$bencher: $bencher_type| $code |
| 54 |
); |
| 55 |
} |
| 56 |
)* |
| 57 |
|
| 58 |
::criterion::criterion_group! { |
| 59 |
name = benches; |
| 60 |
config = ::criterion::Criterion::default() |
| 61 |
// Set a stricter statistical significance threshold ("p-value") |
| 62 |
// for deciding what's an actual performance change vs. noise. |
| 63 |
// The more benchmarks, the lower this needs to be in order to |
| 64 |
// not get lots of false positives. |
| 65 |
.significance_level(0.0001) |
| 66 |
// Ignore any performance change less than this (0.05 = 5%) as |
| 67 |
// noise, regardless of statistical significance. |
| 68 |
.noise_threshold(0.05) |
| 69 |
// Reduce the time taken to run each benchmark |
| 70 |
.warm_up_time(::std::time::Duration::from_millis(100)) |
| 71 |
.measurement_time(::std::time::Duration::from_millis(500)); |
| 72 |
targets = $($fn_name,)* |
| 73 |
} |
| 74 |
}; |
| 75 |
} |
| 76 |
|
| 77 |
macro_rules! iter_batched_ref { |
| 78 |
($ben:ident, $initializer:expr,[$($routine:expr),+ $(,)?]) => {$( |
| 79 |
$ben.iter_batched_ref( |
| 80 |
$initializer, |
| 81 |
$routine, |
| 82 |
::criterion::BatchSize::SmallInput, |
| 83 |
); |
| 84 |
)+}; |
| 85 |
} |
| 86 |
|
| 87 |
macro_rules! mods { |
| 88 |
($(mod $mod:ident;)+) => { |
| 89 |
$(mod $mod;)+ |
| 90 |
::criterion::criterion_main!($($mod::benches),+); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
mods![ |
| 95 |
mod date; |
| 96 |
mod duration; |
| 97 |
mod formatting; |
| 98 |
mod instant; |
| 99 |
mod month; |
| 100 |
mod offset_date_time; |
| 101 |
mod parse_format_description; |
| 102 |
mod parsing; |
| 103 |
mod plain_date_time; |
| 104 |
mod rand08; |
| 105 |
mod rand09; |
| 106 |
mod time; |
| 107 |
mod utc_offset; |
| 108 |
mod util; |
| 109 |
mod weekday; |
| 110 |
]; |
| 111 |
|
| 112 |
/// Shuffle a slice in a random but deterministic manner. |
| 113 |
fn shuffle<T, const N: usize>(mut slice: [T; N]) -> [T; N] { |
| 114 |
use ::rand09::prelude::*; |
| 115 |
|
| 116 |
let mut seed = SmallRng::seed_from_u64(0); |
| 117 |
slice.shuffle(&mut seed); |
| 118 |
slice |
| 119 |
} |