Skip to content

Commit 8cc22d2

Browse files
committed
Auto merge of #3988 - sdroege:bench-all, r=alexcrichton
Add support for benchmarking all members of a workspace with "bench --all" Same behaviour as "build --all" and others. See #2878 (comment)
2 parents 7cfeec1 + 952f3b5 commit 8cc22d2

File tree

2 files changed

+148
-2
lines changed

2 files changed

+148
-2
lines changed

src/bin/bench.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cargo::core::Workspace;
2-
use cargo::ops::{self, MessageFormat};
2+
use cargo::ops::{self, MessageFormat, Packages};
33
use cargo::util::{CliResult, CliError, Human, Config, human};
44
use cargo::util::important_paths::{find_root_manifest_for_wd};
55

@@ -29,6 +29,7 @@ pub struct Options {
2929
flag_frozen: bool,
3030
flag_locked: bool,
3131
arg_args: Vec<String>,
32+
flag_all: bool,
3233
}
3334

3435
pub const USAGE: &'static str = "
@@ -50,6 +51,7 @@ Options:
5051
--benches Benchmark all benches
5152
--no-run Compile, but don't run benchmarks
5253
-p SPEC, --package SPEC ... Package to run benchmarks for
54+
--all Benchmark all packages in the workspace
5355
-j N, --jobs N Number of parallel jobs, defaults to # of CPUs
5456
--features FEATURES Space-separated list of features to also build
5557
--all-features Build all available features
@@ -72,6 +74,9 @@ which indicates which package should be benchmarked. If it is not given, then
7274
the current package is benchmarked. For more information on SPEC and its format,
7375
see the `cargo help pkgid` command.
7476
77+
All packages in the workspace are benchmarked if the `--all` flag is supplied. The
78+
`--all` flag may be supplied in the presence of a virtual manifest.
79+
7580
The --jobs argument affects the building of the benchmark executable but does
7681
not affect how many jobs are used when running the benchmarks.
7782
@@ -80,6 +85,13 @@ Compilation can be customized with the `bench` profile in the manifest.
8085

8186
pub fn execute(options: Options, config: &Config) -> CliResult {
8287
let root = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?;
88+
89+
let spec = if options.flag_all {
90+
Packages::All
91+
} else {
92+
Packages::Packages(&options.flag_package)
93+
};
94+
8395
config.configure(options.flag_verbose,
8496
options.flag_quiet,
8597
&options.flag_color,
@@ -96,7 +108,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
96108
features: &options.flag_features,
97109
all_features: options.flag_all_features,
98110
no_default_features: options.flag_no_default_features,
99-
spec: ops::Packages::Packages(&options.flag_package),
111+
spec: spec,
100112
release: true,
101113
mode: ops::CompileMode::Bench,
102114
filter: ops::CompileFilter::new(options.flag_lib,

tests/bench.rs

+134
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::str;
77
use cargo::util::process;
88
use cargotest::is_nightly;
99
use cargotest::support::paths::CargoPathExt;
10+
use cargotest::support::registry::Package;
1011
use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest};
1112
use hamcrest::{assert_that, existing_file};
1213

@@ -1128,3 +1129,136 @@ test bench_bar ... bench: 0 ns/iter (+/- 0)
11281129
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
11291130
"));
11301131
}
1132+
1133+
#[test]
1134+
fn bench_all_workspace() {
1135+
if !is_nightly() { return }
1136+
1137+
let p = project("foo")
1138+
.file("Cargo.toml", r#"
1139+
[project]
1140+
name = "foo"
1141+
version = "0.1.0"
1142+
1143+
[dependencies]
1144+
bar = { path = "bar" }
1145+
1146+
[workspace]
1147+
"#)
1148+
.file("src/main.rs", r#"
1149+
fn main() {}
1150+
"#)
1151+
.file("benches/foo.rs", r#"
1152+
#![feature(test)]
1153+
extern crate test;
1154+
1155+
use test::Bencher;
1156+
1157+
#[bench]
1158+
fn bench_foo(_: &mut Bencher) -> () { () }
1159+
"#)
1160+
.file("bar/Cargo.toml", r#"
1161+
[project]
1162+
name = "bar"
1163+
version = "0.1.0"
1164+
"#)
1165+
.file("bar/src/lib.rs", r#"
1166+
pub fn bar() {}
1167+
"#)
1168+
.file("bar/benches/bar.rs", r#"
1169+
#![feature(test)]
1170+
extern crate test;
1171+
1172+
use test::Bencher;
1173+
1174+
#[bench]
1175+
fn bench_bar(_: &mut Bencher) -> () { () }
1176+
"#);
1177+
1178+
assert_that(p.cargo_process("bench")
1179+
.arg("--all"),
1180+
execs().with_status(0)
1181+
.with_stderr_contains("\
1182+
[RUNNING] target[/]release[/]deps[/]bar-[..][EXE]")
1183+
.with_stdout_contains("
1184+
running 1 test
1185+
test bench_bar ... bench: 0 ns/iter (+/- 0)
1186+
1187+
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
1188+
")
1189+
.with_stderr_contains("\
1190+
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]")
1191+
.with_stdout_contains("
1192+
running 1 test
1193+
test bench_foo ... bench: 0 ns/iter (+/- 0)
1194+
1195+
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
1196+
"));
1197+
}
1198+
1199+
#[test]
1200+
fn bench_all_virtual_manifest() {
1201+
if !is_nightly() { return }
1202+
1203+
let p = project("workspace")
1204+
.file("Cargo.toml", r#"
1205+
[workspace]
1206+
members = ["foo", "bar"]
1207+
"#)
1208+
.file("foo/Cargo.toml", r#"
1209+
[project]
1210+
name = "foo"
1211+
version = "0.1.0"
1212+
"#)
1213+
.file("foo/src/lib.rs", r#"
1214+
pub fn foo() {}
1215+
"#)
1216+
.file("foo/benches/foo.rs", r#"
1217+
#![feature(test)]
1218+
extern crate test;
1219+
1220+
use test::Bencher;
1221+
1222+
#[bench]
1223+
fn bench_foo(_: &mut Bencher) -> () { () }
1224+
"#)
1225+
.file("bar/Cargo.toml", r#"
1226+
[project]
1227+
name = "bar"
1228+
version = "0.1.0"
1229+
"#)
1230+
.file("bar/src/lib.rs", r#"
1231+
pub fn bar() {}
1232+
"#)
1233+
.file("bar/benches/bar.rs", r#"
1234+
#![feature(test)]
1235+
extern crate test;
1236+
1237+
use test::Bencher;
1238+
1239+
#[bench]
1240+
fn bench_bar(_: &mut Bencher) -> () { () }
1241+
"#);
1242+
1243+
// The order in which foo and bar are built is not guaranteed
1244+
assert_that(p.cargo_process("bench")
1245+
.arg("--all"),
1246+
execs().with_status(0)
1247+
.with_stderr_contains("\
1248+
[RUNNING] target[/]release[/]deps[/]bar-[..][EXE]")
1249+
.with_stdout_contains("
1250+
running 1 test
1251+
test bench_bar ... bench: 0 ns/iter (+/- 0)
1252+
1253+
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
1254+
")
1255+
.with_stderr_contains("\
1256+
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]")
1257+
.with_stdout_contains("
1258+
running 1 test
1259+
test bench_foo ... bench: 0 ns/iter (+/- 0)
1260+
1261+
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
1262+
"));
1263+
}
1264+

0 commit comments

Comments
 (0)