|
3 | 3 | // For the full copyright and license information, please view the LICENSE |
4 | 4 | // file that was distributed with this source code. |
5 | 5 |
|
6 | | -use divan::{Bencher, black_box}; |
| 6 | +use divan::Bencher; |
7 | 7 | use std::ffi::OsString; |
8 | 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 | | -} |
| 9 | +use uucore::benchmark::{bench_util, create_test_file, run_util_function, text_data}; |
17 | 10 |
|
18 | 11 | /// Benchmark for base64 encoding |
19 | 12 | #[divan::bench()] |
20 | 13 | 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 | | - }); |
| 14 | + let data = text_data::generate_by_size(5_000, 80); |
| 15 | + bench_util(bencher, data, &[], uumain); |
26 | 16 | } |
27 | 17 |
|
28 | 18 | // Benchmark for base64 decoding |
29 | 19 | #[divan::bench()] |
30 | 20 | fn b64_decode_synthetic(bencher: Bencher) { |
31 | 21 | 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(); |
| 22 | + let source_data = text_data::generate_by_size(5_000, 80); |
| 23 | + let source_file = create_test_file(&source_data, temp_dir.path()); |
| 24 | + let encoded_file = create_test_file(b"", temp_dir.path()); |
| 25 | + let encoded_file_str = encoded_file.to_str().unwrap(); |
| 26 | + |
| 27 | + // First encode the data to create the test input |
35 | 28 | uumain( |
36 | 29 | [ |
37 | | - OsString::from(file_path_str), |
38 | | - OsString::from(format!(">{in_file_str}")), |
| 30 | + OsString::from(source_file.to_str().unwrap()), |
| 31 | + OsString::from(format!(">{encoded_file_str}")), |
39 | 32 | ] |
40 | 33 | .iter() |
41 | 34 | .map(|x| (*x).clone()), |
42 | 35 | ); |
43 | 36 |
|
44 | 37 | bencher.bench(|| { |
45 | | - black_box(run_util_function(uumain, &["-d", in_file_str])); |
| 38 | + divan::black_box(run_util_function(uumain, &["-d", encoded_file_str])); |
46 | 39 | }); |
47 | 40 | } |
48 | 41 |
|
49 | 42 | // Benchmark different file sizes for base64 decoding ignoring garbage characters |
50 | 43 | #[divan::bench()] |
51 | 44 | fn b64_decode_ignore_garbage_synthetic(bencher: Bencher) { |
52 | 45 | 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(); |
| 46 | + let source_data = text_data::generate_by_size(5_000, 80); |
| 47 | + let source_file = create_test_file(&source_data, temp_dir.path()); |
| 48 | + let encoded_file = create_test_file(b"", temp_dir.path()); |
| 49 | + let encoded_file_str = encoded_file.to_str().unwrap(); |
| 50 | + |
| 51 | + // First encode the data to create the test input |
56 | 52 | uumain( |
57 | 53 | [ |
58 | | - OsString::from(file_path_str), |
59 | | - OsString::from(format!(">{in_file_str}")), |
| 54 | + OsString::from(source_file.to_str().unwrap()), |
| 55 | + OsString::from(format!(">{encoded_file_str}")), |
60 | 56 | ] |
61 | 57 | .iter() |
62 | 58 | .map(|x| (*x).clone()), |
63 | 59 | ); |
64 | 60 |
|
65 | 61 | bencher.bench(|| { |
66 | | - black_box(run_util_function(uumain, &["-d", "-i", in_file_str])); |
| 62 | + divan::black_box(run_util_function(uumain, &["-d", "-i", encoded_file_str])); |
67 | 63 | }); |
68 | 64 | } |
69 | 65 |
|
|
0 commit comments