Skip to content

Commit d1cd999

Browse files
committed
fix some benchmark (some of them were failing)
1 parent 5c15d79 commit d1cd999

File tree

4 files changed

+139
-71
lines changed

4 files changed

+139
-71
lines changed

src/uu/numfmt/benches/numfmt_bench.rs

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,108 +5,100 @@
55

66
use divan::{Bencher, black_box};
77
use uu_numfmt::uumain;
8-
use uucore::benchmark::{run_util_function, setup_test_file, text_data};
8+
use uucore::benchmark::run_util_function;
99

10-
/// Benchmark SI formatting with different number counts
11-
#[divan::bench(args = [1_000_000])]
10+
/// Benchmark SI formatting by passing numbers as command-line arguments
11+
#[divan::bench(args = [10_000])]
1212
fn numfmt_to_si(bencher: Bencher, count: usize) {
13-
let data = text_data::generate_numbers(count);
14-
let file_path = setup_test_file(data.as_bytes());
15-
let file_path_str = file_path.to_str().unwrap();
13+
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect();
14+
let mut args = vec!["--to=si"];
15+
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
16+
args.extend(number_refs);
1617

1718
bencher.bench(|| {
18-
black_box(run_util_function(uumain, &["--to=si", file_path_str]));
19+
black_box(run_util_function(uumain, &args));
1920
});
2021
}
2122

2223
/// Benchmark SI formatting with precision format
23-
#[divan::bench(args = [1_000_000])]
24+
#[divan::bench(args = [10_000])]
2425
fn numfmt_to_si_precision(bencher: Bencher, count: usize) {
25-
let data = text_data::generate_numbers(count);
26-
let file_path = setup_test_file(data.as_bytes());
27-
let file_path_str = file_path.to_str().unwrap();
26+
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect();
27+
let mut args = vec!["--to=si", "--format=%.6f"];
28+
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
29+
args.extend(number_refs);
2830

2931
bencher.bench(|| {
30-
black_box(run_util_function(
31-
uumain,
32-
&["--to=si", "--format=%.6f", file_path_str],
33-
));
32+
black_box(run_util_function(uumain, &args));
3433
});
3534
}
3635

3736
/// Benchmark IEC (binary) formatting
38-
#[divan::bench(args = [1_000_000])]
37+
#[divan::bench(args = [10_000])]
3938
fn numfmt_to_iec(bencher: Bencher, count: usize) {
40-
let data = text_data::generate_numbers(count);
41-
let file_path = setup_test_file(data.as_bytes());
42-
let file_path_str = file_path.to_str().unwrap();
39+
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect();
40+
let mut args = vec!["--to=iec"];
41+
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
42+
args.extend(number_refs);
4343

4444
bencher.bench(|| {
45-
black_box(run_util_function(uumain, &["--to=iec", file_path_str]));
45+
black_box(run_util_function(uumain, &args));
4646
});
4747
}
4848

4949
/// Benchmark parsing from SI format back to raw numbers
50-
#[divan::bench(args = [1_000_000])]
50+
#[divan::bench(args = [10_000])]
5151
fn numfmt_from_si(bencher: Bencher, count: usize) {
52-
// Generate SI formatted data (e.g., "1.0K", "2.0K", etc.)
53-
let data = (1..=count)
54-
.map(|n| format!("{:.1}K", n as f64 / 1000.0))
55-
.collect::<Vec<_>>()
56-
.join("\n");
57-
let file_path = setup_test_file(data.as_bytes());
58-
let file_path_str = file_path.to_str().unwrap();
52+
// Generate SI formatted data (e.g., "1K", "2K", etc.)
53+
let numbers: Vec<String> = (1..=count).map(|n| format!("{n}K")).collect();
54+
let mut args = vec!["--from=si"];
55+
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
56+
args.extend(number_refs);
5957

6058
bencher.bench(|| {
61-
black_box(run_util_function(uumain, &["--from=si", file_path_str]));
59+
black_box(run_util_function(uumain, &args));
6260
});
6361
}
6462

6563
/// Benchmark large numbers with SI formatting
66-
#[divan::bench(args = [1_000_000])]
64+
#[divan::bench(args = [10_000])]
6765
fn numfmt_large_numbers_si(bencher: Bencher, count: usize) {
6866
// Generate larger numbers (millions to billions range)
69-
let data = (1..=count)
70-
.map(|n| (n * 1_000_000).to_string())
71-
.collect::<Vec<_>>()
72-
.join("\n");
73-
let file_path = setup_test_file(data.as_bytes());
74-
let file_path_str = file_path.to_str().unwrap();
67+
let numbers: Vec<String> = (1..=count).map(|n| (n * 1_000_000).to_string()).collect();
68+
let mut args = vec!["--to=si"];
69+
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
70+
args.extend(number_refs);
7571

7672
bencher.bench(|| {
77-
black_box(run_util_function(uumain, &["--to=si", file_path_str]));
73+
black_box(run_util_function(uumain, &args));
7874
});
7975
}
8076

