Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/uu/shuf/benches/shuf_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@

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
#[divan::bench(args = [100_000])]
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
Expand All @@ -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)
Expand All @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions src/uucore/src/lib/features/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::ffi::OsStr>],
) -> std::vec::IntoIter<std::ffi::OsString> {
// 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 {
Expand Down
Loading