| 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, get_bench_args}; |
| 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 |
|
| 29 |
bencher |
| 30 |
.with_inputs(|| get_bench_args(&[&file_path]).into_iter()) |
| 31 |
.bench_values(|args| black_box(uumain(args))); |
| 32 |
} |
| 33 |
|
| 34 |
/// Benchmark large file with spaces (tests performance on large files) |
| 35 |
#[divan::bench(args = [10])] |
| 36 |
fn unexpand_large_file(bencher: Bencher, size_mb: usize) { |
| 37 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 38 |
|
| 39 |
// Generate approximately size_mb worth of indented lines |
| 40 |
let line_size = 50; // approximate bytes per line |
| 41 |
let num_lines = (size_mb * 1024 * 1024) / line_size; |
| 42 |
let data = generate_indented_text(num_lines); |
| 43 |
let file_path = create_test_file(&data, temp_dir.path()); |
| 44 |
|
| 45 |
bencher |
| 46 |
.with_inputs(|| get_bench_args(&[&file_path]).into_iter()) |
| 47 |
.bench_values(|args| black_box(uumain(args))); |
| 48 |
} |
| 49 |
|
| 50 |
fn main() { |
| 51 |
divan::main(); |
| 52 |
} |