|
| 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::io::Write; |
| 8 | +use tempfile::NamedTempFile; |
| 9 | +use uu_hashsum::uumain; |
| 10 | +use uucore::benchmark::{run_util_function, setup_test_file, text_data}; |
| 11 | + |
| 12 | +/// Benchmark MD5 hashing |
| 13 | +#[divan::bench] |
| 14 | +fn hashsum_md5(bencher: Bencher) { |
| 15 | + let data = text_data::generate_by_size(10, 80); |
| 16 | + let file_path = setup_test_file(&data); |
| 17 | + |
| 18 | + bencher.bench(|| { |
| 19 | + black_box(run_util_function( |
| 20 | + uumain, |
| 21 | + &["--md5", file_path.to_str().unwrap()], |
| 22 | + )); |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +/// Benchmark SHA1 hashing |
| 27 | +#[divan::bench] |
| 28 | +fn hashsum_sha1(bencher: Bencher) { |
| 29 | + let data = text_data::generate_by_size(10, 80); |
| 30 | + let file_path = setup_test_file(&data); |
| 31 | + |
| 32 | + bencher.bench(|| { |
| 33 | + black_box(run_util_function( |
| 34 | + uumain, |
| 35 | + &["--sha1", file_path.to_str().unwrap()], |
| 36 | + )); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +/// Benchmark SHA256 hashing |
| 41 | +#[divan::bench] |
| 42 | +fn hashsum_sha256(bencher: Bencher) { |
| 43 | + let data = text_data::generate_by_size(10, 80); |
| 44 | + let file_path = setup_test_file(&data); |
| 45 | + |
| 46 | + bencher.bench(|| { |
| 47 | + black_box(run_util_function( |
| 48 | + uumain, |
| 49 | + &["--sha256", file_path.to_str().unwrap()], |
| 50 | + )); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +/// Benchmark SHA512 hashing |
| 55 | +#[divan::bench] |
| 56 | +fn hashsum_sha512(bencher: Bencher) { |
| 57 | + let data = text_data::generate_by_size(10, 80); |
| 58 | + let file_path = setup_test_file(&data); |
| 59 | + |
| 60 | + bencher.bench(|| { |
| 61 | + black_box(run_util_function( |
| 62 | + uumain, |
| 63 | + &["--sha512", file_path.to_str().unwrap()], |
| 64 | + )); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +/// Benchmark MD5 checksum verification |
| 69 | +#[divan::bench] |
| 70 | +fn hashsum_md5_check(bencher: Bencher) { |
| 71 | + bencher |
| 72 | + .with_inputs(|| { |
| 73 | + // Create test file |
| 74 | + let data = text_data::generate_by_size(10, 80); |
| 75 | + let test_file = setup_test_file(&data); |
| 76 | + |
| 77 | + // Create checksum file - keep it alive by returning it |
| 78 | + let checksum_file = NamedTempFile::new().unwrap(); |
| 79 | + let checksum_path = checksum_file.path().to_str().unwrap().to_string(); |
| 80 | + |
| 81 | + // Write checksum content |
| 82 | + { |
| 83 | + let mut file = std::fs::File::create(&checksum_path).unwrap(); |
| 84 | + writeln!( |
| 85 | + file, |
| 86 | + "d41d8cd98f00b204e9800998ecf8427e {}", |
| 87 | + test_file.to_str().unwrap() |
| 88 | + ) |
| 89 | + .unwrap(); |
| 90 | + } |
| 91 | + |
| 92 | + (checksum_file, checksum_path) |
| 93 | + }) |
| 94 | + .bench_values(|(_checksum_file, checksum_path)| { |
| 95 | + black_box(run_util_function( |
| 96 | + uumain, |
| 97 | + &["--md5", "--check", &checksum_path], |
| 98 | + )); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +/// Benchmark SHA256 checksum verification |
| 103 | +#[divan::bench] |
| 104 | +fn hashsum_sha256_check(bencher: Bencher) { |
| 105 | + bencher |
| 106 | + .with_inputs(|| { |
| 107 | + // Create test file |
| 108 | + let data = text_data::generate_by_size(10, 80); |
| 109 | + let test_file = setup_test_file(&data); |
| 110 | + |
| 111 | + // Create checksum file - keep it alive by returning it |
| 112 | + let checksum_file = NamedTempFile::new().unwrap(); |
| 113 | + let checksum_path = checksum_file.path().to_str().unwrap().to_string(); |
| 114 | + |
| 115 | + // Write checksum content |
| 116 | + { |
| 117 | + let mut file = std::fs::File::create(&checksum_path).unwrap(); |
| 118 | + writeln!( |
| 119 | + file, |
| 120 | + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 {}", |
| 121 | + test_file.to_str().unwrap() |
| 122 | + ) |
| 123 | + .unwrap(); |
| 124 | + } |
| 125 | + |
| 126 | + (checksum_file, checksum_path) |
| 127 | + }) |
| 128 | + .bench_values(|(_checksum_file, checksum_path)| { |
| 129 | + black_box(run_util_function( |
| 130 | + uumain, |
| 131 | + &["--sha256", "--check", &checksum_path], |
| 132 | + )); |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +fn main() { |
| 137 | + divan::main(); |
| 138 | +} |
0 commit comments