Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions crates/cargo-codspeed/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
app::{BenchTargetFilters, PackageFilters},
helpers::{clear_dir, get_codspeed_target_dir},
measurement_mode::MeasurementMode,
measurement_mode::{BuildMode, MeasurementMode},
prelude::*,
};
use anyhow::Context;
Expand Down Expand Up @@ -84,11 +84,11 @@ impl BuildOptions<'_> {
&self,
metadata: &Metadata,
quiet: bool,
measurement_mode: MeasurementMode,
build_mode: BuildMode,
) -> Result<Vec<BuiltBench>> {
let workspace_packages = metadata.workspace_packages();

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

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

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

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

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

self.add_rust_flags(&mut cargo, measurement_mode);
self.add_rust_flags(&mut cargo, build_mode);

if let Some(features) = self.features {
cargo.arg("--features").arg(features.join(","));
Expand Down Expand Up @@ -303,14 +301,15 @@ impl PackageFilters {
}

pub fn build_benches(metadata: &Metadata, config: BuildConfig) -> Result<()> {
let build_mode = config.measurement_mode.into();
let built_benches = BuildOptions {
bench_target_filters: config.bench_target_filters,
package_filters: config.package_filters,
features: &config.features,
profile: &config.profile,
passthrough_flags: &config.passthrough_flags,
}
.build(metadata, config.quiet, config.measurement_mode)?;
.build(metadata, config.quiet, build_mode)?;

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

let codspeed_target_dir = get_codspeed_target_dir(metadata, config.measurement_mode);
let codspeed_target_dir = get_codspeed_target_dir(metadata, build_mode);
let built_bench_count = built_benches.len();

// Create and clear packages codspeed target directories
Expand Down
6 changes: 3 additions & 3 deletions crates/cargo-codspeed/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{measurement_mode::MeasurementMode, prelude::*};
use crate::{measurement_mode::BuildMode, prelude::*};
use cargo_metadata::Metadata;
use std::path::{Path, PathBuf};

pub fn get_codspeed_target_dir(metadata: &Metadata, measurement_mode: MeasurementMode) -> PathBuf {
pub fn get_codspeed_target_dir(metadata: &Metadata, build_mode: BuildMode) -> PathBuf {
metadata
.target_directory
.join("codspeed")
.join(measurement_mode.to_string())
.join(build_mode.to_string())
.into()
}

Expand Down
33 changes: 30 additions & 3 deletions crates/cargo-codspeed/src/measurement_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,42 @@ use clap::ValueEnum;
use serde::Serialize;
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuildMode {
Analysis,
Walltime,
}

impl fmt::Display for BuildMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
BuildMode::Analysis => "analysis",
BuildMode::Walltime => "walltime",
}
)
}
}

#[derive(Debug, Clone, Copy, ValueEnum, Serialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum MeasurementMode {
#[default]
#[value(alias = "instrumentation")]
Simulation,
Walltime,
#[value(alias = "memory")]
Analysis,
Memory,
}

impl From<MeasurementMode> for BuildMode {
fn from(measurement_mode: MeasurementMode) -> Self {
match measurement_mode {
MeasurementMode::Simulation | MeasurementMode::Memory => BuildMode::Analysis,
MeasurementMode::Walltime => BuildMode::Walltime,
}
}
}

impl fmt::Display for MeasurementMode {
Expand All @@ -21,7 +48,7 @@ impl fmt::Display for MeasurementMode {
match self {
MeasurementMode::Simulation => "simulation",
MeasurementMode::Walltime => "walltime",
MeasurementMode::Analysis => "analysis",
MeasurementMode::Memory => "memory",
}
)
}
Expand Down
11 changes: 6 additions & 5 deletions crates/cargo-codspeed/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
app::{BenchTargetFilters, PackageFilters},
helpers::get_codspeed_target_dir,
measurement_mode::MeasurementMode,
measurement_mode::{BuildMode, MeasurementMode},
prelude::*,
};
use anyhow::Context;
Expand Down Expand Up @@ -100,9 +100,10 @@ pub fn run_benches(
bench_target_filters: BenchTargetFilters,
measurement_mode: MeasurementMode,
) -> Result<()> {
let codspeed_target_dir = get_codspeed_target_dir(metadata, measurement_mode);
let build_mode = measurement_mode.into();
let codspeed_target_dir = get_codspeed_target_dir(metadata, build_mode);
let workspace_root = metadata.workspace_root.as_std_path();
if measurement_mode == MeasurementMode::Walltime {
if build_mode == BuildMode::Walltime {
WalltimeResults::clear(workspace_root)?;
}
let benches =
Expand All @@ -124,7 +125,7 @@ pub fn run_benches(
.env("CODSPEED_CARGO_WORKSPACE_ROOT", workspace_root)
.current_dir(&bench.working_directory);

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

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

if measurement_mode == MeasurementMode::Walltime {
if build_mode == BuildMode::Walltime {
aggregate_raw_walltime_data(workspace_root)?;
}

Expand Down
Loading