Debian Codegraph @

src/uu/base64/benches/base64_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_base64::uumain;
8 use uucore::benchmark::{create_test_file, get_bench_args, text_data};
9
10 fn create_tmp_file(size_mb: usize) -> std::path::PathBuf {
11 let temp_dir = tempfile::tempdir().unwrap();
12 let data = text_data::generate_by_size(size_mb, 80);
13 create_test_file(&data, temp_dir.path())
14 }
15
16 fn redirect_in(in_file: &std::path::Path) -> std::ffi::OsString {
17 std::iter::once(std::ffi::OsString::from(">").as_os_str())
18 .chain(std::iter::once(in_file.as_os_str()))
19 .collect()
20 }
21
22 /// Benchmark for base64 encoding
23 #[divan::bench()]
24 fn b64_encode_synthetic(bencher: Bencher) {
25 let file_path = create_tmp_file(5_000);
26
27 bencher
28 .with_inputs(|| get_bench_args(&[&file_path]).into_iter())
29 .bench_values(|args| black_box(uumain(args)));
30 }
31
32 // Benchmark for base64 decoding
33 #[divan::bench()]
34 fn b64_decode_synthetic(bencher: Bencher) {
35 let temp_dir = tempfile::tempdir().unwrap();
36 let file_path = create_tmp_file(5_000);
37 let in_file = create_test_file(b"", temp_dir.path());
38
39 uumain([file_path.into(), redirect_in(&in_file)].into_iter());
40
41 bencher
42 .with_inputs(|| get_bench_args(&[&"-d", &in_file]).into_iter())
43 .bench_values(|args| black_box(uumain(args)));
44 }
45
46 // Benchmark different file sizes for base64 decoding ignoring garbage characters
47 #[divan::bench()]
48 fn b64_decode_ignore_garbage_synthetic(bencher: Bencher) {
49 let tempdir = tempfile::tempdir().unwrap();
50 let temp_dir = tempdir;
51 let file_path = create_tmp_file(5_000);
52 let in_file = create_test_file(b"", temp_dir.path());
53
54 uumain([file_path.into(), redirect_in(&in_file)].into_iter());
55
56 bencher
57 .with_inputs(|| get_bench_args(&[&"-d", &"-i", &in_file]).into_iter())
58 .bench_values(|args| black_box(uumain(args)));
59 }
60
61 fn main() {
62 divan::main();
63 }