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