bench: reduce memory variance in cp and numfmt benchmarks (#10283)

This commit is contained in:
Sylvestre Ledru
2026-01-17 16:01:04 +01:00
committed by GitHub
2 changed files with 34 additions and 26 deletions
+18 -13
View File
@@ -82,20 +82,25 @@ fn cp_preserve_metadata(
#[divan::bench(args = [16])]
fn cp_large_file(bencher: Bencher, size_mb: usize) {
let temp_dir = TempDir::new().unwrap();
let source = temp_dir.path().join("source.bin");
let dest = temp_dir.path().join("dest.bin");
bencher
.with_inputs(|| {
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)
})
.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();
binary_data::create_file(&source, size_mb, b'x');
let source_str = source.to_str().unwrap();
let dest_str = dest.to_str().unwrap();
bencher.bench(|| {
fs_utils::remove_path(&dest);
black_box(run_util_function(uumain, &[source_str, dest_str]));
});
black_box(run_util_function(uumain, &[source_str, dest_str]));
});
}
fn main() {
+16 -13
View File
@@ -63,19 +63,22 @@ fn numfmt_from_si(bencher: Bencher, count: usize) {
/// Benchmark large numbers with SI formatting
#[divan::bench(args = [10_000])]
fn numfmt_large_numbers_si(bencher: Bencher, count: usize) {
// Generate numbers that all produce uniform SI output lengths (all in 1-9M range)
// This avoids variance from variable output string lengths
let numbers: Vec<String> = (1..=count)
.map(|n| ((n % 9) + 1) * 1_000_000)
.map(|n| n.to_string())
.collect();
let mut args = vec!["--to=si"];
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
args.extend(number_refs);
bencher.bench(|| {
black_box(run_util_function(uumain, &args));
});
bencher
.with_inputs(|| {
// Generate numbers that all produce uniform SI output lengths (all in 1-9M range)
// This avoids variance from variable output string lengths
let numbers: Vec<String> = (1..=count)
.map(|n| ((n % 9) + 1) * 1_000_000)
.map(|n| n.to_string())
.collect();
let mut args: Vec<String> = vec!["--to=si".to_string()];
args.extend(numbers);
args
})
.bench_values(|args| {
let arg_refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
black_box(run_util_function(uumain, &arg_refs));
});
}
/// Benchmark different padding widths