Debian Codegraph @

src/uu/ptx/benches/ptx_bench.rs

raw
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 //! Benchmarks for `ptx`.
7
8 use divan::{Bencher, black_box};
9 use uu_ptx::uumain;
10 use uucore::benchmark::{run_util_function, setup_test_file, text_data};
11
12 fn bench_ptx(bencher: Bencher, data: &[u8], args: &[&str]) {
13 let file_path = setup_test_file(data);
14 let file_path_str = file_path.to_str().unwrap();
15
16 let mut full_args: Vec<&str> = args.to_vec();
17 full_args.push(file_path_str);
18
19 bencher.bench(|| {
20 black_box(run_util_function(uumain, &full_args));
21 });
22 }
23
24 /// Build a fixed ~1 MiB input spread across num_lines lines.
25 fn fixed_size_data(num_lines: usize) -> Vec<u8> {
26 let line_len = (1024 * 1024 / num_lines).max(1);
27 text_data::generate_by_lines(num_lines, line_len)
28 }
29
30 /// Benchmark the common case of many short lines.
31 #[divan::bench(args = [100_000])]
32 fn ptx_short_lines(bencher: Bencher, num_lines: usize) {
33 let data = text_data::generate_by_lines(num_lines, 80);
34 bench_ptx(bencher, &data, &[]);
35 }
36
37 /// Benchmark a fixed ~1 MiB input spread across 100 lines.
38 #[divan::bench(args = [100])]
39 fn ptx_long_lines(bencher: Bencher, num_lines: usize) {
40 bench_ptx(bencher, &fixed_size_data(num_lines), &[]);
41 }
42
43 /// Benchmark -r on many short lines.
44 #[divan::bench(args = [100_000])]
45 fn ptx_input_references_short_lines(bencher: Bencher, num_lines: usize) {
46 let data = text_data::generate_by_lines(num_lines, 80);
47 bench_ptx(bencher, &data, &["-r"]);
48 }
49
50 /// Benchmark -r on long lines
51 #[divan::bench(args = [1000])]
52 fn ptx_input_references_long_lines(bencher: Bencher, num_lines: usize) {
53 bench_ptx(bencher, &fixed_size_data(num_lines), &["-r"]);
54 }
55
56 fn tex_special_data(num_lines: usize) -> Vec<u8> {
57 let lines = [
58 "the permuted index of \\alpha sorted around each keyword",
59 "context $x$ lines paired with file_name and A&B output",
60 "roughly 50% of {group} entries escaped as #1 or x_1 here",
61 "each \\ref and $y$ carries a file_name style suffix too",
62 "sorted A&B context around 90% of the {group} keywords",
63 "index entries with x_1 and #1 spread across the \\alpha line",
64 ];
65 text_data::generate_data_from_words(&lines, num_lines)
66 }
67
68 /// Benchmark -T on many short lines.
69 #[divan::bench(args = [10000])]
70 fn ptx_tex(bencher: Bencher, num_lines: usize) {
71 let data = tex_special_data(num_lines);
72 bench_ptx(bencher, &data, &["-T"]);
73 }
74
75 fn main() {
76 divan::main();
77 }