| 1 |
use std::{cmp::Ordering, error::Error, str::FromStr, time::Duration}; |
| 2 |
|
| 3 |
use regex::Regex; |
| 4 |
|
| 5 |
use crate::util::sort::natural_cmp; |
| 6 |
|
| 7 |
/// `Duration` wrapper for parsing seconds from the CLI. |
| 8 |
#[derive(Clone, Copy)] |
| 9 |
pub(crate) struct ParsedSeconds(pub Duration); |
| 10 |
|
| 11 |
impl FromStr for ParsedSeconds { |
| 12 |
type Err = Box<dyn Error + Send + Sync>; |
| 13 |
|
| 14 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 15 |
Ok(Self(Duration::try_from_secs_f64(f64::from_str(s)?)?)) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
/// The primary action to perform. |
| 20 |
#[derive(Clone, Copy, Default)] |
| 21 |
pub(crate) enum Action { |
| 22 |
/// Run benchmark loops. |
| 23 |
#[default] |
| 24 |
Bench, |
| 25 |
|
| 26 |
/// Run benchmarked functions once to ensure they run successfully. |
| 27 |
Test, |
| 28 |
|
| 29 |
/// List benchmarks. |
| 30 |
List, |
| 31 |
} |
| 32 |
|
| 33 |
#[allow(dead_code)] |
| 34 |
impl Action { |
| 35 |
#[inline] |
| 36 |
pub fn is_bench(&self) -> bool { |
| 37 |
matches!(self, Self::Bench) |
| 38 |
} |
| 39 |
|
| 40 |
#[inline] |
| 41 |
pub fn is_test(&self) -> bool { |
| 42 |
matches!(self, Self::Test) |
| 43 |
} |
| 44 |
|
| 45 |
#[inline] |
| 46 |
pub fn is_list(&self) -> bool { |
| 47 |
matches!(self, Self::List) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/// Filters which benchmark to run based on name. |
| 52 |
pub(crate) enum Filter { |
| 53 |
Regex(Regex), |
| 54 |
Exact(String), |
| 55 |
} |
| 56 |
|
| 57 |
impl Filter { |
| 58 |
/// Returns `true` if a string matches this filter. |
| 59 |
pub fn is_match(&self, s: &str) -> bool { |
| 60 |
match self { |
| 61 |
Self::Regex(r) => r.is_match(s), |
| 62 |
Self::Exact(e) => e == s, |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// How to treat benchmarks based on whether they're marked as `#[ignore]`. |
| 68 |
#[derive(Copy, Clone, Default)] |
| 69 |
pub(crate) enum RunIgnored { |
| 70 |
/// Skip ignored. |
| 71 |
#[default] |
| 72 |
No, |
| 73 |
|
| 74 |
/// `--include-ignored`. |
| 75 |
Yes, |
| 76 |
|
| 77 |
/// `--ignored`. |
| 78 |
Only, |
| 79 |
} |
| 80 |
|
| 81 |
impl RunIgnored { |
| 82 |
pub fn run_ignored(self) -> bool { |
| 83 |
matches!(self, Self::Yes | Self::Only) |
| 84 |
} |
| 85 |
|
| 86 |
pub fn run_non_ignored(self) -> bool { |
| 87 |
matches!(self, Self::Yes | Self::No) |
| 88 |
} |
| 89 |
|
| 90 |
pub fn should_run(self, ignored: bool) -> bool { |
| 91 |
if ignored { |
| 92 |
self.run_ignored() |
| 93 |
} else { |
| 94 |
self.run_non_ignored() |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/// The attribute to sort benchmarks by. |
| 100 |
#[derive(Clone, Copy, Default)] |
| 101 |
pub(crate) enum SortingAttr { |
| 102 |
/// Sort by kind, then by name and location. |
| 103 |
#[default] |
| 104 |
Kind, |
| 105 |
|
| 106 |
/// Sort by name, then by location and kind. |
| 107 |
Name, |
| 108 |
|
| 109 |
/// Sort by location, then by kind and name. |
| 110 |
Location, |
| 111 |
} |
| 112 |
|
| 113 |
impl SortingAttr { |
| 114 |
/// Returns an array containing `self` along with other attributes that |
| 115 |
/// should break ties if attributes are equal. |
| 116 |
pub fn with_tie_breakers(self) -> [Self; 3] { |
| 117 |
use SortingAttr::*; |
| 118 |
|
| 119 |
match self { |
| 120 |
Kind => [self, Name, Location], |
| 121 |
Name => [self, Location, Kind], |
| 122 |
Location => [self, Kind, Name], |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/// Compares benchmark runtime argument names. |
| 127 |
/// |
| 128 |
/// This takes `&&str` to handle `SortingAttr::Location` since the strings |
| 129 |
/// are considered to be within the same `&[&str]`. |
| 130 |
pub fn cmp_bench_arg_names(self, a: &&str, b: &&str) -> Ordering { |
| 131 |
for attr in self.with_tie_breakers() { |
| 132 |
let ordering = match attr { |
| 133 |
SortingAttr::Kind => Ordering::Equal, |
| 134 |
|
| 135 |
SortingAttr::Name => 'ordering: { |
| 136 |
// Compare as integers. |
| 137 |
match (a.parse::<u128>(), a.parse::<u128>()) { |
| 138 |
(Ok(a_u128), Ok(b_u128)) => break 'ordering a_u128.cmp(&b_u128), |
| 139 |
|
| 140 |
(Ok(_), Err(_)) => { |
| 141 |
if b.parse::<i128>().is_ok() { |
| 142 |
// a > b, because b is negative. |
| 143 |
break 'ordering Ordering::Greater; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
(Err(_), Ok(_)) => { |
| 148 |
if a.parse::<i128>().is_ok() { |
| 149 |
// a < b, because a is negative. |
| 150 |
break 'ordering Ordering::Less; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
(Err(_), Err(_)) => { |
| 155 |
if let (Ok(a_i128), Ok(b_i128)) = (a.parse::<i128>(), a.parse::<i128>()) |
| 156 |
{ |
| 157 |
break 'ordering a_i128.cmp(&b_i128); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// Compare as floats. |
| 163 |
if let (Ok(a), Ok(b)) = (a.parse::<f64>(), b.parse::<f64>()) { |
| 164 |
if let Some(ordering) = a.partial_cmp(&b) { |
| 165 |
break 'ordering ordering; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
natural_cmp(a, b) |
| 170 |
} |
| 171 |
|
| 172 |
SortingAttr::Location => { |
| 173 |
let a: *const &str = a; |
| 174 |
let b: *const &str = b; |
| 175 |
a.cmp(&b) |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
if ordering != Ordering::Equal { |
| 180 |
return ordering; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
Ordering::Equal |
| 185 |
} |
| 186 |
} |