|
| 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::fmt::Write; |
| 8 | +use uu_expand::uumain; |
| 9 | +use uucore::benchmark::{create_test_file, run_util_function}; |
| 10 | + |
| 11 | +/// Helper function to run expand benchmark with generated data |
| 12 | +fn bench_expand(bencher: Bencher, data: impl AsRef<[u8]>, args: &[&str]) { |
| 13 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 14 | + let file_path = create_test_file(data.as_ref(), temp_dir.path()); |
| 15 | + let file_path_str = file_path.to_str().unwrap(); |
| 16 | + |
| 17 | + let mut all_args = vec![]; |
| 18 | + all_args.extend_from_slice(args); |
| 19 | + all_args.push(file_path_str); |
| 20 | + |
| 21 | + bencher.bench(|| { |
| 22 | + black_box(run_util_function(uumain, &all_args)); |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +/// Benchmark expanding tabs on files with many short lines |
| 27 | +#[divan::bench(args = [100_000])] |
| 28 | +fn expand_many_lines(bencher: Bencher, num_lines: usize) { |
| 29 | + let data = (0..num_lines).fold(String::new(), |mut acc, i| { |
| 30 | + writeln!(&mut acc, "line{i}\tvalue{}\tdata{}", i * 2, i * 3).unwrap(); |
| 31 | + acc |
| 32 | + }); |
| 33 | + bench_expand(bencher, data, &[]); |
| 34 | +} |
| 35 | + |
| 36 | +/// Benchmark expanding tabs with custom tab stops |
| 37 | +#[divan::bench(args = [50_000])] |
| 38 | +fn expand_custom_tabstops(bencher: Bencher, num_lines: usize) { |
| 39 | + let data = (0..num_lines).fold(String::new(), |mut acc, i| { |
| 40 | + writeln!(&mut acc, "a\tb\tc\td\te{i}").unwrap(); |
| 41 | + acc |
| 42 | + }); |
| 43 | + bench_expand(bencher, data, &["--tabs=4,8,12"]); |
| 44 | +} |
| 45 | + |
| 46 | +fn main() { |
| 47 | + divan::main(); |
| 48 | +} |
0 commit comments