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/support selecting multiple packages #43

Merged
merged 3 commits into from
Apr 24, 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,603 changes: 2,136 additions & 467 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions crates/cargo-codspeed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ categories = [
keywords = ["codspeed", "benchmark", "cargo"]

[dependencies]
cargo = "0.66.0"
clap = { version = "=4.0.29", features = ["derive"] }
cargo = "0.78.1"
clap = { version = "=4.5.4", features = ["derive"] }
termcolor = "1.0"
anyhow = "1.0.66"
itertools = "0.10.5"
anstyle = "1.0.6"

[dev-dependencies]
assert_cmd = "2.0.7"
Expand Down
61 changes: 45 additions & 16 deletions crates/cargo-codspeed/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{ffi::OsString, process::exit};

use crate::helpers::style;
use crate::{prelude::*, run::run_benches};

use cargo::util::important_paths::find_root_manifest_for_wd;
use cargo::Config;
use clap::{Parser, Subcommand};
use termcolor::Color;
use cargo::{ops::Packages, util::important_paths::find_root_manifest_for_wd};
use clap::{Args, Parser, Subcommand};

use crate::build::build_benches;

Expand All @@ -16,15 +16,30 @@ struct Cli {
command: Commands,
}

/// Package selection flags
#[derive(Args)]
struct PackageSelection {
/// Select all packages in the workspace
#[arg(long)]
workspace: bool,
/// Exclude packages
#[arg(long)]
exclude: Vec<String>,
/// Package to select
#[arg(short, long)]
package: Vec<String>,
}
art049 marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Subcommand)]
enum Commands {
/// Build the benchmarks
Build {
/// Optional list of benchmarks to build (builds all benchmarks by default)
benches: Option<Vec<String>>,
/// Package to build benchmarks for (if using a workspace)
#[arg(short, long)]
package: Option<String>,

#[command(flatten)]
package_selection: PackageSelection,

/// Space or comma separated list of features to activate
#[arg(short = 'F', long)]
features: Option<String>,
Expand All @@ -33,9 +48,9 @@ enum Commands {
Run {
/// Optional list of benchmarks to run (run all found benchmarks by default)
benches: Option<Vec<String>>,
/// Package to build benchmarks for (if using a workspace)
#[arg(short, long)]
package: Option<String>,

#[command(flatten)]
package_selection: PackageSelection,
},
}

Expand All @@ -50,29 +65,43 @@ pub fn run(args: impl Iterator<Item = OsString>) -> Result<()> {
let cli = Cli::try_parse_from(args)?;
let cargo_config = get_cargo_config()?;
let manifest_path = find_root_manifest_for_wd(cargo_config.cwd())?;
let workspace = Workspace::new(&manifest_path, &cargo_config)?;
let ws = Workspace::new(&manifest_path, &cargo_config)?;

let res = match cli.command {
Commands::Build {
benches,
package,
package_selection,
features,
} => {
let features = features.map(|f| {
f.split(|c| c == ' ' || c == ',')
.map(|s| s.to_string())
.collect_vec()
});
build_benches(&workspace, benches, package, features)
let packages = Packages::from_flags(
package_selection.workspace,
package_selection.exclude,
package_selection.package,
)?;
build_benches(&ws, benches, packages, features)
}
Commands::Run {
benches,
package_selection,
} => {
let packages = Packages::from_flags(
package_selection.workspace,
package_selection.exclude,
package_selection.package,
)?;
run_benches(&ws, benches, packages)
}
Commands::Run { benches, package } => run_benches(&workspace, benches, package),
};

if let Err(e) = res {
workspace
.config()
ws.config()
.shell()
.status_with_color("Error", e.to_string(), Color::Red)?;
.status_with_color("Error", e.to_string(), &style::ERROR)?;
exit(1);
}

Expand Down
13 changes: 6 additions & 7 deletions crates/cargo-codspeed/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
helpers::{clear_dir, get_codspeed_target_dir, get_target_packages},
helpers::{clear_dir, get_codspeed_target_dir, style},
prelude::*,
};

Expand All @@ -11,7 +11,6 @@ use cargo::{
util::{command_prelude::CompileMode, interning::InternedString},
Config,
};
use termcolor::Color;

fn get_compile_options(
config: &Config,
Expand Down Expand Up @@ -58,10 +57,10 @@ struct BenchToBuild<'a> {
pub fn build_benches(
ws: &Workspace,
selected_benches: Option<Vec<String>>,
package_name: Option<String>,
packages: Packages,
features: Option<Vec<String>>,
) -> Result<()> {
let packages_to_build = get_target_packages(&package_name, ws)?;
let packages_to_build = packages.get_packages(ws)?;
let mut benches_to_build = vec![];
for package in packages_to_build.iter() {
let benches = package
Expand Down Expand Up @@ -104,7 +103,7 @@ pub fn build_benches(
"".to_string()
}
),
Color::White,
&style::TITLE,
)?;

let config = ws.config();
Expand All @@ -120,7 +119,7 @@ pub fn build_benches(
ws.config().shell().status_with_color(
"Building",
format!("{} {}", bench.package.name(), bench.target.name()),
Color::Yellow,
&style::ACTIVE,
)?;
let is_root_package = ws.current_opt().map_or(false, |p| p == bench.package);
let benches_names = vec![bench.target.name()];
Expand Down Expand Up @@ -159,7 +158,7 @@ pub fn build_benches(
ws.config().shell().status_with_color(
"Finished",
format!("built {} benchmark suite(s)", built_benches),
Color::Green,
&style::SUCCESS,
)?;

Ok(())
Expand Down
39 changes: 15 additions & 24 deletions crates/cargo-codspeed/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::prelude::*;
use cargo::CargoResult;
use std::path::{Path, PathBuf};

pub fn get_codspeed_target_dir(ws: &Workspace) -> PathBuf {
Expand All @@ -9,29 +8,6 @@ pub fn get_codspeed_target_dir(ws: &Workspace) -> PathBuf {
.join("codspeed")
}

/// Get the packages to run benchmarks for
/// If a package name is provided, only that package is a target
/// If no package name is provided,
/// and the current directory is a package then only that package is a target
/// Otherwise all packages in the workspace are targets
pub fn get_target_packages<'a>(
package_name: &Option<String>,
ws: &'a Workspace<'_>,
) -> Result<Vec<&'a cargo::core::Package>> {
let packages_to_run = if let Some(package) = package_name.as_ref() {
let p = ws
.members()
.find(|m| m.manifest().name().to_string().as_str() == package)
.ok_or(anyhow!("Package {} not found", package))?;
vec![p]
} else if let CargoResult::Ok(p) = ws.current() {
vec![p]
} else {
ws.members().collect::<Vec<_>>()
};
Ok(packages_to_run)
}

pub fn clear_dir<P>(dir: P) -> Result<()>
where
P: AsRef<Path>,
Expand All @@ -47,3 +23,18 @@ where
}
Ok(())
}

pub mod style {
use anstyle::{AnsiColor, Color, Style};

pub const TITLE: Style = Style::new().bold();
pub const ERROR: Style = Style::new()
.fg_color(Some(Color::Ansi(AnsiColor::Red)))
.bold();
pub const SUCCESS: Style = Style::new()
.fg_color(Some(Color::Ansi(AnsiColor::Green)))
.bold();
pub const ACTIVE: Style = Style::new()
.fg_color(Some(Color::Ansi(AnsiColor::Yellow)))
.bold();
}
25 changes: 9 additions & 16 deletions crates/cargo-codspeed/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{io, path::PathBuf};

use anyhow::anyhow;
use termcolor::Color;
use cargo::ops::Packages;

use crate::{
helpers::{get_codspeed_target_dir, get_target_packages},
helpers::{get_codspeed_target_dir, style},
prelude::*,
};

Expand All @@ -18,10 +18,10 @@ struct BenchToRun {
pub fn run_benches(
ws: &Workspace,
selected_bench_names: Option<Vec<String>>,
package: Option<String>,
packages: Packages,
) -> Result<()> {
let codspeed_target_dir = get_codspeed_target_dir(ws);
let packages_to_run = get_target_packages(&package, ws)?;
let packages_to_run = packages.get_packages(ws)?;
let mut benches: Vec<BenchToRun> = vec![];
for p in packages_to_run {
let package_name = p.manifest().name().to_string();
Expand Down Expand Up @@ -50,14 +50,7 @@ pub fn run_benches(
}

if benches.is_empty() {
if let Some(package) = package.as_ref() {
bail!(
"No benchmarks found. Run `cargo codspeed build -p {}` first.",
package
);
} else {
bail!("No benchmarks found. Run `cargo codspeed build` first.");
}
bail!("No benchmarks found. Run `cargo codspeed build` first.");
}

let mut to_run = vec![];
Expand Down Expand Up @@ -86,7 +79,7 @@ pub fn run_benches(
ws.config().shell().status_with_color(
"Collected",
format!("{} benchmark suite(s) to run", to_run.len()),
Color::White,
&style::TITLE,
)?;
for bench in to_run.iter() {
let bench_name = &bench.bench_name;
Expand All @@ -96,7 +89,7 @@ pub fn run_benches(
ws.config().shell().status_with_color(
"Running",
format!("{} {}", &bench.package_name, bench_name),
Color::Yellow,
&style::ACTIVE,
)?;
std::process::Command::new(&bench.bench_path)
.env("CODSPEED_CARGO_WORKSPACE_ROOT", workspace_root.as_ref())
Expand All @@ -116,13 +109,13 @@ pub fn run_benches(
ws.config().shell().status_with_color(
"Done",
format!("running {}", bench_name),
Color::Green,
&style::SUCCESS,
)?;
}
ws.config().shell().status_with_color(
"Finished",
format!("running {} benchmark suite(s)", to_run.len()),
Color::Green,
&style::SUCCESS,
)?;
Ok(())
}
36 changes: 36 additions & 0 deletions crates/cargo-codspeed/tests/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,39 @@ fn test_workspace_build_both_and_run_all() {
.stderr(contains("Finished running 3 benchmark suite(s)"));
teardown(dir);
}

#[test]
fn test_workspace_build_both_and_run_all_explicitely() {
let dir = setup(DIR, Project::Workspace);
cargo_codspeed(&dir)
.arg("build")
.args(["--package", "package-a"])
.args(["--package", "package-b"])
.assert()
.success();
cargo_codspeed(&dir)
.arg("run")
.args(["--package", "package-a"])
.args(["--package", "package-b"])
.assert()
.success()
.stderr(contains("Finished running 3 benchmark suite(s)"));
teardown(dir);
}

#[test]
fn test_workspace_build_exclude() {
let dir = setup(DIR, Project::Workspace);
cargo_codspeed(&dir)
.arg("build")
.args(["--workspace", "--exclude", "package-b"])
.assert()
.success()
.stderr(contains("Finished built 1 benchmark suite(s)"));
cargo_codspeed(&dir)
.arg("run")
.assert()
.success()
.stderr(contains("Finished running 1 benchmark suite(s)"));
teardown(dir);
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.75.0"
channel = "1.77.2"
Loading