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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::{
measurement_mode::{BuildMode, MeasurementMode},
prelude::*,
run::run_benches,
};
use cargo_metadata::MetadataCommand;
use clap::{ArgAction, Args, Parser, Subcommand};
use std::{ffi::OsString, process::exit};
use crate::build::{build_benches, BuildConfig};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Do not print cargo log messages
#[arg(short, long, global = true)]
quiet: bool,
#[command(subcommand)]
command: Commands,
}
impl Cli {
pub fn run(self) -> Result<()> {
let metadata = MetadataCommand::new().exec()?;
match self.command {
Commands::Build {
package_filters,
bench_target_filters,
features,
all_features,
jobs,
no_default_features,
profile,
locked,
offline,
frozen,
measurement_mode,
} => {
let passthrough_flags = {
let mut passthrough_flags = Vec::new();
if all_features {
passthrough_flags.push("--all-features".to_string());
}
if no_default_features {
passthrough_flags.push("--no-default-features".to_string());
}
if locked {
passthrough_flags.push("--locked".to_string());
}
if offline {
passthrough_flags.push("--offline".to_string());
}
if frozen {
passthrough_flags.push("--frozen".to_string());
}
if let Some(jobs) = jobs {
passthrough_flags.push(format!("--jobs={jobs}"));
}
passthrough_flags
};
let features =
features.map(|f| f.split([' ', ',']).map(|s| s.to_string()).collect_vec());
let modes = measurement_mode.iter().map(|m| m.to_string()).join(", ");
eprintln!(
"[cargo-codspeed] Measurement mode{}: {modes}\n",
if measurement_mode.len() > 1 { "s" } else { "" }
);
let build_modes: Vec<BuildMode> = measurement_mode
.into_iter()
.map(BuildMode::from)
.unique()
.collect();
let build_modes = if build_modes.is_empty() {
vec![BuildMode::default()]
} else {
build_modes
};
for build_mode in build_modes {
build_benches(
&metadata,
BuildConfig {
package_filters: package_filters.clone(),
bench_target_filters: bench_target_filters.clone(),
features: features.clone(),
profile: profile.clone(),
quiet: self.quiet,
build_mode,
passthrough_flags: passthrough_flags.clone(),
},
)?;
}
Ok(())
}
Commands::Run {
benchname,
package_filters,
bench_target_filters,
measurement_mode,
bench_args,
} => {
let mode = measurement_mode.unwrap_or_default();
eprintln!("[cargo-codspeed] Measurement mode: {mode:?}\n");
run_benches(
&metadata,
benchname,
package_filters,
bench_target_filters,
mode,
bench_args,
)
}
}
}
}
const PACKAGE_HELP: &str = "Package Selection";
#[derive(Args, Clone)]
pub(crate) struct PackageFilters {
/// Select all packages in the workspace
#[arg(long, help_heading = PACKAGE_HELP)]
pub(crate) workspace: bool,
/// Package to exclude
#[arg(long, value_name = "SPEC", help_heading = PACKAGE_HELP)]
pub(crate) exclude: Vec<String>,
/// Package to select
#[arg(short, long, value_name= "SPEC", help_heading = PACKAGE_HELP)]
pub(crate) package: Vec<String>,
}
#[derive(Args, Clone)]
pub(crate) struct BenchTargetFilters {
/// Select only the specified benchmark target; can be repeated (all benchmark targets by default)
#[arg(long, help_heading = TARGET_HELP)]
pub(crate) bench: Option<Vec<String>>,
}
// Help headings, should mostly match the headers from cargo build --help
const FEATURE_HELP: &str = "Feature Selection";
const COMPILATION_HELP: &str = "Compilation Options";
const TARGET_HELP: &str = "Target Selection";
const MANIFEST_HELP: &str = "Manifest Options";
#[derive(Subcommand)]
enum Commands {
/// Build the benchmarks
Build {
#[command(flatten)]
package_filters: PackageFilters,
/// Space or comma separated list of features to activate
#[arg(short = 'F', long, help_heading = FEATURE_HELP)]
features: Option<String>,
/// Activate all available features of all selected packages.
#[arg(long, help_heading = FEATURE_HELP)]
all_features: bool,
/// Do not activate the `default` feature of the selected packages.
#[arg(long, help_heading = FEATURE_HELP)]
no_default_features: bool,
/// Number of parallel jobs, defaults to # of CPUs.
#[arg(short, long, help_heading = COMPILATION_HELP)]
jobs: Option<u32>,
/// Build the benchmarks with the specified profile
#[arg(long, default_value = "bench", help_heading = COMPILATION_HELP)]
profile: String,
/// Assert that `Cargo.lock` will remain unchanged
#[arg(long, help_heading = MANIFEST_HELP)]
locked: bool,
/// Run without accessing the network
#[arg(long, help_heading = MANIFEST_HELP)]
offline: bool,
/// Equivalent to specifying both --locked and --offline
#[arg(long, help_heading = MANIFEST_HELP)]
frozen: bool,
#[command(flatten)]
bench_target_filters: BenchTargetFilters,
/// The measurement tool(s) to use for measuring performance.
/// Can be specified multiple times or comma-separated.
#[arg(
short = 'm',
long = "measurement-mode",
value_delimiter = ',',
action = ArgAction::Append,
help_heading = COMPILATION_HELP,
env = "CODSPEED_RUNNER_MODE"
)]
measurement_mode: Vec<MeasurementMode>,
},
/// Run the previously built benchmarks
Run {
/// Only run benchmarks whose name matches this unanchored regex.
///
/// The string matched against depends on the measurement mode:
/// - **simulation/memory**: the CodSpeed URI
/// - **walltime**: the benchmark framework's own name (can be listed with `-- --list`,
/// and validated with `-- --list '<filter>'`)
///
/// To require the whole name to match instead of a regex, pass `-- --exact`. The name format
/// still differs between modes.
#[arg(verbatim_doc_comment)]
benchname: Option<String>,
#[command(flatten)]
package_filters: PackageFilters,
#[command(flatten)]
bench_target_filters: BenchTargetFilters,
/// The measurement tool to use for measuring performance.
/// Automatically set to `walltime` on macro runners
#[arg(short = 'm', long = "measurement-mode", env = "CODSPEED_RUNNER_MODE")]
measurement_mode: Option<MeasurementMode>,
/// Arguments forwarded to each benchmark binary after `--`
#[arg(last = true)]
bench_args: Vec<String>,
},
}
pub fn run(args: impl Iterator<Item = OsString>) -> Result<()> {
let cli = Cli::try_parse_from(args)?;
if let Err(e) = cli.run() {
eprintln!("Error: {e}");
exit(1);
}
Ok(())
}