|
| 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_unexpand::uumain; |
| 8 | +use uucore::benchmark::{create_test_file, run_util_function}; |
| 9 | + |
| 10 | +/// Generate text data with leading spaces (typical unexpand use case) |
| 11 | +fn generate_indented_text(num_lines: usize) -> Vec<u8> { |
| 12 | + let mut data = Vec::new(); |
| 13 | + for i in 0..num_lines { |
| 14 | + // Add varying amounts of leading spaces (4, 8, 12, etc.) |
| 15 | + let indent = (i % 4 + 1) * 4; |
| 16 | + data.extend(vec![b' '; indent]); |
| 17 | + data.extend_from_slice(b"This is a line of text with leading spaces\n"); |
| 18 | + } |
| 19 | + data |
| 20 | +} |
| 21 | + |
| 22 | +/// Benchmark unexpanding many lines with leading spaces (most common use case) |
| 23 | +#[divan::bench(args = [100_000])] |
| 24 | +fn unexpand_many_lines(bencher: Bencher, num_lines: usize) { |
| 25 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 26 | + let data = generate_indented_text(num_lines); |
| 27 | + let file_path = create_test_file(&data, temp_dir.path()); |
| 28 | + let file_path_str = file_path.to_str().unwrap(); |
| 29 | + |
| 30 | + bencher.bench(|| { |
| 31 | + black_box(run_util_function(uumain, &[file_path_str])); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +/// Benchmark large file with spaces (tests performance on large files) |
| 36 | +#[divan::bench(args = [10])] |
| 37 | +fn unexpand_large_file(bencher: Bencher, size_mb: usize) { |
| 38 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 39 | + |
| 40 | + // Generate approximately size_mb worth of indented lines |
| 41 | + let line_size = 50; // approximate bytes per line |
| 42 | + let num_lines = (size_mb * 1024 * 1024) / line_size; |
| 43 | + let data = generate_indented_text(num_lines); |
| 44 | + let file_path = create_test_file(&data, temp_dir.path()); |
| 45 | + let file_path_str = file_path.to_str().unwrap(); |
| 46 | + |
| 47 | + bencher.bench(|| { |
| 48 | + black_box(run_util_function(uumain, &[file_path_str])); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +fn main() { |
| 53 | + divan::main(); |
| 54 | +} |
0 commit comments