Debian Codegraph @

src/uu/cut/benches/cut_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_cut::uumain;
8 use uucore::benchmark::{get_bench_args, setup_test_file, text_data};
9
10 /// Benchmark cutting specific byte ranges
11 #[divan::bench]
12 fn cut_bytes(bencher: Bencher) {
13 let data = text_data::generate_by_lines(100_000, 80);
14 let file_path = setup_test_file(&data);
15
16 bencher
17 .with_inputs(|| get_bench_args(&[&"-b", &"1-20", &file_path]).into_iter())
18 .bench_values(|args| black_box(uumain(args)));
19 }
20
21 /// Benchmark cutting specific character ranges
22 #[divan::bench]
23 fn cut_characters(bencher: Bencher) {
24 let data = text_data::generate_mixed_data(100_000);
25 let file_path = setup_test_file(&data);
26
27 bencher
28 .with_inputs(|| get_bench_args(&[&"-c", &"5-30", &file_path]).into_iter())
29 .bench_values(|args| black_box(uumain(args)));
30 }
31
32 /// Benchmark cutting a range that both starts and ends inside long multi-byte
33 /// lines. `cut_characters` uses short lines and a range running past their end,
34 /// so it measures the fixed per-line cost; here the character walk dominates.
35 #[divan::bench]
36 fn cut_characters_long_lines(bencher: Bencher) {
37 let mut data = Vec::new();
38 for i in 0..40_000 {
39 // Roughly 100 characters a line, a third of them multi-byte, so that
40 // neither end of the range lands on a byte offset equal to its
41 // character position.
42 let line = format!("{}école-piñata-{i:05}-Straße-", "aéiöu".repeat(16));
43 data.extend_from_slice(line.as_bytes());
44 data.push(b'\n');
45 }
46 let file_path = setup_test_file(&data);
47
48 bencher
49 .with_inputs(|| get_bench_args(&[&"-c", &"20-70", &file_path]).into_iter())
50 .bench_values(|args| black_box(uumain(args)));
51 }
52
53 /// Benchmark cutting fields with tab delimiter
54 #[divan::bench]
55 fn cut_fields_tab(bencher: Bencher) {
56 let mut data = Vec::new();
57 for i in 0..100_000 {
58 let line = format!("field1\tfield2_{i}\tfield3\tfield4\tfield5\n");
59 data.extend_from_slice(line.as_bytes());
60 }
61 let file_path = setup_test_file(&data);
62
63 bencher
64 .with_inputs(|| get_bench_args(&[&"-f", &"2,4", &file_path]).into_iter())
65 .bench_values(|args| black_box(uumain(args)));
66 }
67
68 /// Benchmark cutting fields with custom delimiter
69 #[divan::bench]
70 fn cut_fields_custom_delim(bencher: Bencher) {
71 let mut data = Vec::new();
72 for i in 0..100_000 {
73 let line = format!("apple,banana_{i},cherry,date,elderberry\n");
74 data.extend_from_slice(line.as_bytes());
75 }
76 let file_path = setup_test_file(&data);
77
78 bencher
79 .with_inputs(|| get_bench_args(&[&"-d", &",", &"-f", &"1,3,5", &file_path]).into_iter())
80 .bench_values(|args| black_box(uumain(args)));
81 }
82
83 /// Benchmark cutting fields with newline delimiter
84 #[divan::bench]
85 fn cut_fields_newline_delim(bencher: Bencher) {
86 let mut data = Vec::new();
87 for i in 0..100_000 {
88 let line = format!("field_content_number_{i}\n");
89 data.extend_from_slice(line.as_bytes());
90 }
91 let file_path = setup_test_file(&data);
92
93 bencher
94 .with_inputs(|| get_bench_args(&[&"-d", &"\n", &"-f", &"1,3,5", &file_path]).into_iter())
95 .bench_values(|args| black_box(uumain(args)));
96 }
97
98 fn main() {
99 divan::main();
100 }