Debian Codegraph @

src/uu/wc/benches/wc_bench.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 use divan::{Bencher, black_box};
7 use uu_wc::uumain;
8 use uucore::benchmark::{create_test_file, get_bench_args, text_data};
9
10 /// Benchmark different file sizes for byte counting
11 #[divan::bench(args = [1, 500])] //todo: add 10kb to measure splice() overhead
12 fn wc_bytes_synthetic(bencher: Bencher, size_mb: usize) {
13 let temp_dir = tempfile::tempdir().unwrap();
14 let data = text_data::generate_by_size(size_mb, 80);
15 let file_path = create_test_file(&data, temp_dir.path());
16
17 bencher
18 .with_inputs(|| get_bench_args(&[&"-c", &file_path]).into_iter())
19 .bench_values(|args| black_box(uumain(args)));
20 }
21
22 #[divan::bench(args = [2_000])]
23 fn wc_words_synthetic(bencher: Bencher, size_mb: usize) {
24 let temp_dir = tempfile::tempdir().unwrap();
25 let data = text_data::generate_by_size(size_mb, 80);
26 let file_path = create_test_file(&data, temp_dir.path());
27
28 bencher
29 .with_inputs(|| get_bench_args(&[&"-w", &file_path]).into_iter())
30 .bench_values(|args| black_box(uumain(args)));
31 }
32
33 /// Benchmark combined byte+line counting
34 #[divan::bench(args = [2_000])]
35 fn wc_bytes_lines_synthetic(bencher: Bencher, size_mb: usize) {
36 let temp_dir = tempfile::tempdir().unwrap();
37 let data = text_data::generate_by_size(size_mb, 80);
38 let file_path = create_test_file(&data, temp_dir.path());
39
40 bencher
41 .with_inputs(|| get_bench_args(&[&"-cl", &file_path]).into_iter())
42 .bench_values(|args| black_box(uumain(args)));
43 }
44
45 /// Test different line lengths impact on performance
46 #[divan::bench(args = [(50, 500)])]
47 fn wc_lines_variable_length(bencher: Bencher, (size_mb, avg_line_len): (usize, usize)) {
48 let temp_dir = tempfile::tempdir().unwrap();
49 let data = text_data::generate_by_size(size_mb, avg_line_len);
50 let file_path = create_test_file(&data, temp_dir.path());
51
52 bencher
53 .with_inputs(|| get_bench_args(&[&"-l", &file_path]).into_iter())
54 .bench_values(|args| black_box(uumain(args)));
55 }
56
57 /// Benchmark large files by line count - up to 500K lines!
58 #[divan::bench(args = [500_000])]
59 fn wc_lines_large_line_count(bencher: Bencher, num_lines: usize) {
60 let temp_dir = tempfile::tempdir().unwrap();
61 let data = text_data::generate_by_lines(num_lines, 80);
62 let file_path = create_test_file(&data, temp_dir.path());
63
64 bencher
65 .with_inputs(|| get_bench_args(&[&"-l", &file_path]).into_iter())
66 .bench_values(|args| black_box(uumain(args)));
67 }
68
69 /// Benchmark character counting on large line counts
70 #[divan::bench(args = [100_000])]
71 fn wc_chars_large_line_count(bencher: Bencher, num_lines: usize) {
72 let temp_dir = tempfile::tempdir().unwrap();
73 let data = text_data::generate_by_lines(num_lines, 80);
74 let file_path = create_test_file(&data, temp_dir.path());
75
76 bencher
77 .with_inputs(|| get_bench_args(&[&"-m", &file_path]).into_iter())
78 .bench_values(|args| black_box(uumain(args)));
79 }
80
81 /// Benchmark word counting on large line counts
82 #[divan::bench(args = [100_000])]
83 fn wc_words_large_line_count(bencher: Bencher, num_lines: usize) {
84 let temp_dir = tempfile::tempdir().unwrap();
85 let data = text_data::generate_by_lines(num_lines, 80);
86 let file_path = create_test_file(&data, temp_dir.path());
87
88 bencher
89 .with_inputs(|| get_bench_args(&[&"-w", &file_path]).into_iter())
90 .bench_values(|args| black_box(uumain(args)));
91 }
92
93 /// Benchmark default wc (lines, words, bytes) on large line counts
94 #[divan::bench(args = [100_000])]
95 fn wc_default_large_line_count(bencher: Bencher, num_lines: usize) {
96 let temp_dir = tempfile::tempdir().unwrap();
97 let data = text_data::generate_by_lines(num_lines, 80);
98 let file_path = create_test_file(&data, temp_dir.path());
99
100 bencher
101 .with_inputs(|| get_bench_args(&[&"-lwc", &file_path]).into_iter())
102 .bench_values(|args| black_box(uumain(args)));
103 }
104
105 /// Benchmark very short vs very long lines with 100K lines
106 #[divan::bench(args = [(100_000, 200)])]
107 fn wc_lines_extreme_line_lengths(bencher: Bencher, (num_lines, line_len): (usize, usize)) {
108 let temp_dir = tempfile::tempdir().unwrap();
109 let data = text_data::generate_by_lines(num_lines, line_len);
110 let file_path = create_test_file(&data, temp_dir.path());
111
112 bencher
113 .with_inputs(|| get_bench_args(&[&"-l", &file_path]).into_iter())
114 .bench_values(|args| black_box(uumain(args)));
115 }
116
117 fn main() {
118 divan::main();
119 }