|
| 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 std::ffi::OsString; |
| 8 | +use uu_base64::uumain; |
| 9 | +use uucore::benchmark::{create_test_file, run_util_function, text_data}; |
| 10 | + |
| 11 | +fn create_tmp_file(size_mb: usize) -> String { |
| 12 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 13 | + let data = text_data::generate_by_size(size_mb, 80); |
| 14 | + let file_path = create_test_file(&data, temp_dir.path()); |
| 15 | + String::from(file_path.to_str().unwrap()) |
| 16 | +} |
| 17 | + |
| 18 | +/// Benchmark for base64 encoding |
| 19 | +#[divan::bench()] |
| 20 | +fn b64_encode_synthetic(bencher: Bencher) { |
| 21 | + let file_path_str = &create_tmp_file(5_000); |
| 22 | + |
| 23 | + bencher.bench(|| { |
| 24 | + black_box(run_util_function(uumain, &[file_path_str])); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +// Benchmark for base64 decoding |
| 29 | +#[divan::bench()] |
| 30 | +fn b64_decode_synthetic(bencher: Bencher) { |
| 31 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 32 | + let file_path_str = &create_tmp_file(5_000); |
| 33 | + let in_file = create_test_file(b"", temp_dir.path()); |
| 34 | + let in_file_str = in_file.to_str().unwrap(); |
| 35 | + uumain( |
| 36 | + [ |
| 37 | + OsString::from(file_path_str), |
| 38 | + OsString::from(format!(">{in_file_str}")), |
| 39 | + ] |
| 40 | + .iter() |
| 41 | + .map(|x| (*x).clone()), |
| 42 | + ); |
| 43 | + |
| 44 | + bencher.bench(|| { |
| 45 | + black_box(run_util_function(uumain, &["-d", in_file_str])); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +// Benchmark different file sizes for base64 decoding ignoring garbage characters |
| 50 | +#[divan::bench()] |
| 51 | +fn b64_decode_ignore_garbage_synthetic(bencher: Bencher) { |
| 52 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 53 | + let file_path_str = &create_tmp_file(5_000); |
| 54 | + let in_file = create_test_file(b"", temp_dir.path()); |
| 55 | + let in_file_str = in_file.to_str().unwrap(); |
| 56 | + uumain( |
| 57 | + [ |
| 58 | + OsString::from(file_path_str), |
| 59 | + OsString::from(format!(">{in_file_str}")), |
| 60 | + ] |
| 61 | + .iter() |
| 62 | + .map(|x| (*x).clone()), |
| 63 | + ); |
| 64 | + |
| 65 | + bencher.bench(|| { |
| 66 | + black_box(run_util_function(uumain, &["-d", "-i", in_file_str])); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +fn main() { |
| 71 | + divan::main(); |
| 72 | +} |
0 commit comments