Skip to content
Open
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
63 changes: 27 additions & 36 deletions src/uu/base64/benches/base64_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,58 @@
// file that was distributed with this source code.

use divan::{Bencher, black_box};
use std::ffi::OsString;
use uu_base64::uumain;
use uucore::benchmark::{create_test_file, run_util_function, text_data};
use uucore::benchmark::{create_test_file, get_bench_args, text_data};

fn create_tmp_file(size_mb: usize) -> String {
fn create_tmp_file(size_mb: usize) -> std::path::PathBuf {
let temp_dir = tempfile::tempdir().unwrap();
let data = text_data::generate_by_size(size_mb, 80);
let file_path = create_test_file(&data, temp_dir.path());
String::from(file_path.to_str().unwrap())
create_test_file(&data, temp_dir.path())
}

fn redirect_in(in_file: &std::path::Path) -> std::ffi::OsString {
std::iter::once(std::ffi::OsString::from(">").as_os_str())
.chain(std::iter::once(in_file.as_os_str()))
.collect()
}

/// Benchmark for base64 encoding
#[divan::bench()]
fn b64_encode_synthetic(bencher: Bencher) {
let file_path_str = &create_tmp_file(5_000);
let file_path = create_tmp_file(5_000);

bencher.bench(|| {
black_box(run_util_function(uumain, &[file_path_str]));
});
bencher
.with_inputs(|| get_bench_args(&[&file_path]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

// Benchmark for base64 decoding
#[divan::bench()]
fn b64_decode_synthetic(bencher: Bencher) {
let temp_dir = tempfile::tempdir().unwrap();
let file_path_str = &create_tmp_file(5_000);
let file_path = create_tmp_file(5_000);
let in_file = create_test_file(b"", temp_dir.path());
let in_file_str = in_file.to_str().unwrap();
uumain(
[
OsString::from(file_path_str),
OsString::from(format!(">{in_file_str}")),
]
.iter()
.map(|x| (*x).clone()),
);

bencher.bench(|| {
black_box(run_util_function(uumain, &["-d", in_file_str]));
});
uumain([file_path.into(), redirect_in(&in_file)].into_iter());

bencher
.with_inputs(|| get_bench_args(&[&"-d", &in_file]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

// Benchmark different file sizes for base64 decoding ignoring garbage characters
#[divan::bench()]
fn b64_decode_ignore_garbage_synthetic(bencher: Bencher) {
let temp_dir = tempfile::tempdir().unwrap();
let file_path_str = &create_tmp_file(5_000);
let tempdir = tempfile::tempdir().unwrap();
let temp_dir = tempdir;
let file_path = create_tmp_file(5_000);
let in_file = create_test_file(b"", temp_dir.path());
let in_file_str = in_file.to_str().unwrap();
uumain(
[
OsString::from(file_path_str),
OsString::from(format!(">{in_file_str}")),
]
.iter()
.map(|x| (*x).clone()),
);

bencher.bench(|| {
black_box(run_util_function(uumain, &["-d", "-i", in_file_str]));
});
uumain([file_path.into(), redirect_in(&in_file)].into_iter());

bencher
.with_inputs(|| get_bench_args(&[&"-d", &"-i", &in_file]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

fn main() {
Expand Down
83 changes: 34 additions & 49 deletions src/uu/cksum/benches/cksum_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use divan::{Bencher, black_box};
use uu_cksum::uumain;
use uucore::benchmark::{run_util_function, setup_test_file, text_data};
use uucore::benchmark::{get_bench_args, setup_test_file, text_data};

// Macro to generate benchmarks for each algorithm
macro_rules! bench_algorithm {
Expand All @@ -15,12 +15,11 @@ macro_rules! bench_algorithm {
let data = text_data::generate_by_size(100, 80);
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(
uumain,
&["--algorithm", $algo_str, file_path.to_str().unwrap()],
));
});
bencher
.with_inputs(|| {
get_bench_args(&[&"--algorithm", &$algo_str, &file_path]).into_iter()
})
.bench_values(|args| black_box(uumain(args)));
}
};
($algo_name:ident, $algo_str:expr, $length:expr) => {
Expand All @@ -29,18 +28,18 @@ macro_rules! bench_algorithm {
let data = text_data::generate_by_size(100, 80);
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(
uumain,
&[
"--algorithm",
$algo_str,
"--length",
$length,
file_path.to_str().unwrap(),
],
));
});
bencher
.with_inputs(|| {
get_bench_args(&[
&"--algorithm",
&$algo_str,
&"--length",
&$length,
&file_path,
])
.into_iter()
})
.bench_values(|args| black_box(uumain(args)));
}
};
}
Expand Down Expand Up @@ -114,9 +113,9 @@ fn cksum_default(bencher: Bencher) {
let data = text_data::generate_by_size(100, 80);
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(uumain, &[file_path.to_str().unwrap()]));
});
bencher
.with_inputs(|| get_bench_args(&[&file_path]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

/// Benchmark cksum with raw output format
Expand All @@ -125,39 +124,25 @@ fn cksum_raw_output(bencher: Bencher) {
let data = text_data::generate_by_size(100, 80);
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(
uumain,
&["--raw", file_path.to_str().unwrap()],
));
});
bencher
.with_inputs(|| get_bench_args(&[&"--raw", &file_path]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

/// Benchmark cksum processing multiple files
#[divan::bench]
fn cksum_multiple_files(bencher: Bencher) {
let data1 = text_data::generate_by_size(50, 80);
let data2 = text_data::generate_by_size(50, 80);
let data3 = text_data::generate_by_size(50, 80);

let file1 = setup_test_file(&data1);
let file2 = setup_test_file(&data2);
let file3 = setup_test_file(&data3);

bencher
.with_inputs(|| {
let data1 = text_data::generate_by_size(50, 80);
let data2 = text_data::generate_by_size(50, 80);
let data3 = text_data::generate_by_size(50, 80);

let file1 = setup_test_file(&data1);
let file2 = setup_test_file(&data2);
let file3 = setup_test_file(&data3);

(file1, file2, file3)
})
.bench_values(|(file1, file2, file3)| {
black_box(run_util_function(
uumain,
&[
file1.to_str().unwrap(),
file2.to_str().unwrap(),
file3.to_str().unwrap(),
],
));
});
.with_inputs(|| get_bench_args(&[&file1, &file2, &file3]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

fn main() {
Expand Down
20 changes: 7 additions & 13 deletions src/uu/cp/benches/cp_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::fs;
use std::path::Path;
use tempfile::TempDir;
use uu_cp::uumain;
use uucore::benchmark::{binary_data, fs_tree, fs_utils, run_util_function};
use uucore::benchmark::{binary_data, fs_tree, fs_utils, get_bench_args, run_util_function};

fn bench_cp_directory<F>(bencher: Bencher, args: &[&str], setup_source: F)
where
Expand Down Expand Up @@ -87,20 +87,14 @@ fn cp_large_file(bencher: Bencher, size_mb: usize) {
let temp_dir = TempDir::new().unwrap();
let source = temp_dir.path().join("source.bin");
binary_data::create_file(&source, size_mb, b'x');
(temp_dir, source)
// Use unique destination name to avoid filesystem allocation variance
let dest = temp_dir
.path()
.join(format!("dest_{}.bin", (&raw const temp_dir).addr()));
get_bench_args(&[&source, &dest]).into_iter()
})
.counter(divan::counter::BytesCount::new(size_mb * 1024 * 1024))
.bench_values(|(temp_dir, source)| {
// Use unique destination name to avoid filesystem allocation variance
let dest = temp_dir.path().join(format!(
"dest_{}.bin",
std::ptr::addr_of!(temp_dir) as usize
));
let source_str = source.to_str().unwrap();
let dest_str = dest.to_str().unwrap();

black_box(run_util_function(uumain, &[source_str, dest_str]));
});
.bench_values(|args| black_box(uumain(args)));
}

fn main() {
Expand Down
47 changes: 16 additions & 31 deletions src/uu/cut/benches/cut_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@

use divan::{Bencher, black_box};
use uu_cut::uumain;
use uucore::benchmark::{run_util_function, setup_test_file, text_data};
use uucore::benchmark::{get_bench_args, setup_test_file, text_data};

/// Benchmark cutting specific byte ranges
#[divan::bench]
fn cut_bytes(bencher: Bencher) {
let data = text_data::generate_by_lines(100_000, 80);
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(
uumain,
&["-b", "1-20", file_path.to_str().unwrap()],
));
});
bencher
.with_inputs(|| get_bench_args(&[&"-b", &"1-20", &file_path]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

/// Benchmark cutting specific character ranges
Expand All @@ -27,12 +24,9 @@ fn cut_characters(bencher: Bencher) {
let data = text_data::generate_mixed_data(100_000);
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(
uumain,
&["-c", "5-30", file_path.to_str().unwrap()],
));
});
bencher
.with_inputs(|| get_bench_args(&[&"-c", &"5-30", &file_path]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

/// Benchmark cutting fields with tab delimiter
Expand All @@ -45,12 +39,9 @@ fn cut_fields_tab(bencher: Bencher) {
}
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(
uumain,
&["-f", "2,4", file_path.to_str().unwrap()],
));
});
bencher
.with_inputs(|| get_bench_args(&[&"-f", &"2,4", &file_path]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

/// Benchmark cutting fields with custom delimiter
Expand All @@ -63,12 +54,9 @@ fn cut_fields_custom_delim(bencher: Bencher) {
}
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(
uumain,
&["-d", ",", "-f", "1,3,5", file_path.to_str().unwrap()],
));
});
bencher
.with_inputs(|| get_bench_args(&[&"-d", &",", &"-f", &"1,3,5", &file_path]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

/// Benchmark cutting fields with newline delimiter
Expand All @@ -81,12 +69,9 @@ fn cut_fields_newline_delim(bencher: Bencher) {
}
let file_path = setup_test_file(&data);

bencher.bench(|| {
black_box(run_util_function(
uumain,
&["-d", "\n", "-f", "1,3,5", file_path.to_str().unwrap()],
));
});
bencher
.with_inputs(|| get_bench_args(&[&"-d", &"\n", &"-f", &"1,3,5", &file_path]).into_iter())
.bench_values(|args| black_box(uumain(args)));
}

fn main() {
Expand Down
Loading
Loading