Skip to content

Commit 607383a

Browse files
committed
bench(sort): reduce variance by reusing output files across iterations
Locale benchmarks were creating temp files inside the bench loop (from d1cd999), causing filesystem noise and false CodSpeed regressions. The same commit's sort_bench.rs got it right with file creation outside the loop. This fix aligns with that pattern.
1 parent b990406 commit 607383a

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/uu/sort/benches/sort_locale_bench.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ use uucore::benchmark::{run_util_function, setup_test_file, text_data};
1414
fn sort_ascii_c_locale(bencher: Bencher) {
1515
let data = text_data::generate_ascii_data_simple(100_000);
1616
let file_path = setup_test_file(&data);
17+
// Reuse the same output file across iterations to reduce filesystem variance
18+
let output_file = NamedTempFile::new().unwrap();
19+
let output_path = output_file.path().to_str().unwrap().to_string();
1720

1821
bencher.bench(|| {
1922
unsafe {
2023
env::set_var("LC_ALL", "C");
2124
}
22-
let output_file = NamedTempFile::new().unwrap();
23-
let output_path = output_file.path().to_str().unwrap();
2425
black_box(run_util_function(
2526
uumain,
26-
&["-o", output_path, file_path.to_str().unwrap()],
27+
&["-o", &output_path, file_path.to_str().unwrap()],
2728
));
2829
});
2930
}
@@ -33,16 +34,17 @@ fn sort_ascii_c_locale(bencher: Bencher) {
3334
fn sort_ascii_utf8_locale(bencher: Bencher) {
3435
let data = text_data::generate_ascii_data_simple(200_000);
3536
let file_path = setup_test_file(&data);
37+
// Reuse the same output file across iterations to reduce filesystem variance
38+
let output_file = NamedTempFile::new().unwrap();
39+
let output_path = output_file.path().to_str().unwrap().to_string();
3640

3741
bencher.bench(|| {
3842
unsafe {
3943
env::set_var("LC_ALL", "en_US.UTF-8");
4044
}
41-
let output_file = NamedTempFile::new().unwrap();
42-
let output_path = output_file.path().to_str().unwrap();
4345
black_box(run_util_function(
4446
uumain,
45-
&["-o", output_path, file_path.to_str().unwrap()],
47+
&["-o", &output_path, file_path.to_str().unwrap()],
4648
));
4749
});
4850
}
@@ -52,16 +54,17 @@ fn sort_ascii_utf8_locale(bencher: Bencher) {
5254
fn sort_mixed_c_locale(bencher: Bencher) {
5355
let data = text_data::generate_mixed_locale_data(50_000);
5456
let file_path = setup_test_file(&data);
57+
// Reuse the same output file across iterations to reduce filesystem variance
58+
let output_file = NamedTempFile::new().unwrap();
59+
let output_path = output_file.path().to_str().unwrap().to_string();
5560

5661
bencher.bench(|| {
5762
unsafe {
5863
env::set_var("LC_ALL", "C");
5964
}
60-
let output_file = NamedTempFile::new().unwrap();
61-
let output_path = output_file.path().to_str().unwrap();
6265
black_box(run_util_function(
6366
uumain,
64-
&["-o", output_path, file_path.to_str().unwrap()],
67+
&["-o", &output_path, file_path.to_str().unwrap()],
6568
));
6669
});
6770
}
@@ -71,16 +74,17 @@ fn sort_mixed_c_locale(bencher: Bencher) {
7174
fn sort_mixed_utf8_locale(bencher: Bencher) {
7275
let data = text_data::generate_mixed_locale_data(50_000);
7376
let file_path = setup_test_file(&data);
77+
// Reuse the same output file across iterations to reduce filesystem variance
78+
let output_file = NamedTempFile::new().unwrap();
79+
let output_path = output_file.path().to_str().unwrap().to_string();
7480

7581
bencher.bench(|| {
7682
unsafe {
7783
env::set_var("LC_ALL", "en_US.UTF-8");
7884
}
79-
let output_file = NamedTempFile::new().unwrap();
80-
let output_path = output_file.path().to_str().unwrap();
8185
black_box(run_util_function(
8286
uumain,
83-
&["-o", output_path, file_path.to_str().unwrap()],
87+
&["-o", &output_path, file_path.to_str().unwrap()],
8488
));
8589
});
8690
}
@@ -90,16 +94,17 @@ fn sort_mixed_utf8_locale(bencher: Bencher) {
9094
fn sort_german_c_locale(bencher: Bencher) {
9195
let data = text_data::generate_german_locale_data(50_000);
9296
let file_path = setup_test_file(&data);
97+
// Reuse the same output file across iterations to reduce filesystem variance
98+
let output_file = NamedTempFile::new().unwrap();
99+
let output_path = output_file.path().to_str().unwrap().to_string();
93100

94101
bencher.bench(|| {
95102
unsafe {
96103
env::set_var("LC_ALL", "C");
97104
}
98-
let output_file = NamedTempFile::new().unwrap();
99-
let output_path = output_file.path().to_str().unwrap();
100105
black_box(run_util_function(
101106
uumain,
102-
&["-o", output_path, file_path.to_str().unwrap()],
107+
&["-o", &output_path, file_path.to_str().unwrap()],
103108
));
104109
});
105110
}
@@ -109,16 +114,17 @@ fn sort_german_c_locale(bencher: Bencher) {
109114
fn sort_german_locale(bencher: Bencher) {
110115
let data = text_data::generate_german_locale_data(50_000);
111116
let file_path = setup_test_file(&data);
117+
// Reuse the same output file across iterations to reduce filesystem variance
118+
let output_file = NamedTempFile::new().unwrap();
119+
let output_path = output_file.path().to_str().unwrap().to_string();
112120

113121
bencher.bench(|| {
114122
unsafe {
115123
env::set_var("LC_ALL", "de_DE.UTF-8");
116124
}
117-
let output_file = NamedTempFile::new().unwrap();
118-
let output_path = output_file.path().to_str().unwrap();
119125
black_box(run_util_function(
120126
uumain,
121-
&["-o", output_path, file_path.to_str().unwrap()],
127+
&["-o", &output_path, file_path.to_str().unwrap()],
122128
));
123129
});
124130
}
@@ -128,16 +134,17 @@ fn sort_german_locale(bencher: Bencher) {
128134
fn sort_random_strings(bencher: Bencher) {
129135
let data = text_data::generate_random_strings(50_000, 50);
130136
let file_path = setup_test_file(&data);
137+
// Reuse the same output file across iterations to reduce filesystem variance
138+
let output_file = NamedTempFile::new().unwrap();
139+
let output_path = output_file.path().to_str().unwrap().to_string();
131140

132141
bencher.bench(|| {
133142
unsafe {
134143
env::set_var("LC_ALL", "en_US.UTF-8");
135144
}
136-
let output_file = NamedTempFile::new().unwrap();
137-
let output_path = output_file.path().to_str().unwrap();
138145
black_box(run_util_function(
139146
uumain,
140-
&["-o", output_path, file_path.to_str().unwrap()],
147+
&["-o", &output_path, file_path.to_str().unwrap()],
141148
));
142149
});
143150
}

0 commit comments

Comments
 (0)