Skip to content

Commit c25f303

Browse files
committed
bench: remove some duplication
1 parent 443c201 commit c25f303

File tree

8 files changed

+188
-281
lines changed

8 files changed

+188
-281
lines changed

src/uu/base64/benches/base64_bench.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,63 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use divan::{Bencher, black_box};
6+
use divan::Bencher;
77
use std::ffi::OsString;
88
use uu_base64::uumain;
9-
use uucore::benchmark::{create_test_file, run_util_function, text_data};
10-
11-
fn create_tmp_file(size_mb: usize) -> String {
12-
let temp_dir = tempfile::tempdir().unwrap();
13-
let data = text_data::generate_by_size(size_mb, 80);
14-
let file_path = create_test_file(&data, temp_dir.path());
15-
String::from(file_path.to_str().unwrap())
16-
}
9+
use uucore::benchmark::{bench_util, create_test_file, run_util_function, text_data};
1710

1811
/// Benchmark for base64 encoding
1912
#[divan::bench()]
2013
fn b64_encode_synthetic(bencher: Bencher) {
21-
let file_path_str = &create_tmp_file(5_000);
22-
23-
bencher.bench(|| {
24-
black_box(run_util_function(uumain, &[file_path_str]));
25-
});
14+
let data = text_data::generate_by_size(5_000, 80);
15+
bench_util(bencher, data, &[], uumain);
2616
}
2717

2818
// Benchmark for base64 decoding
2919
#[divan::bench()]
3020
fn b64_decode_synthetic(bencher: Bencher) {
3121
let temp_dir = tempfile::tempdir().unwrap();
32-
let file_path_str = &create_tmp_file(5_000);
33-
let in_file = create_test_file(b"", temp_dir.path());
34-
let in_file_str = in_file.to_str().unwrap();
22+
let source_data = text_data::generate_by_size(5_000, 80);
23+
let source_file = create_test_file(&source_data, temp_dir.path());
24+
let encoded_file = create_test_file(b"", temp_dir.path());
25+
let encoded_file_str = encoded_file.to_str().unwrap();
26+
27+
// First encode the data to create the test input
3528
uumain(
3629
[
37-
OsString::from(file_path_str),
38-
OsString::from(format!(">{in_file_str}")),
30+
OsString::from(source_file.to_str().unwrap()),
31+
OsString::from(format!(">{encoded_file_str}")),
3932
]
4033
.iter()
4134
.map(|x| (*x).clone()),
4235
);
4336

4437
bencher.bench(|| {
45-
black_box(run_util_function(uumain, &["-d", in_file_str]));
38+
divan::black_box(run_util_function(uumain, &["-d", encoded_file_str]));
4639
});
4740
}
4841

4942
// Benchmark different file sizes for base64 decoding ignoring garbage characters
5043
#[divan::bench()]
5144
fn b64_decode_ignore_garbage_synthetic(bencher: Bencher) {
5245
let temp_dir = tempfile::tempdir().unwrap();
53-
let file_path_str = &create_tmp_file(5_000);
54-
let in_file = create_test_file(b"", temp_dir.path());
55-
let in_file_str = in_file.to_str().unwrap();
46+
let source_data = text_data::generate_by_size(5_000, 80);
47+
let source_file = create_test_file(&source_data, temp_dir.path());
48+
let encoded_file = create_test_file(b"", temp_dir.path());
49+
let encoded_file_str = encoded_file.to_str().unwrap();
50+
51+
// First encode the data to create the test input
5652
uumain(
5753
[
58-
OsString::from(file_path_str),
59-
OsString::from(format!(">{in_file_str}")),
54+
OsString::from(source_file.to_str().unwrap()),
55+
OsString::from(format!(">{encoded_file_str}")),
6056
]
6157
.iter()
6258
.map(|x| (*x).clone()),
6359
);
6460

6561
bencher.bench(|| {
66-
black_box(run_util_function(uumain, &["-d", "-i", in_file_str]));
62+
divan::black_box(run_util_function(uumain, &["-d", "-i", encoded_file_str]));
6763
});
6864
}
6965

