Skip to content

Commit 693edb3

Browse files
committed
feat(cargo-codspeed): add internal build mode
1 parent 0afd168 commit 693edb3

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

crates/cargo-codspeed/src/build.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
app::{BenchTargetFilters, PackageFilters},
33
helpers::{clear_dir, get_codspeed_target_dir},
4-
measurement_mode::MeasurementMode,
4+
measurement_mode::{BuildMode, MeasurementMode},
55
prelude::*,
66
};
77
use anyhow::Context;
@@ -84,11 +84,11 @@ impl BuildOptions<'_> {
8484
&self,
8585
metadata: &Metadata,
8686
quiet: bool,
87-
measurement_mode: MeasurementMode,
87+
build_mode: BuildMode,
8888
) -> Result<Vec<BuiltBench>> {
8989
let workspace_packages = metadata.workspace_packages();
9090

91-
let mut cargo = self.build_command(measurement_mode);
91+
let mut cargo = self.build_command(build_mode);
9292
if quiet {
9393
cargo.arg("--quiet");
9494
}
@@ -211,7 +211,7 @@ See `cargo codspeed build --help` for more information.");
211211
/// completely overrides rustflags from cargo config
212212
/// We use the cargo built-in config mechanism to set the flags if the user has not set
213213
/// `RUSTFLAGS`.
214-
fn add_rust_flags(&self, cargo: &mut Command, measurement_mode: MeasurementMode) {
214+
fn add_rust_flags(&self, cargo: &mut Command, build_mode: BuildMode) {
215215
let mut flags = vec![
216216
// Add debug info (equivalent to -g)
217217
"-Cdebuginfo=2".to_owned(),
@@ -226,9 +226,7 @@ See `cargo codspeed build --help` for more information.");
226226
];
227227

228228
// Add the codspeed cfg flag if the benchmark should only run once
229-
if measurement_mode == MeasurementMode::Simulation
230-
|| measurement_mode == MeasurementMode::Analysis
231-
{
229+
if build_mode == BuildMode::Analysis {
232230
flags.push("--cfg=codspeed".to_owned());
233231
}
234232

@@ -254,7 +252,7 @@ See `cargo codspeed build --help` for more information.");
254252

255253
/// Generates a subcommand to build the benchmarks by invoking cargo and forwarding the filters
256254
/// This command explicitly ignores the `self.benches`: all benches are built
257-
fn build_command(&self, measurement_mode: MeasurementMode) -> Command {
255+
fn build_command(&self, build_mode: BuildMode) -> Command {
258256
let mut cargo = Command::new("cargo");
259257
cargo.arg("build");
260258

@@ -266,7 +264,7 @@ See `cargo codspeed build --help` for more information.");
266264
cargo.args(["--benches"]);
267265
}
268266

269-
self.add_rust_flags(&mut cargo, measurement_mode);
267+
self.add_rust_flags(&mut cargo, build_mode);
270268

271269
if let Some(features) = self.features {
272270
cargo.arg("--features").arg(features.join(","));
@@ -303,14 +301,15 @@ impl PackageFilters {
303301
}
304302

305303
pub fn build_benches(metadata: &Metadata, config: BuildConfig) -> Result<()> {
304+
let build_mode = config.measurement_mode.into();
306305
let built_benches = BuildOptions {
307306
bench_target_filters: config.bench_target_filters,
308307
package_filters: config.package_filters,
309308
features: &config.features,
310309
profile: &config.profile,
311310
passthrough_flags: &config.passthrough_flags,
312311
}
313-
.build(metadata, config.quiet, config.measurement_mode)?;
312+
.build(metadata, config.quiet, build_mode)?;
314313

315314
if built_benches.is_empty() {
316315
bail!(
@@ -319,7 +318,7 @@ pub fn build_benches(metadata: &Metadata, config: BuildConfig) -> Result<()> {
319318
);
320319
}
321320

322-
let codspeed_target_dir = get_codspeed_target_dir(metadata, config.measurement_mode);
321+
let codspeed_target_dir = get_codspeed_target_dir(metadata, build_mode);
323322
let built_bench_count = built_benches.len();
324323

325324
// Create and clear packages codspeed target directories

crates/cargo-codspeed/src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::{measurement_mode::MeasurementMode, prelude::*};
1+
use crate::{measurement_mode::BuildMode, prelude::*};
22
use cargo_metadata::Metadata;
33
use std::path::{Path, PathBuf};
44

5-
pub fn get_codspeed_target_dir(metadata: &Metadata, measurement_mode: MeasurementMode) -> PathBuf {
5+
pub fn get_codspeed_target_dir(metadata: &Metadata, build_mode: BuildMode) -> PathBuf {
66
metadata
77
.target_directory
88
.join("codspeed")
9-
.join(measurement_mode.to_string())
9+
.join(build_mode.to_string())
1010
.into()
1111
}
1212

crates/cargo-codspeed/src/measurement_mode.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,42 @@ use clap::ValueEnum;
22
use serde::Serialize;
33
use std::fmt;
44

5+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6+
pub enum BuildMode {
7+
Analysis,
8+
Walltime,
9+
}
10+
11+
impl fmt::Display for BuildMode {
12+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13+
write!(
14+
f,
15+
"{}",
16+
match self {
17+
BuildMode::Analysis => "analysis",
18+
BuildMode::Walltime => "walltime",
19+
}
20+
)
21+
}
22+
}
23+
524
#[derive(Debug, Clone, Copy, ValueEnum, Serialize, PartialEq, Eq, Default)]
625
#[serde(rename_all = "lowercase")]
726
pub enum MeasurementMode {
827
#[default]
928
#[value(alias = "instrumentation")]
1029
Simulation,
1130
Walltime,
12-
#[value(alias = "memory")]
13-
Analysis,
31+
Memory,
32+
}
33+
34+
impl From<MeasurementMode> for BuildMode {
35+
fn from(measurement_mode: MeasurementMode) -> Self {
36+
match measurement_mode {
37+
MeasurementMode::Simulation | MeasurementMode::Memory => BuildMode::Analysis,
38+
MeasurementMode::Walltime => BuildMode::Walltime,
39+
}
40+
}
1441
}
1542

1643
impl fmt::Display for MeasurementMode {
@@ -21,7 +48,7 @@ impl fmt::Display for MeasurementMode {
2148
match self {
2249
MeasurementMode::Simulation => "simulation",
2350
MeasurementMode::Walltime => "walltime",
24-
MeasurementMode::Analysis => "analysis",
51+
MeasurementMode::Memory => "memory",
2552
}
2653
)
2754
}

crates/cargo-codspeed/src/run.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
app::{BenchTargetFilters, PackageFilters},
33
helpers::get_codspeed_target_dir,
4-
measurement_mode::MeasurementMode,
4+
measurement_mode::{BuildMode, MeasurementMode},
55
prelude::*,
66
};
77
use anyhow::Context;
@@ -100,9 +100,10 @@ pub fn run_benches(
100100
bench_target_filters: BenchTargetFilters,
101101
measurement_mode: MeasurementMode,
102102
) -> Result<()> {
103-
let codspeed_target_dir = get_codspeed_target_dir(metadata, measurement_mode);
103+
let build_mode = measurement_mode.into();
104+
let codspeed_target_dir = get_codspeed_target_dir(metadata, build_mode);
104105
let workspace_root = metadata.workspace_root.as_std_path();
105-
if measurement_mode == MeasurementMode::Walltime {
106+
if build_mode == BuildMode::Walltime {
106107
WalltimeResults::clear(workspace_root)?;
107108
}
108109
let benches =
@@ -124,7 +125,7 @@ pub fn run_benches(
124125
.env("CODSPEED_CARGO_WORKSPACE_ROOT", workspace_root)
125126
.current_dir(&bench.working_directory);
126127

127-
if measurement_mode == MeasurementMode::Walltime {
128+
if build_mode == BuildMode::Walltime {
128129
command.arg("--bench"); // Walltime targets need this additional argument (inherited from running them with `cargo bench`)
129130
}
130131

@@ -160,7 +161,7 @@ pub fn run_benches(
160161
}
161162
eprintln!("Finished running {} benchmark suite(s)", benches.len());
162163

163-
if measurement_mode == MeasurementMode::Walltime {
164+
if build_mode == BuildMode::Walltime {
164165
aggregate_raw_walltime_data(workspace_root)?;
165166
}
166167

0 commit comments

Comments
 (0)