Debian Codegraph @

src/uu/uniq/benches/uniq_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_uniq::uumain;
8 use uucore::benchmark::{get_bench_args, run_util_function, setup_test_file};
9
10 /// Generate data with many consecutive duplicate lines
11 /// This directly tests the core optimization of PR #8703 - avoiding allocations when comparing lines
12 fn generate_duplicate_heavy_data(num_groups: usize, duplicates_per_group: usize) -> Vec<u8> {
13 let mut data = Vec::new();
14
15 for group in 0..num_groups {
16 // Generate a line with realistic content
17 let line = format!(
18 "Line content for group {group:06} with additional text to make it more realistic for testing performance\n"
19 );
20
21 // Repeat the line multiple times (this is what PR #8703 optimizes)
22 for _ in 0..duplicates_per_group {
23 data.extend_from_slice(line.as_bytes());
24 }
25 }
26
27 data
28 }
29
30 /// Benchmark 1: Heavy duplicates - the main optimization target
31 /// Many consecutive duplicate lines that stress the line comparison optimization
32 #[divan::bench(args = [10_000])]
33 fn uniq_heavy_duplicates(bencher: Bencher, num_lines: usize) {
34 // Create 1000 groups with ~10,000 duplicates each
35 // This maximizes the benefit of PR #8703's optimization
36 let num_groups = 1000;
37 let duplicates_per_group = num_lines / num_groups;
38 let data = generate_duplicate_heavy_data(num_groups, duplicates_per_group);
39 let file_path = setup_test_file(&data);
40
41 bencher
42 .with_inputs(|| get_bench_args(&[&file_path]).into_iter())
43 .bench_values(|args| black_box(uumain(args)));
44 }
45
46 /// Benchmark 2: Mixed duplicates with counting
47 /// Tests the -c flag with a mix of duplicate groups
48 #[divan::bench(args = [10_000])]
49 fn uniq_with_count(bencher: Bencher, num_lines: usize) {
50 // Create more groups with fewer duplicates for varied counting
51 let num_groups = num_lines / 100;
52 let data = generate_duplicate_heavy_data(num_groups, 100);
53 let file_path = setup_test_file(&data);
54
55 bencher
56 .with_inputs(|| get_bench_args(&[&"-c", &file_path]).into_iter())
57 .bench_values(|args| black_box(uumain(args)));
58 }
59
60 /// Benchmark 3: Case-insensitive comparison with duplicates
61 /// Tests the -i flag which requires case folding during comparison
62 #[divan::bench(args = [10_000])]
63 fn uniq_case_insensitive(bencher: Bencher, num_lines: usize) {
64 let mut data = Vec::new();
65 let words = [
66 "Hello",
67 "WORLD",
68 "Testing",
69 "UNIQ",
70 "Benchmark",
71 "Performance",
72 ];
73
74 // Generate groups of case variations
75 for i in 0..num_lines {
76 let word = words[(i / 50) % words.len()];
77
78 // Create case variations that should be treated as duplicates with -i
79 let variation = match i % 4 {
80 0 => word.to_lowercase(),
81 1 => word.to_uppercase(),
82 2 => word.to_string(),
83 _ => {
84 // Mixed case
85 word.chars()
86 .enumerate()
87 .map(|(idx, c)| {
88 if idx % 2 == 0 {
89 c.to_lowercase().to_string()
90 } else {
91 c.to_uppercase().to_string()
92 }
93 })
94 .collect()
95 }
96 };
97
98 data.extend_from_slice(format!("{variation}\n").as_bytes());
99 }
100
101 let file_path = setup_test_file(&data);
102
103 bencher
104 .with_inputs(|| get_bench_args(&[&"-i", &file_path]).into_iter())
105 .bench_values(|args| black_box(uumain(args)));
106 }
107
108 /// Benchmark 4: `-w N` over duplicate-heavy input, for both a narrow N (1)
109 /// and a wide N (512, as reported in #13199).
110 /// Regression coverage for #13199: `uniq -w N` was ~5x slower than GNU uniq
111 /// because the locale check inside key bounds computation was re-evaluated
112 /// via environment variable lookups on every single line (duplicate or not),
113 /// regardless of N. Reuses the duplicate-heavy generator so the vast
114 /// majority of lines collapse and the benchmark isn't dominated by output.
115 #[divan::bench(args = [(10_000, "1"), (10_000, "512")])]
116 fn uniq_check_chars(bencher: Bencher, (num_lines, check_chars): (usize, &str)) {
117 let num_groups = 1000;
118 let duplicates_per_group = num_lines / num_groups;
119 let data = generate_duplicate_heavy_data(num_groups, duplicates_per_group);
120 let file_path = setup_test_file(&data);
121 let file_path_str = file_path.to_str().unwrap();
122
123 bencher.bench(|| {
124 black_box(run_util_function(
125 uumain,
126 &["-w", check_chars, file_path_str],
127 ));
128 });
129 }
130
131 fn main() {
132 divan::main();
133 }