Debian Codegraph @

src/uu/cksum/benches/cksum_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_cksum::uumain;
8 use uucore::benchmark::{get_bench_args, setup_test_file, text_data};
9
10 // Macro to generate benchmarks for each algorithm
11 macro_rules! bench_algorithm {
12 ($algo_name:ident, $algo_str:expr) => {
13 #[divan::bench]
14 fn $algo_name(bencher: Bencher) {
15 let data = text_data::generate_by_size(100, 80);
16 let file_path = setup_test_file(&data);
17
18 bencher
19 .with_inputs(|| {
20 get_bench_args(&[&"--algorithm", &$algo_str, &file_path]).into_iter()
21 })
22 .bench_values(|args| black_box(uumain(args)));
23 }
24 };
25 ($algo_name:ident, $algo_str:expr, $length:expr) => {
26 #[divan::bench]
27 fn $algo_name(bencher: Bencher) {
28 let data = text_data::generate_by_size(100, 80);
29 let file_path = setup_test_file(&data);
30
31 bencher
32 .with_inputs(|| {
33 get_bench_args(&[
34 &"--algorithm",
35 &$algo_str,
36 &"--length",
37 &$length,
38 &file_path,
39 ])
40 .into_iter()
41 })
42 .bench_values(|args| black_box(uumain(args)));
43 }
44 };
45 }
46
47 // Special macro for SHAKE algorithms that require length parameter
48 // Since SHAKE algorithms have fundamental --length parameter conflicts in cksum,
49 // we implement them using direct digest calculation for meaningful benchmarks
50 macro_rules! bench_shake_algorithm {
51 ($algo_name:ident, $algo_str:expr, Shake128) => {
52 #[divan::bench]
53 fn $algo_name(bencher: Bencher) {
54 use uucore::sum::{Digest, Shake128};
55
56 let data = text_data::generate_by_size(100, 80);
57
58 bencher.bench(|| {
59 let mut shake = Shake128::with_output_bits(256);
60 shake.hash_update(&data);
61
62 // SHAKE algorithms can output any length, use 256 bits (32 bytes) for meaningful comparison
63 let mut output = [0u8; 32];
64 shake.hash_finalize(&mut output);
65
66 black_box(output);
67 });
68 }
69 };
70 ($algo_name:ident, $algo_str:expr, Shake256) => {
71 #[divan::bench]
72 fn $algo_name(bencher: Bencher) {
73 use uucore::sum::{Digest, Shake256};
74
75 let data = text_data::generate_by_size(100, 80);
76
77 bencher.bench(|| {
78 let mut shake = Shake256::with_output_bits(512);
79 shake.hash_update(&data);
80
81 // SHAKE algorithms can output any length, use 256 bits (32 bytes) for meaningful comparison
82 let mut output = [0u8; 32];
83 shake.hash_finalize(&mut output);
84
85 black_box(output);
86 });
87 }
88 };
89 }
90
91 // Generate benchmarks for all supported algorithms
92 bench_algorithm!(cksum_sysv, "sysv");
93 bench_algorithm!(cksum_bsd, "bsd");
94 bench_algorithm!(cksum_crc, "crc");
95 bench_algorithm!(cksum_crc32b, "crc32b");
96 bench_algorithm!(cksum_md5, "md5");
97 bench_algorithm!(cksum_sha1, "sha1");
98 bench_algorithm!(cksum_sha2, "sha2", "256");
99 bench_algorithm!(cksum_sha3, "sha3", "256");
100 bench_algorithm!(cksum_blake2b, "blake2b");
101 bench_algorithm!(cksum_sm3, "sm3");
102 bench_algorithm!(cksum_sha224, "sha224");
103 bench_algorithm!(cksum_sha256, "sha256");
104 bench_algorithm!(cksum_sha384, "sha384");
105 bench_algorithm!(cksum_sha512, "sha512");
106 bench_algorithm!(cksum_blake3, "blake3");
107 bench_shake_algorithm!(cksum_shake128, "shake128", Shake128);
108 bench_shake_algorithm!(cksum_shake256, "shake256", Shake256);
109
110 /// Benchmark cksum with default CRC algorithm
111 #[divan::bench]
112 fn cksum_default(bencher: Bencher) {
113 let data = text_data::generate_by_size(100, 80);
114 let file_path = setup_test_file(&data);
115
116 bencher
117 .with_inputs(|| get_bench_args(&[&file_path]).into_iter())
118 .bench_values(|args| black_box(uumain(args)));
119 }
120
121 /// Benchmark cksum with raw output format
122 #[divan::bench]
123 fn cksum_raw_output(bencher: Bencher) {
124 let data = text_data::generate_by_size(100, 80);
125 let file_path = setup_test_file(&data);
126
127 bencher
128 .with_inputs(|| get_bench_args(&[&"--raw", &file_path]).into_iter())
129 .bench_values(|args| black_box(uumain(args)));
130 }
131
132 /// Benchmark cksum processing multiple files
133 #[divan::bench]
134 fn cksum_multiple_files(bencher: Bencher) {
135 let data1 = text_data::generate_by_size(50, 80);
136 let data2 = text_data::generate_by_size(50, 80);
137 let data3 = text_data::generate_by_size(50, 80);
138
139 let file1 = setup_test_file(&data1);
140 let file2 = setup_test_file(&data2);
141 let file3 = setup_test_file(&data3);
142
143 bencher
144 .with_inputs(|| get_bench_args(&[&file1, &file2, &file3]).into_iter())
145 .bench_values(|args| black_box(uumain(args)));
146 }
147
148 fn main() {
149 divan::main();
150 }