|
| 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_fold::uumain; |
| 9 | +use uucore::benchmark::{create_test_file, run_util_function}; |
| 10 | + |
| 11 | +/// Benchmark folding many short lines |
| 12 | +#[divan::bench(args = [100_000])] |
| 13 | +fn fold_many_lines(bencher: Bencher, num_lines: usize) { |
| 14 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 15 | + // Create long lines that need folding |
| 16 | + let data = (0..num_lines) |
| 17 | + .fold(String::new(), |mut acc, i| { |
| 18 | + writeln!(&mut acc, "This is a very long line number {i} that definitely needs to be folded at the default width of 80 columns").unwrap(); |
| 19 | + acc |
| 20 | + }); |
| 21 | + let file_path = create_test_file(data.as_bytes(), temp_dir.path()); |
| 22 | + let file_path_str = file_path.to_str().unwrap(); |
| 23 | + |
| 24 | + bencher.bench(|| { |
| 25 | + black_box(run_util_function(uumain, &[file_path_str])); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +/// Benchmark folding with custom width |
| 30 | +#[divan::bench(args = [50_000])] |
| 31 | +fn fold_custom_width(bencher: Bencher, num_lines: usize) { |
| 32 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 33 | + let data = (0..num_lines).fold(String::new(), |mut acc, i| { |
| 34 | + writeln!( |
| 35 | + &mut acc, |
| 36 | + "Line {i} with enough text to exceed width 40 characters and require folding" |
| 37 | + ) |
| 38 | + .unwrap(); |
| 39 | + acc |
| 40 | + }); |
| 41 | + let file_path = create_test_file(data.as_bytes(), temp_dir.path()); |
| 42 | + let file_path_str = file_path.to_str().unwrap(); |
| 43 | + |
| 44 | + bencher.bench(|| { |
| 45 | + black_box(run_util_function(uumain, &["-w", "40", file_path_str])); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +fn main() { |
| 50 | + divan::main(); |
| 51 | +} |
0 commit comments