From 6d0e5a83e83c7cf52d3b54b91bc19660c74dd952 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 15 Dec 2018 10:03:26 -0700 Subject: [PATCH] Add a "cargo bench --debug" option It builds the benchmarks in debug mode and then runs them. It's useful in a CI context where it's important that the benches aren't broken, but it's not important how fast they run. Fixes #6445 --- src/bin/cargo/commands/bench.rs | 8 +++++++- tests/testsuite/bench.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/bin/cargo/commands/bench.rs b/src/bin/cargo/commands/bench.rs index 087510e2555..31de0adcd12 100644 --- a/src/bin/cargo/commands/bench.rs +++ b/src/bin/cargo/commands/bench.rs @@ -6,6 +6,12 @@ pub fn cli() -> App { subcommand("bench") .setting(AppSettings::TrailingVarArg) .about("Execute all benchmarks of a local package") + .arg( + Arg::with_name("debug") + .help("Build benchmarks in debug mode, without optimizations") + .long("debug") + .short("d") + ) .arg( Arg::with_name("BENCHNAME") .help("If specified, only run benches containing this string in their names"), @@ -73,7 +79,7 @@ Compilation can be customized with the `bench` profile in the manifest. pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; let mut compile_opts = args.compile_options(config, CompileMode::Bench)?; - compile_opts.build_config.release = true; + compile_opts.build_config.release = !args.is_present("debug"); let ops = TestOptions { no_run: args.is_present("no-run"), diff --git a/tests/testsuite/bench.rs b/tests/testsuite/bench.rs index 2022abed8c3..10fab83f361 100644 --- a/tests/testsuite/bench.rs +++ b/tests/testsuite/bench.rs @@ -209,6 +209,37 @@ fn bench_multiple_targets() { .run(); } +#[test] +fn cargo_bench_debug() { + if !is_nightly() { + return; + } + + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file( + "src/main.rs", + r#" + #![feature(test)] + #[cfg(test)] + extern crate test; + fn main() {} + #[bench] fn bench_hello(_b: &mut test::Bencher) {} + "#, + ) + .build(); + + p.cargo("bench -d hello") + .with_stderr( + "\ +[COMPILING] foo v0.5.0 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[RUNNING] target/debug/deps/foo-[..][EXE]", + ) + .with_stdout_contains("test bench_hello ... bench: [..]") + .run(); +} + #[test] fn cargo_bench_verbose() { if !is_nightly() {