| 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 tempfile::TempDir; |
| 8 |
use uu_fold::uumain; |
| 9 |
use uucore::benchmark::{create_test_file, get_bench_args}; |
| 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 mut data = String::with_capacity(num_lines * 110); |
| 15 |
for i in 0..num_lines { |
| 16 |
data.push_str("This is a very long line number "); |
| 17 |
append_usize(&mut data, i); |
| 18 |
data.push_str(" that definitely needs to be folded at the default width of 80 columns\n"); |
| 19 |
} |
| 20 |
let temp_dir = TempDir::new().unwrap(); |
| 21 |
let file_path = create_test_file(data.as_bytes(), temp_dir.path()); |
| 22 |
|
| 23 |
bencher |
| 24 |
.with_inputs(|| get_bench_args(&[&file_path]).into_iter()) |
| 25 |
.bench_values(|args| black_box(uumain(args))); |
| 26 |
} |
| 27 |
|
| 28 |
/// Benchmark folding with custom width |
| 29 |
#[divan::bench(args = [50_000])] |
| 30 |
fn fold_custom_width(bencher: Bencher, num_lines: usize) { |
| 31 |
let mut data = String::with_capacity(num_lines * 80); |
| 32 |
for i in 0..num_lines { |
| 33 |
data.push_str("Line "); |
| 34 |
append_usize(&mut data, i); |
| 35 |
data.push_str(" with enough text to exceed width 40 characters and require folding\n"); |
| 36 |
} |
| 37 |
let temp_dir = TempDir::new().unwrap(); |
| 38 |
let file_path = create_test_file(data.as_bytes(), temp_dir.path()); |
| 39 |
|
| 40 |
bencher |
| 41 |
.with_inputs(|| get_bench_args(&[&"-w", &"40", &file_path]).into_iter()) |
| 42 |
.bench_values(|args| black_box(uumain(args))); |
| 43 |
} |
| 44 |
|
| 45 |
fn main() { |
| 46 |
divan::main(); |
| 47 |
} |
| 48 |
|
| 49 |
fn append_usize(buf: &mut String, mut value: usize) { |
| 50 |
let mut digits = [0u8; 20]; |
| 51 |
let mut idx = digits.len(); |
| 52 |
|
| 53 |
if value == 0 { |
| 54 |
buf.push('0'); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
while value > 0 { |
| 59 |
idx -= 1; |
| 60 |
digits[idx] = b'0' + (value % 10) as u8; |
| 61 |
value /= 10; |
| 62 |
} |
| 63 |
|
| 64 |
buf.push_str(std::str::from_utf8(&digits[idx..]).unwrap()); |
| 65 |
} |