Skip to content

Commit 75ad901

Browse files
committed
benchmarks: prepare args outside benchmark
1 parent b2b584e commit 75ad901

2 files changed

Lines changed: 59 additions & 89 deletions

File tree

src/uu/sort/benches/sort_bench.rs

Lines changed: 48 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,19 @@
66
use divan::{Bencher, black_box};
77
use tempfile::NamedTempFile;
88
use uu_sort::uumain;
9-
use uucore::benchmark::{run_util_function, setup_test_file, text_data};
9+
use uucore::benchmark::{get_benchmark_args, setup_test_file, text_data};
1010

1111
/// Benchmark sorting ASCII-only data
1212
#[divan::bench(args = [500_000])]
1313
fn sort_ascii_only(bencher: Bencher, num_lines: usize) {
1414
let data = text_data::generate_ascii_data(num_lines);
1515
let file_path = setup_test_file(&data);
1616
let output_file = NamedTempFile::new().unwrap();
17-
let output_path = output_file.path().to_str().unwrap();
17+
let output_path = output_file.path();
1818

19-
bencher.bench(|| {
20-
black_box(run_util_function(
21-
uumain,
22-
&["-o", output_path, file_path.to_str().unwrap()],
23-
));
24-
});
19+
bencher
20+
.with_inputs(|| get_benchmark_args(&[&"-o", &output_path, &file_path]))
21+
.bench_values(|args| black_box(uumain(args)));
2522
}
2623

2724
/// Benchmark sorting accented/non-ASCII data
@@ -30,14 +27,11 @@ fn sort_accented_data(bencher: Bencher, num_lines: usize) {
3027
let data = text_data::generate_accented_data(num_lines);
3128
let file_path = setup_test_file(&data);
3229
let output_file = NamedTempFile::new().unwrap();
33-
let output_path = output_file.path().to_str().unwrap();
30+
let output_path = output_file.path();
3431

35-
bencher.bench(|| {
36-
black_box(run_util_function(
37-
uumain,
38-
&["-o", output_path, file_path.to_str().unwrap()],
39-
));
40-
});
32+
bencher
33+
.with_inputs(|| get_benchmark_args(&[&"-o", &output_path, &file_path]))
34+
.bench_values(|args| black_box(uumain(args)));
4135
}
4236

4337
/// Benchmark sorting mixed ASCII/non-ASCII data
@@ -46,14 +40,11 @@ fn sort_mixed_data(bencher: Bencher, num_lines: usize) {
4640
let data = text_data::generate_mixed_data(num_lines);
4741
let file_path = setup_test_file(&data);
4842
let output_file = NamedTempFile::new().unwrap();
49-
let output_path = output_file.path().to_str().unwrap();
43+
let output_path = output_file.path();
5044

51-
bencher.bench(|| {
52-
black_box(run_util_function(
53-
uumain,
54-
&["-o", output_path, file_path.to_str().unwrap()],
55-
));
56-
});
45+
bencher
46+
.with_inputs(|| get_benchmark_args(&[&"-o", &output_path, &file_path]))
47+
.bench_values(|args| black_box(uumain(args)));
5748
}
5849

5950
/// Benchmark case-sensitive sorting with mixed case data
@@ -62,14 +53,11 @@ fn sort_case_sensitive(bencher: Bencher, num_lines: usize) {
6253
let data = text_data::generate_case_sensitive_data(num_lines);
6354
let file_path = setup_test_file(&data);
6455
let output_file = NamedTempFile::new().unwrap();
65-
let output_path = output_file.path().to_str().unwrap();
56+
let output_path = output_file.path();
6657

67-
bencher.bench(|| {
68-
black_box(run_util_function(
69-
uumain,
70-
&["-o", output_path, file_path.to_str().unwrap()],
71-
));
72-
});
58+
bencher
59+
.with_inputs(|| get_benchmark_args(&[&"-o", &output_path, &file_path]))
60+
.bench_values(|args| black_box(uumain(args)));
7361
}
7462