8177
/// Benchmark different padding widths
82-
#[divan::bench(args = [(1_000_000, 50)])]
78+
#[divan::bench(args = [(10_000, 50)])]
8379
fn numfmt_padding(bencher: Bencher, (count, padding): (usize, usize)) {
84-
let data = text_data::generate_numbers(count);
85-
let file_path = setup_test_file(data.as_bytes());
86-
let file_path_str = file_path.to_str().unwrap();
80+
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect();
8781
let padding_arg = format!("--padding={padding}");
82+
let mut args = vec!["--to=si", &padding_arg];
83+
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
84+
args.extend(number_refs);
8885

8986
bencher.bench(|| {
90-
black_box(run_util_function(
91-
uumain,
92-
&["--to=si", &padding_arg, file_path_str],
93-
));
87+
black_box(run_util_function(uumain, &args));
9488
});
9589
}
9690

9791
/// Benchmark round modes with SI formatting
98-
#[divan::bench(args = [("up", 1_000_000), ("down", 1_000_000), ("towards-zero", 1_000_000)])]
92+
#[divan::bench(args = [("up", 10_000), ("down", 10_000), ("towards-zero", 10_000)])]
9993
fn numfmt_round_modes(bencher: Bencher, (round_mode, count): (&str, usize)) {
100-
let data = text_data::generate_numbers(count);
101-
let file_path = setup_test_file(data.as_bytes());
102-
let file_path_str = file_path.to_str().unwrap();
94+
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect();
10395
let round_arg = format!("--round={round_mode}");
96+
let mut args = vec!["--to=si", &round_arg];
97+
let number_refs: Vec<&str> = numbers.iter().map(|s| s.as_str()).collect();
98+
args.extend(number_refs);
10499

105100
bencher.bench(|| {
106-
black_box(run_util_function(
107-
uumain,
108-
&["--to=si", &round_arg, file_path_str],
109-
));
101+
black_box(run_util_function(uumain, &args));
110102
});
111103
}
112104

src/uu/sort/benches/sort_bench.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file that was distributed with this source code.
55

66
use divan::{Bencher, black_box};
7+
use tempfile::NamedTempFile;
78
use uu_sort::uumain;
89
use uucore::benchmark::{run_util_function, setup_test_file, text_data};
910

@@ -12,9 +13,14 @@ use uucore::benchmark::{run_util_function, setup_test_file, text_data};
1213
fn sort_ascii_only(bencher: Bencher, num_lines: usize) {
1314
let data = text_data::generate_ascii_data(num_lines);
1415
let file_path = setup_test_file(&data);
16+
let output_file = NamedTempFile::new().unwrap();
17+
let output_path = output_file.path().to_str().unwrap();
1518

1619
bencher.bench(|| {
17-
black_box(run_util_function(uumain, &[file_path.to_str().unwrap()]));
20+
black_box(run_util_function(
21+
uumain,
22+
&["-o", output_path, file_path.to_str().unwrap()],
23+
));
1824
});
1925
}
2026

