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: run benchmarks in non-bench targets #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{anyhow, Result};
use cargo::util::command_prelude::CompileMode;
use cargo::{
core::Workspace,
ops::CompileOptions,
Expand Down Expand Up @@ -147,7 +148,9 @@ fn build_target(cargo_options: &CargoOpts, workspace: &Workspace) -> Result<Path
.map(|unit_output| unit_output.path.clone())
.ok_or_else(|| anyhow!("no benchmark '{}'", bench))
} else {
match result.binaries.as_slice() {
let unit_outputs =
if cargo_options.mode == CompileMode::Bench { &result.tests } else { &result.binaries };
match unit_outputs.as_slice() {
[unit_output] => Ok(unit_output.path.clone()),
[] => Err(anyhow!("no targets found")),
other => Err(anyhow!(
Expand All @@ -166,10 +169,9 @@ fn build_target(cargo_options: &CargoOpts, workspace: &Workspace) -> Result<Path
/// This additionally filters options based on user args, so that Cargo
/// builds as little as possible.
fn make_compile_opts(cargo_options: &CargoOpts, cfg: &Config) -> Result<CompileOptions> {
use cargo::core::compiler::CompileMode;
use cargo::ops::CompileFilter;

let mut compile_options = CompileOptions::new(cfg, CompileMode::Build)?;
let mut compile_options = CompileOptions::new(cfg, cargo_options.mode)?;
let profile = &cargo_options.profile;

compile_options.build_config.requested_profile = InternedString::new(profile);
Expand Down
6 changes: 4 additions & 2 deletions src/instruments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,10 @@ pub(crate) fn profile_target(

command.arg(target_filepath);

if !app_config.target_args.is_empty() {
command.args(app_config.target_args.as_slice());
let bench_args = app_config.benchmarks.then_some("--bench".to_string());
let target_args = bench_args.iter().chain(&app_config.target_args).collect::<Vec<_>>();
if !target_args.is_empty() {
command.args(target_args.as_slice());
}

let output = command.output()?;
Expand Down
25 changes: 20 additions & 5 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use anyhow::Result;
use cargo::core::resolver::CliFeatures;
use cargo::ops::Packages;
use cargo::util::command_prelude::CompileMode;
use std::fmt;
use std::path::PathBuf;
use structopt::StructOpt;
Expand Down Expand Up @@ -53,6 +54,8 @@ pub(crate) struct AppConfig {
bin: Option<String>,

/// Benchmark target to run
///
/// Use --benchmarks to execute benchmarks in non-bench targets.
#[structopt(long, group = "target", value_name = "NAME")]
bench: Option<String>,

Expand All @@ -64,6 +67,10 @@ pub(crate) struct AppConfig {
#[structopt(long, value_name = "NAME")]
profile: Option<String>,

/// Execute benchmarks like `cargo bench`.
#[structopt(long)]
pub(crate) benchmarks: bool,

/// Output .trace file to the given path
///
/// Defaults to `target/instruments/{name}_{template-name}_{date}.trace`.
Expand Down Expand Up @@ -161,6 +168,7 @@ impl fmt::Display for Target {

/// Cargo-specific options
pub(crate) struct CargoOpts {
pub(crate) mode: CompileMode,
pub(crate) package: Package,
pub(crate) target: Target,
pub(crate) profile: String,
Expand All @@ -169,6 +177,7 @@ pub(crate) struct CargoOpts {

impl AppConfig {
pub(crate) fn to_cargo_opts(&self) -> Result<CargoOpts> {
let mode = if self.benchmarks { CompileMode::Bench } else { CompileMode::Build };
let package = self.get_package();
let target = self.get_target();
let features = self.features.clone().map(|s| vec![s]).unwrap_or_default();
Expand All @@ -177,11 +186,17 @@ impl AppConfig {
self.all_features,
!self.no_default_features,
)?;
let profile = self
.profile
.clone()
.unwrap_or_else(|| (if self.release { "release" } else { "dev" }).to_owned());
Ok(CargoOpts { package, target, profile, features })
let profile = self.profile.clone().unwrap_or_else(|| {
(if self.release {
"release"
} else if self.benchmarks {
"bench"
} else {
"dev"
})
.to_owned()
});
Ok(CargoOpts { mode, package, target, profile, features })
}

fn get_package(&self) -> Package {
Expand Down