7563
/// Benchmark case-insensitive sorting (fold case)
@@ -78,14 +66,11 @@ fn sort_case_insensitive(bencher: Bencher, num_lines: usize) {
7866
let data = text_data::generate_case_sensitive_data(num_lines);
7967
let file_path = setup_test_file(&data);
8068
let output_file = NamedTempFile::new().unwrap();
81-
let output_path = output_file.path().to_str().unwrap();
69+
let output_path = output_file.path();
8270

83-
bencher.bench(|| {
84-
black_box(run_util_function(
85-
uumain,
86-
&["-f", "-o", output_path, file_path.to_str().unwrap()],
87-
));
88-
});
71+
bencher
72+
.with_inputs(|| get_benchmark_args(&[&"-f", &"-o", &output_path, &file_path]))
73+
.bench_values(|args| black_box(uumain(args)));
8974
}
9075

9176
/// Benchmark dictionary order sorting (only blanks and alphanumeric)
@@ -94,14 +79,11 @@ fn sort_dictionary_order(bencher: Bencher, num_lines: usize) {
9479
let data = text_data::generate_mixed_data(num_lines);
9580
let file_path = setup_test_file(&data);
9681
let output_file = NamedTempFile::new().unwrap();
97-
let output_path = output_file.path().to_str().unwrap();
82+
let output_path = output_file.path();
9883

99-
bencher.bench(|| {
100-
black_box(run_util_function(
101-
uumain,
102-
&["-d", "-o", output_path, file_path.to_str().unwrap()],
103-
));
104-
});
84+
bencher
85+
.with_inputs(|| get_benchmark_args(&[&"-d", &"-o", &output_path, &file_path]))
86+
.bench_values(|args| black_box(uumain(args)));
10587
}
10688

10789
/// Benchmark numeric sorting with mixed data
@@ -118,14 +100,11 @@ fn sort_numeric(bencher: Bencher, num_lines: usize) {
118100
let file_path = setup_test_file(&data);
119101

120102
let output_file = NamedTempFile::new().unwrap();
121-
let output_path = output_file.path().to_str().unwrap();
103+
let output_path = output_file.path();
122104

123-
bencher.bench(|| {
124-
black_box(run_util_function(
125-
uumain,
126-
&["-n", "-o", output_path, file_path.to_str().unwrap()],
127-
));
128-
});
105+
bencher
106+
.with_inputs(|| get_benchmark_args(&[&"-n", &"-o", &output_path, &file_path]))
107+
.bench_values(|args| black_box(uumain(args)));
129108
}
130109

131110
/// Benchmark general numeric sorting (-g) with decimal and exponent notation
@@ -146,12 +125,9 @@ fn sort_general_numeric(bencher: Bencher, num_lines: usize) {
146125
let output_file = NamedTempFile::new().unwrap();
147126
let output_path = output_file.path().to_str().unwrap();
148127

149-
bencher.bench(|| {
150-
black_box(run_util_function(
151-
uumain,
152-
&["-g", "-o", output_path, file_path.to_str().unwrap()],
153-
));
154-
});
128+
bencher
129+
.with_inputs(|| get_benchmark_args(&[&"-g", &"-o", &output_path, &file_path]))
130+
.bench_values(|args| black_box(uumain(args)));
155131
}
156132

157133
/// Benchmark reverse sorting with locale-aware data
@@ -160,14 +136,11 @@ fn sort_reverse_locale(bencher: Bencher, num_lines: usize) {
160136
let data = text_data::generate_accented_data(num_lines);
161137
let file_path = setup_test_file(&data);
162138
let output_file = NamedTempFile::new().unwrap();
163-
let output_path = output_file.path().to_str().unwrap();
139+
let output_path = output_file.path();
164140

165-
bencher.bench(|| {
166-
black_box(run_util_function(
167-
uumain,
168-
&["-r", "-o", output_path, file_path.to_str().unwrap()],
169-
));
170-
});
141+
bencher
142+
.with_inputs(|| get_benchmark_args(&[&"-r", &"-o", &output_path, &file_path]))
143+
.bench_values(|args| black_box(uumain(args)));
171144
}
172145

