Skip to content

Commit 2cb20e0

Browse files
committed
bench: reduce memory variance in cp and numfmt benchmarks
1 parent 2a044db commit 2cb20e0

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/uu/cp/benches/cp_bench.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,25 @@ fn cp_preserve_metadata(
8282

8383
#[divan::bench(args = [16])]
8484
fn cp_large_file(bencher: Bencher, size_mb: usize) {
85-
let temp_dir = TempDir::new().unwrap();
86-
let source = temp_dir.path().join("source.bin");
87-
let dest = temp_dir.path().join("dest.bin");
88-
89-
binary_data::create_file(&source, size_mb, b'x');
90-
91-
let source_str = source.to_str().unwrap();
92-
let dest_str = dest.to_str().unwrap();
93-
94-
bencher.bench(|| {
95-
fs_utils::remove_path(&dest);
96-
97-
black_box(run_util_function(uumain, &[source_str, dest_str]));
98-
});
85+
bencher
86+
.with_inputs(|| {
87+
let temp_dir = TempDir::new().unwrap();
88+
let source = temp_dir.path().join("source.bin");
89+
binary_data::create_file(&source, size_mb, b'x');
90+
(temp_dir, source)
91+
})
92+
.counter(divan::counter::BytesCount::new(size_mb * 1024 * 1024))
93+
.bench_values(|(temp_dir, source)| {
94+
// Use unique destination name to avoid filesystem allocation variance
95+
let dest = temp_dir.path().join(format!(
96+
"dest_{}.bin",
97+
std::ptr::addr_of!(temp_dir) as usize
98+
));
99+
let source_str = source.to_str().unwrap();
100+
let dest_str = dest.to_str().unwrap();
101+
102+
black_box(run_util_function(uumain, &[source_str, dest_str]));
103+
});
99104
}
100105

101106
fn main() {

src/uu/numfmt/benches/numfmt_bench.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,22 @@ fn numfmt_from_si(bencher: Bencher, count: usize) {
6363
/// Benchmark large numbers with SI formatting
6464
#[divan::bench(args = [10_000])]
6565
fn numfmt_large_numbers_si(bencher: Bencher, count: usize) {
66-
// Generate numbers that all produce uniform SI output lengths (all in 1-9M range)
67-
// This avoids variance from variable output string lengths
68-
let numbers: Vec<String> = (1..=count)
69-
.map(|n| ((n % 9) + 1) * 1_000_000)
70-
.map(|n| n.to_string())
71-
.collect();
72-
let mut args = vec!["--to=si"];
73-
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
74-
args.extend(number_refs);
75-
76-
bencher.bench(|| {
77-
black_box(run_util_function(uumain, &args));
78-
});
66+
bencher
67+
.with_inputs(|| {
68+
// Generate numbers that all produce uniform SI output lengths (all in 1-9M range)
69+
// This avoids variance from variable output string lengths
70+
let numbers: Vec<String> = (1..=count)
71+
.map(|n| ((n % 9) + 1) * 1_000_000)
72+
.map(|n| n.to_string())
73+
.collect();
74+
let mut args: Vec<String> = vec!["--to=si".to_string()];
75+
args.extend(numbers);
76+
args
77+
})
78+
.bench_values(|args| {
79+
let arg_refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
80+
black_box(run_util_function(uumain, &arg_refs));
81+
});
7982
}
8083

8184
/// Benchmark different padding widths

0 commit comments

Comments
 (0)