Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cargo-codspeed): build all benches in a single cargo build #34

Merged
merged 3 commits into from
Feb 10, 2024
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
2 changes: 1 addition & 1 deletion crates/cargo-codspeed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ keywords = ["codspeed", "benchmark", "cargo"]

[dependencies]
cargo = "0.66.0"
clap = { version = "4.0.29", features = ["derive"] }
clap = { version = "=4.0.29", features = ["derive"] }
termcolor = "1.0"
anyhow = "1.0.66"
itertools = "0.10.5"
Expand Down
53 changes: 26 additions & 27 deletions crates/cargo-codspeed/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn get_compile_options(
config: &Config,
features: &Option<Vec<String>>,
package: &Package,
bench: &str,
benches: Vec<&str>,
) -> Result<CompileOptions> {
let mut compile_opts = CompileOptions::new(config, CompileMode::Build)?;
compile_opts.spec = Packages::Packages(vec![package.name().to_string()]);
Expand All @@ -38,7 +38,7 @@ fn get_compile_options(
false,
vec![],
false,
vec![bench.into()],
benches.iter().map(|s| s.to_string()).collect(),
false,
false,
);
Expand Down Expand Up @@ -92,27 +92,20 @@ pub fn build_benches(
)?;

let config = ws.config();
let mut built_benches = vec![];
for bench in benches {
ws.config()
.shell()
.status_with_color("Building", bench.name(), Color::Yellow)?;
let compile_opts = get_compile_options(config, &features, package, bench.name())?;
let result = cargo::ops::compile(ws, &compile_opts)?;
let built_targets = result
.tests
.into_iter()
.filter(|u| u.unit.target.is_bench())
.collect_vec();
if let Some(built_bench) = built_targets.into_iter().next() {
built_benches.push(built_bench);
} else {
bail!("No benchmark target found.")
}
ws.config()
.shell()
.status_with_color("Built", bench.name(), Color::Green)?;
}

let benches_names = benches.iter().map(|t| t.name()).collect_vec();
let benches_names_str = benches_names.join(", ");

ws.config()
.shell()
.status_with_color("Building", benches_names_str.clone(), Color::Yellow)?;
let compile_opts = get_compile_options(config, &features, package, benches_names)?;
let result = cargo::ops::compile(ws, &compile_opts)?;
let built_benches = result
.tests
.into_iter()
.filter(|u| u.unit.target.is_bench())
.collect_vec();

if built_benches.is_empty() {
bail!(
Expand All @@ -121,6 +114,10 @@ pub fn build_benches(
);
}

ws.config()
.shell()
.status_with_color("Built", benches_names_str, Color::Green)?;

let mut codspeed_target_dir = get_codspeed_dir(ws);
create_dir_all(&codspeed_target_dir)?;
if let Some(name) = package_name.as_ref() {
Expand All @@ -129,14 +126,16 @@ pub fn build_benches(
}
clear_dir(&codspeed_target_dir)?;

for bench in built_benches.iter() {
let bench_dest = codspeed_target_dir.clone().join(bench.unit.target.name());
std::fs::copy(bench.path.clone(), bench_dest)?;
for built_bench in built_benches.iter() {
let bench_dest = codspeed_target_dir
.clone()
.join(built_bench.unit.target.name());
std::fs::copy(built_bench.path.clone(), bench_dest)?;
}

ws.config().shell().status_with_color(
"Finished",
format!("built {} benchmark suite(s)", built_benches.len()),
format!("built {} benchmark suite(s)", benches.len()),
Color::Green,
)?;

Expand Down
1 change: 1 addition & 0 deletions crates/cargo-codspeed/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(unused_imports)]
pub use anyhow::{anyhow, bail, Error, Ok, Result};
pub use cargo::core::Workspace;
pub use itertools::Itertools;
1 change: 0 additions & 1 deletion crates/criterion_compat/src/compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ mod macros;
pub use self::bencher::*;
pub use self::criterion::*;
pub use self::group::*;
pub use self::macros::*;
Loading