src/uu/expand/benches/expand_bench.rs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,22 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use divan::{Bencher, black_box};
7-
use std::fmt::Write;
6+
use divan::Bencher;
87
use uu_expand::uumain;
9-
use uucore::benchmark::{create_test_file, run_util_function};
10-
11-
/// Helper function to run expand benchmark with generated data
12-
fn bench_expand(bencher: Bencher, data: impl AsRef<[u8]>, args: &[&str]) {
13-
let temp_dir = tempfile::tempdir().unwrap();
14-
let file_path = create_test_file(data.as_ref(), temp_dir.path());
15-
let file_path_str = file_path.to_str().unwrap();
16-
17-
let mut all_args = vec![];
18-
all_args.extend_from_slice(args);
19-
all_args.push(file_path_str);
20-
21-
bencher.bench(|| {
22-
black_box(run_util_function(uumain, &all_args));
23-
});
24-
}
8+
use uucore::benchmark::{bench_util, generate_multi_tab_text, generate_tabbed_text};
259

2610
/// Benchmark expanding tabs on files with many short lines
2711
#[divan::bench(args = [100_000])]
2812
fn expand_many_lines(bencher: Bencher, num_lines: usize) {
29-
let data = (0..num_lines).fold(String::new(), |mut acc, i| {
30-
writeln!(&mut acc, "line{i}\tvalue{}\tdata{}", i * 2, i * 3).unwrap();
31-
acc
32-
});
33-
bench_expand(bencher, data, &[]);
13+
let data = generate_tabbed_text(num_lines);
14+
bench_util(bencher, data, &[], uumain);
3415
}
3516

3617
/// Benchmark expanding tabs with custom tab stops
3718
#[divan::bench(args = [50_000])]
3819
fn expand_custom_tabstops(bencher: Bencher, num_lines: usize) {
39-
let data = (0..num_lines).fold(String::new(), |mut acc, i| {
40-
writeln!(&mut acc, "a\tb\tc\td\te{i}").unwrap();
41-
acc
42-
});
43-
bench_expand(bencher, data, &["--tabs=4,8,12"]);
20+
let data = generate_multi_tab_text(num_lines);
21+
bench_util(bencher, data, &["--tabs=4,8,12"], uumain);
4422
}
4523