@@ -23,9 +29,14 @@ fn sort_ascii_only(bencher: Bencher, num_lines: usize) {
2329
fn sort_accented_data(bencher: Bencher, num_lines: usize) {
2430
let data = text_data::generate_accented_data(num_lines);
2531
let file_path = setup_test_file(&data);
32+
let output_file = NamedTempFile::new().unwrap();
33+
let output_path = output_file.path().to_str().unwrap();
2634

2735
bencher.bench(|| {
28-
black_box(run_util_function(uumain, &[file_path.to_str().unwrap()]));
36+
black_box(run_util_function(
37+
uumain,
38+
&["-o", output_path, file_path.to_str().unwrap()],
39+
));
2940
});
3041
}
3142

@@ -34,9 +45,14 @@ fn sort_accented_data(bencher: Bencher, num_lines: usize) {
3445
fn sort_mixed_data(bencher: Bencher, num_lines: usize) {
3546
let data = text_data::generate_mixed_data(num_lines);
3647
let file_path = setup_test_file(&data);
48+
let output_file = NamedTempFile::new().unwrap();
49+
let output_path = output_file.path().to_str().unwrap();
3750

3851
bencher.bench(|| {
39-
black_box(run_util_function(uumain, &[file_path.to_str().unwrap()]));
52+
black_box(run_util_function(
53+
uumain,
54+
&["-o", output_path, file_path.to_str().unwrap()],
55+
));
4056
});
4157
}
4258

@@ -45,9 +61,14 @@ fn sort_mixed_data(bencher: Bencher, num_lines: usize) {
4561
fn sort_case_sensitive(bencher: Bencher, num_lines: usize) {
4662
let data = text_data::generate_case_sensitive_data(num_lines);
4763
let file_path = setup_test_file(&data);
64+
let output_file = NamedTempFile::new().unwrap();
65+
let output_path = output_file.path().to_str().unwrap();
4866

4967
bencher.bench(|| {
50-
black_box(run_util_function(uumain, &[file_path.to_str().unwrap()]));
68+
black_box(run_util_function(
69+
uumain,
70+
&["-o", output_path, file_path.to_str().unwrap()],
71+
));
5172
});
5273
}
5374

@@ -56,11 +77,13 @@ fn sort_case_sensitive(bencher: Bencher, num_lines: usize) {
5677
fn sort_case_insensitive(bencher: Bencher, num_lines: usize) {
5778
let data = text_data::generate_case_sensitive_data(num_lines);
5879
let file_path = setup_test_file(&data);
80+
let output_file = NamedTempFile::new().unwrap();
81+
let output_path = output_file.path().to_str().unwrap();
5982

6083
bencher.bench(|| {
6184
black_box(run_util_function(
6285
uumain,
63-
&["-f", file_path.to_str().unwrap()],
86+
&["-f", "-o", output_path, file_path.to_str().unwrap()],
6487
));
6588
});
6689
}
@@ -70,11 +93,13 @@ fn sort_case_insensitive(bencher: Bencher, num_lines: usize) {
7093
fn sort_dictionary_order(bencher: Bencher, num_lines: usize) {
7194
let data = text_data::generate_mixed_data(num_lines);
7295
let file_path = setup_test_file(&data);
96+
let output_file = NamedTempFile::new().unwrap();
97+
let output_path = output_file.path().to_str().unwrap();
7398

7499
bencher.bench(|| {
75100
black_box(run_util_function(
76101
uumain,
77-
&["-d", file_path.to_str().unwrap()],
102+
&["-d", "-o", output_path, file_path.to_str().unwrap()],
78103
));
79104
});
80105
}
@@ -92,10 +117,13 @@ fn sort_numeric(bencher: Bencher, num_lines: usize) {
92117

93118
let file_path = setup_test_file(&data);
94119

120+
let output_file = NamedTempFile::new().unwrap();
121+
let output_path = output_file.path().to_str().unwrap();
122+
95123
bencher.bench(|| {
96124
black_box(run_util_function(
97125
uumain,
98-
&["-n", file_path.to_str().unwrap()],
126+
&["-n", "-o", output_path, file_path.to_str().unwrap()],
99127
));
100128
});
101129
}
@@ -105,11 +133,13 @@ fn sort_numeric(bencher: Bencher, num_lines: usize) {
105133
fn sort_reverse_locale(bencher: Bencher, num_lines: usize) {
106134
let data = text_data::generate_accented_data(num_lines);
107135
let file_path = setup_test_file(&data);
136+
let output_file = NamedTempFile::new().unwrap();
137+
let output_path = output_file.path().to_str().unwrap();
108138

109139
bencher.bench(|| {
110140
black_box(run_util_function(
111141
uumain,
112-
&["-r", file_path.to_str().unwrap()],
142+
&["-r", "-o", output_path, file_path.to_str().unwrap()],
113143
));
114144
});
115145
}
@@ -130,11 +160,14 @@ fn sort_key_field(bencher: Bencher, num_lines: usize) {
130160

131161
let file_path = setup_test_file(&data);
132162

163+
let output_file = NamedTempFile::new().unwrap();
164+
let output_path = output_file.path().to_str().unwrap();
165+
133166
bencher.bench(|| {
134167
// Sort by second field
135168
black_box(run_util_function(
136169
uumain,
137-
&["-k", "2", file_path.to_str().unwrap()],
170+
&["-k", "2", "-o", output_path, file_path.to_str().unwrap()],
138171
));
139172
});
140173
}
@@ -144,11 +177,13 @@ fn sort_key_field(bencher: Bencher, num_lines: usize) {
144177
fn sort_unique_locale(bencher: Bencher, num_lines: usize) {
145178
let data = text_data::generate_accented_data(num_lines);
146179
let file_path = setup_test_file(&data);
180+
let output_file = NamedTempFile::new().unwrap();
181+
let output_path = output_file.path().to_str().unwrap();
147182

148183
bencher.bench(|| {
149184
black_box(run_util_function(
150185
uumain,
151-
&["-u", file_path.to_str().unwrap()],
186+
&["-u", "-o", output_path, file_path.to_str().unwrap()],
152187
));
153188
});
154189
}

0 commit comments

Comments
 (0)