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