|
| 1 | +use std::path::PathBuf; |
| 2 | + |
1 | 3 | use anyhow::anyhow;
|
| 4 | +use cargo::core::Package; |
2 | 5 | use termcolor::Color;
|
3 | 6 |
|
4 | 7 | use crate::{
|
5 |
| - helpers::{get_codspeed_dir, read_dir_recursive}, |
| 8 | + helpers::{get_codspeed_target_dir, read_dir_recursive}, |
6 | 9 | prelude::*,
|
7 | 10 | };
|
8 | 11 |
|
| 12 | +struct BenchToRun { |
| 13 | + bench_path: PathBuf, |
| 14 | + bench_name: String, |
| 15 | + package_name: String, |
| 16 | + working_directory: PathBuf, |
| 17 | +} |
| 18 | + |
9 | 19 | pub fn run_benches(
|
10 | 20 | ws: &Workspace,
|
11 |
| - benches: Option<Vec<String>>, |
| 21 | + selected_bench_names: Option<Vec<String>>, |
12 | 22 | package: Option<String>,
|
13 | 23 | ) -> Result<()> {
|
14 |
| - let mut codspeed_dir = get_codspeed_dir(ws); |
| 24 | + let codspeed_target_dir = get_codspeed_target_dir(ws); |
15 | 25 |
|
16 |
| - if let Some(package) = package { |
17 |
| - codspeed_dir.push(package.clone()); |
18 |
| - if !codspeed_dir.exists() { |
19 |
| - return Err(anyhow!( |
| 26 | + let packages_to_run = if let Some(package) = package.as_ref() { |
| 27 | + let p = ws |
| 28 | + .members() |
| 29 | + .find(|m| m.manifest().name().to_string().as_str() == package); |
| 30 | + if let Some(p) = p { |
| 31 | + vec![p] |
| 32 | + } else { |
| 33 | + bail!("Package {} not found", package); |
| 34 | + } |
| 35 | + } else { |
| 36 | + ws.default_members().collect::<Vec<_>>() |
| 37 | + }; |
| 38 | + |
| 39 | + let mut benches: Vec<BenchToRun> = vec![]; |
| 40 | + for p in packages_to_run { |
| 41 | + let package_name = p.manifest().name().to_string(); |
| 42 | + let package_target_dir = codspeed_target_dir.join(&package_name); |
| 43 | + let working_directory = p.root().to_path_buf(); |
| 44 | + for entry in std::fs::read_dir(package_target_dir)? { |
| 45 | + let entry = entry?; |
| 46 | + let bench_path = entry.path(); |
| 47 | + let bench_name = bench_path |
| 48 | + .file_name() |
| 49 | + .unwrap() |
| 50 | + .to_str() |
| 51 | + .unwrap() |
| 52 | + .to_string(); |
| 53 | + if !bench_path.is_dir() { |
| 54 | + benches.push(BenchToRun { |
| 55 | + package_name: package_name.clone(), |
| 56 | + bench_path, |
| 57 | + bench_name, |
| 58 | + working_directory: working_directory.clone(), |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if benches.is_empty() { |
| 65 | + if let Some(package) = package.as_ref() { |
| 66 | + bail!( |
20 | 67 | "No benchmarks found. Run `cargo codspeed build -p {}` first.",
|
21 | 68 | package
|
22 |
| - )); |
| 69 | + ); |
| 70 | + } else { |
| 71 | + bail!("No benchmarks found. Run `cargo codspeed build` first."); |
23 | 72 | }
|
24 | 73 | }
|
25 |
| - if !codspeed_dir.exists() { |
26 |
| - return Err(anyhow!( |
27 |
| - "No benchmarks found. Run `cargo codspeed build` first." |
28 |
| - )); |
29 |
| - } |
30 |
| - |
31 |
| - let found_benches = read_dir_recursive(codspeed_dir)?; |
32 | 74 |
|
33 |
| - if found_benches.is_empty() { |
34 |
| - return Err(anyhow!( |
35 |
| - "No benchmark target found. Run `cargo codspeed build` first." |
36 |
| - )); |
37 |
| - } |
38 | 75 | let mut to_run = vec![];
|
39 |
| - if let Some(benches) = benches { |
| 76 | + if let Some(selected_bench_names) = selected_bench_names { |
40 | 77 | // Make sure all benchmarks are found
|
41 | 78 | let mut not_found = vec![];
|
42 |
| - for bench in benches.iter() { |
43 |
| - let bench_path = found_benches |
44 |
| - .iter() |
45 |
| - .find(|b| b.file_name().unwrap().to_str().unwrap() == bench); |
| 79 | + for bench_name in selected_bench_names.iter() { |
| 80 | + let bench = benches.iter().find(|b| &b.bench_name == bench_name); |
46 | 81 |
|
47 |
| - if let Some(bench_path) = bench_path { |
48 |
| - to_run.push(bench_path.clone()); |
| 82 | + if let Some(bench) = bench { |
| 83 | + to_run.push(bench); |
49 | 84 | } else {
|
50 |
| - not_found.push(bench); |
| 85 | + not_found.push(bench_name); |
51 | 86 | }
|
52 | 87 | }
|
53 | 88 |
|
54 | 89 | if !not_found.is_empty() {
|
55 |
| - return Err(anyhow!( |
| 90 | + bail!( |
56 | 91 | "The following benchmarks to run were not found: {}",
|
57 | 92 | not_found.iter().join(", ")
|
58 |
| - )); |
| 93 | + ); |
59 | 94 | }
|
60 | 95 | } else {
|
61 |
| - to_run = found_benches; |
| 96 | + to_run = benches.iter().collect(); |
62 | 97 | }
|
63 | 98 | ws.config().shell().status_with_color(
|
64 | 99 | "Collected",
|
65 | 100 | format!("{} benchmark suite(s) to run", to_run.len()),
|
66 | 101 | Color::White,
|
67 | 102 | )?;
|
68 | 103 | for bench in to_run.iter() {
|
69 |
| - let bench_name = bench.file_name().unwrap().to_str().unwrap(); |
| 104 | + let bench_name = &bench.bench_name; |
70 | 105 | // workspace_root is needed since file! returns the path relatively to the workspace root
|
71 | 106 | // while CARGO_MANIFEST_DIR returns the path to the sub package
|
72 | 107 | let workspace_root = ws.root().to_string_lossy();
|
73 | 108 | ws.config()
|
74 | 109 | .shell()
|
75 | 110 | .status_with_color("Running", bench_name, Color::Yellow)?;
|
76 |
| - std::process::Command::new(bench) |
| 111 | + std::process::Command::new(&bench.bench_path) |
77 | 112 | .env("CODSPEED_CARGO_WORKSPACE_ROOT", workspace_root.as_ref())
|
| 113 | + .current_dir(&bench.working_directory) |
78 | 114 | .status()
|
79 | 115 | .map_err(|_| anyhow!("failed to execute the benchmark process"))
|
80 | 116 | .and_then(|status| {
|
|
0 commit comments