4624
fn main() {

src/uu/fold/benches/fold_bench.rs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,37 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use divan::{Bencher, black_box};
6+
use divan::Bencher;
7+
use std::fmt::Write;
78
use uu_fold::uumain;
8-
use uucore::benchmark::{create_test_file, run_util_function};
9+
use uucore::benchmark::bench_util;
910

1011
/// Benchmark folding many short lines
1112
#[divan::bench(args = [100_000])]
1213
fn fold_many_lines(bencher: Bencher, num_lines: usize) {
13-
let temp_dir = tempfile::tempdir().unwrap();
14-
let mut data = String::with_capacity(num_lines * 110);
15-
for i in 0..num_lines {
16-
data.push_str("This is a very long line number ");
17-
append_usize(&mut data, i);
18-
data.push_str(" that definitely needs to be folded at the default width of 80 columns\n");
19-
}
20-
let file_path = create_test_file(data.as_bytes(), temp_dir.path());
21-
let file_path_str = file_path.to_str().unwrap();
22-
23-
bencher.bench(|| {
24-
black_box(run_util_function(uumain, &[file_path_str]));
25-
});
14+
// Create long lines that need folding
15+
let data = (0..num_lines)
16+
.fold(String::new(), |mut acc, i| {
17+
writeln!(&mut acc, "This is a very long line number {i} that definitely needs to be folded at the default width of 80 columns").unwrap();
18+
acc
19+
});
20+
bench_util(bencher, data, &[], uumain);
2621
}
2722

2823
/// Benchmark folding with custom width
2924
#[divan::bench(args = [50_000])]
3025
fn fold_custom_width(bencher: Bencher, num_lines: usize) {
31-
let temp_dir = tempfile::tempdir().unwrap();
32-
let mut data = String::with_capacity(num_lines * 80);
33-
for i in 0..num_lines {
34-
data.push_str("Line ");
35-
append_usize(&mut data, i);
36-
data.push_str(" with enough text to exceed width 40 characters and require folding\n");
37-
}
38-
let file_path = create_test_file(data.as_bytes(), temp_dir.path());
39-
let file_path_str = file_path.to_str().unwrap();
40-
41-
bencher.bench(|| {
42-
black_box(run_util_function(uumain, &["-w", "40", file_path_str]));
26+
let data = (0..num_lines).fold(String::new(), |mut acc, i| {
27+
writeln!(
28+
&mut acc,
29+
"Line {i} with enough text to exceed width 40 characters and require folding"
30+
)
31+
.unwrap();
32+
acc
4333
});
34+
bench_util(bencher, data, &["-w", "40"], uumain);
4435
}
4536

4637
fn main() {
4738
divan::main();
4839
}
49-
50-
fn append_usize(buf: &mut String, mut value: usize) {
51-
let mut digits = [0u8; 20];
52-
let mut idx = digits.len();
53-
54-
if value == 0 {
55-
buf.push('0');
56-
return;
57-
}
58-
59-
while value > 0 {
60-
idx -= 1;
61-
digits[idx] = b'0' + (value % 10) as u8;
62-
value /= 10;
63-
}
64-
65-
buf.push_str(std::str::from_utf8(&digits[idx..]).unwrap());
66-
}

src/uu/nl/benches/nl_bench.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,22 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use divan::{Bencher, black_box};
6+
use divan::Bencher;
77
use uu_nl::uumain;
8-
use uucore::benchmark::{create_test_file, run_util_function, text_data};
8+
use uucore::benchmark::{bench_util, text_data};
99

1010
/// Benchmark numbering many lines (default mode - most common use case)
1111
#[divan::bench(args = [100_000])]
1212
fn nl_many_lines(bencher: Bencher, num_lines: usize) {
13-
let temp_dir = tempfile::tempdir().unwrap();
1413
let data = text_data::generate_by_lines(num_lines, 80);
15-
let file_path = create_test_file(&data, temp_dir.path());
16-
let file_path_str = file_path.to_str().unwrap();
17-
18-
bencher.bench(|| {
19-
black_box(run_util_function(uumain, &[file_path_str]));
20-
});
14+
bench_util(bencher, data, &[], uumain);
2115
}
2216

2317
/// Benchmark large file with -ba option (number all lines - most common argument)
2418
#[divan::bench(args = [10])]
2519
fn nl_large_file(bencher: Bencher, size_mb: usize) {
26-
let temp_dir = tempfile::tempdir().unwrap();
2720
let data = text_data::generate_by_size(size_mb, 80);
28-
let file_path = create_test_file(&data, temp_dir.path());
29-
let file_path_str = file_path.to_str().unwrap();
30-
31-
bencher.bench(|| {
32-
black_box(run_util_function(uumain, &["-ba", file_path_str]));
33-
});
21+
bench_util(bencher, data, &["-ba"], uumain);
3422
}
3523

3624
fn main() {

src/uu/unexpand/benches/unexpand_bench.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,25 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use divan::{Bencher, black_box};
6+
use divan::Bencher;
77
use uu_unexpand::uumain;
8-
use uucore::benchmark::{create_test_file, run_util_function};
9-
10-
/// Generate text data with leading spaces (typical unexpand use case)
11-
fn generate_indented_text(num_lines: usize) -> Vec<u8> {
12-
let mut data = Vec::new();
13-
for i in 0..num_lines {
14-
// Add varying amounts of leading spaces (4, 8, 12, etc.)
15-
let indent = (i % 4 + 1) * 4;
16-
data.extend(vec![b' '; indent]);
17-
data.extend_from_slice(b"This is a line of text with leading spaces\n");
18-
}
19-
data
20-
}
8+
use uucore::benchmark::{bench_util, generate_indented_text};
219

2210
/// Benchmark unexpanding many lines with leading spaces (most common use case)
2311
#[divan::bench(args = [100_000])]
2412
fn unexpand_many_lines(bencher: Bencher, num_lines: usize) {
25-
let temp_dir = tempfile::tempdir().unwrap();
2613
let data = generate_indented_text(num_lines);
27-
let file_path = create_test_file(&data, temp_dir.path());
28-
let file_path_str = file_path.to_str().unwrap();
29-
30-
bencher.bench(|| {
31-
black_box(run_util_function(uumain, &[file_path_str]));
32-
});
14+
bench_util(bencher, data, &[], uumain);
3315
}
3416

3517
/// Benchmark large file with spaces (tests performance on large files)
3618
#[divan::bench(args = [10])]
3719
fn unexpand_large_file(bencher: Bencher, size_mb: usize) {
38-
let temp_dir = tempfile::tempdir().unwrap();
39-
4020
// Generate approximately size_mb worth of indented lines
4121
let line_size = 50; // approximate bytes per line
4222
let num_lines = (size_mb * 1024 * 1024) / line_size;
4323
let data = generate_indented_text(num_lines);
44-
let file_path = create_test_file(&data, temp_dir.path());
45-
let file_path_str = file_path.to_str().unwrap();
46-
47-
bencher.bench(|| {
48-
black_box(run_util_function(uumain, &[file_path_str]));
49-
});
24+
bench_util(bencher, data, &[], uumain);
5025
}
5126

5227
fn main() {

0 commit comments

Comments
 (0)