173146
/// Benchmark sorting with specific key field
@@ -187,15 +160,12 @@ fn sort_key_field(bencher: Bencher, num_lines: usize) {
187160
let file_path = setup_test_file(&data);
188161

189162
let output_file = NamedTempFile::new().unwrap();
190-
let output_path = output_file.path().to_str().unwrap();
163+
let output_path = output_file.path();
191164

192-
bencher.bench(|| {
165+
bencher
193166
// Sort by second field
194-
black_box(run_util_function(
195-
uumain,
196-
&["-k", "2", "-o", output_path, file_path.to_str().unwrap()],
197-
));
198-
});
167+
.with_inputs(|| get_benchmark_args(&[&"-k", &"2", &"-o", &output_path, &file_path]))
168+
.bench_values(|args| black_box(uumain(args)));
199169
}
200170

201171
/// Benchmark unique sorting with locale-aware data
@@ -204,14 +174,11 @@ fn sort_unique_locale(bencher: Bencher, num_lines: usize) {
204174
let data = text_data::generate_accented_data(num_lines);
205175
let file_path = setup_test_file(&data);
206176
let output_file = NamedTempFile::new().unwrap();
207-
let output_path = output_file.path().to_str().unwrap();
177+
let output_path = output_file.path();
208178

209-
bencher.bench(|| {
210-
black_box(run_util_function(
211-
uumain,
212-
&["-u", "-o", output_path, file_path.to_str().unwrap()],
213-
));
214-
});
179+
bencher
180+
.with_inputs(|| get_benchmark_args(&[&"-u", &"-o", &output_path, &file_path]))
181+
.bench_values(|args| black_box(uumain(args)));
215182
}
216183

217184
/// Benchmark sorting with very long lines exceeding START_BUFFER_SIZE (8000 bytes)
@@ -227,19 +194,11 @@ fn sort_long_line(bencher: Bencher, line_size: usize) {
227194
let file_a = setup_test_file(&data_a);
228195
let file_b = setup_test_file(&data_b);
229196
let output_file = NamedTempFile::new().unwrap();
230-
let output_path = output_file.path().to_str().unwrap();
197+
let output_path = output_file.path();
231198

232-
bencher.bench(|| {
233-
black_box(run_util_function(
234-
uumain,
235-
&[
236-
file_a.to_str().unwrap(),
237-
file_b.to_str().unwrap(),
238-
"-o",
239-
output_path,
240-
],
241-
));
242-
});
199+
bencher
200+
.with_inputs(|| get_benchmark_args(&[&file_a, &file_b, &"-o", &output_path]))
201+
.bench_values(|args| black_box(uumain(args)));
243202
}
244203

245204
fn main() {

src/uucore/src/lib/features/benchmark.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ where
4040
util_func(os_args.into_iter())
4141
}
4242

43+
/// Prepare benchmark arguments for a utility function
44+
pub fn get_benchmark_args(
45+
args: &[&dyn AsRef<std::ffi::OsStr>],
46+
) -> std::vec::IntoIter<std::ffi::OsString> {
47+
// Prepend a dummy program name as argv[0] since clap expects it
48+
let os_args = std::iter::once("benchmark".into())
49+
.chain(args.iter().map(Into::into))
50+
.collect_vec();
51+
os_args.into_iter()
52+
}
53+
4354
/// Helper function to set up a temporary test file and leak the temporary directory
4455
/// so it persists for the duration of the benchmark
4556
pub fn setup_test_file(data: &[u8]) -> PathBuf {

0 commit comments

Comments
 (0)