diff --git a/src/uu/shuf/benches/shuf_bench.rs b/src/uu/shuf/benches/shuf_bench.rs index 62c3be0bad7..39c4d46ffa4 100644 --- a/src/uu/shuf/benches/shuf_bench.rs +++ b/src/uu/shuf/benches/shuf_bench.rs @@ -5,7 +5,7 @@ use divan::{Bencher, black_box}; use uu_shuf::uumain; -use uucore::benchmark::{run_util_function, setup_test_file, text_data}; +use uucore::benchmark::{get_bench_args, setup_test_file, text_data}; /// Benchmark shuffling lines from a file /// Tests the default mode with a large number of lines @@ -13,11 +13,10 @@ use uucore::benchmark::{run_util_function, setup_test_file, text_data}; fn shuf_lines(bencher: Bencher, num_lines: usize) { let data = text_data::generate_by_lines(num_lines, 80); let file_path = setup_test_file(&data); - let file_path_str = file_path.to_str().unwrap(); - bencher.bench(|| { - black_box(run_util_function(uumain, &[file_path_str])); - }); + bencher + .with_inputs(|| get_bench_args(&[&file_path])) + .bench_values(|args| black_box(uumain(args))); } /// Benchmark shuffling a numeric range with -i @@ -26,9 +25,9 @@ fn shuf_lines(bencher: Bencher, num_lines: usize) { fn shuf_input_range(bencher: Bencher, range_size: usize) { let range_arg = format!("1-{range_size}"); - bencher.bench(|| { - black_box(run_util_function(uumain, &["-i", &range_arg])); - }); + bencher + .with_inputs(|| get_bench_args(&[&"-i", &range_arg])) + .bench_values(|args| black_box(uumain(args))); } /// Benchmark shuffling with repeat (sampling with replacement) @@ -37,15 +36,11 @@ fn shuf_input_range(bencher: Bencher, range_size: usize) { fn shuf_repeat_sampling(bencher: Bencher, num_lines: usize) { let data = text_data::generate_by_lines(10_000, 80); let file_path = setup_test_file(&data); - let file_path_str = file_path.to_str().unwrap(); let count = format!("{num_lines}"); - bencher.bench(|| { - black_box(run_util_function( - uumain, - &["-r", "-n", &count, file_path_str], - )); - }); + bencher + .with_inputs(|| get_bench_args(&[&"-r", &"-n", &count, &file_path])) + .bench_values(|args| black_box(uumain(args))); } fn main() { diff --git a/src/uucore/src/lib/features/benchmark.rs b/src/uucore/src/lib/features/benchmark.rs index e2dfac1e72a..1747c2e0433 100644 --- a/src/uucore/src/lib/features/benchmark.rs +++ b/src/uucore/src/lib/features/benchmark.rs @@ -40,6 +40,17 @@ where util_func(os_args.into_iter()) } +/// Prepare benchmark arguments for a utility function +pub fn get_bench_args( + args: &[&dyn AsRef], +) -> std::vec::IntoIter { + // Prepend a dummy program name as argv[0] since clap expects it + let os_args = std::iter::once("benchmark".into()) + .chain(args.iter().map(Into::into)) + .collect_vec(); + os_args.into_iter() +} + /// Helper function to set up a temporary test file and leak the temporary directory /// so it persists for the duration of the benchmark pub fn setup_test_file(data: &[u8]) -> PathBuf {