Debian Codegraph @

src/uu/ls/benches/ls_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 use divan::{Bencher, black_box};
7 use std::fs;
8 use tempfile::TempDir;
9 use uu_ls::uumain;
10 use uucore::benchmark::{fs_tree, run_util_function};
11
12 /// Helper to run ls with given arguments on a directory
13 fn bench_ls_with_args(bencher: Bencher, temp_dir: &TempDir, args: &[&str]) {
14 let temp_path_str = temp_dir.path().to_str().unwrap();
15 let mut full_args = vec!["-R"];
16 full_args.extend_from_slice(args);
17 full_args.push(temp_path_str);
18
19 bencher.bench(|| {
20 black_box(run_util_function(uumain, &full_args));
21 });
22 }
23
24 /// Benchmark ls -R on balanced directory tree
25 #[divan::bench(args = [(6, 4, 15)])]
26 fn ls_recursive_balanced_tree(
27 bencher: Bencher,
28 (depth, dirs_per_level, files_per_dir): (usize, usize, usize),
29 ) {
30 let temp_dir = TempDir::new().unwrap();
31 fs_tree::create_balanced_tree(temp_dir.path(), depth, dirs_per_level, files_per_dir);
32 bench_ls_with_args(bencher, &temp_dir, &[]);
33 }
34
35 /// Benchmark ls -R -a -l on balanced directory tree (tests PR #8728 optimization)
36 #[divan::bench(args = [(6, 4, 15)])]
37 fn ls_recursive_long_all_balanced_tree(
38 bencher: Bencher,
39 (depth, dirs_per_level, files_per_dir): (usize, usize, usize),
40 ) {
41 let temp_dir = TempDir::new().unwrap();
42 fs_tree::create_balanced_tree(temp_dir.path(), depth, dirs_per_level, files_per_dir);
43 bench_ls_with_args(bencher, &temp_dir, &["-a", "-l"]);
44 }
45
46 /// Benchmark ls -R on wide directory structures
47 #[divan::bench(args = [(10000, 1000)])]
48 fn ls_recursive_wide_tree(bencher: Bencher, (total_files, total_dirs): (usize, usize)) {
49 let temp_dir = TempDir::new().unwrap();
50 fs_tree::create_wide_tree(temp_dir.path(), total_files, total_dirs);
51 bench_ls_with_args(bencher, &temp_dir, &[]);
52 }
53
54 /// Benchmark ls -R -a -l on wide directory structures
55 #[divan::bench(args = [(15000, 1500)])]
56 fn ls_recursive_long_all_wide_tree(bencher: Bencher, (total_files, total_dirs): (usize, usize)) {
57 let temp_dir = TempDir::new().unwrap();
58 fs_tree::create_wide_tree(temp_dir.path(), total_files, total_dirs);
59 bench_ls_with_args(bencher, &temp_dir, &["-a", "-l"]);
60 }
61
62 /// Benchmark ls -R on deep directory structures
63 #[divan::bench(args = [(200, 2)])]
64 fn ls_recursive_deep_tree(bencher: Bencher, (depth, files_per_level): (usize, usize)) {
65 let temp_dir = TempDir::new().unwrap();
66 fs_tree::create_deep_tree(temp_dir.path(), depth, files_per_level);
67 bench_ls_with_args(bencher, &temp_dir, &[]);
68 }
69
70 /// Benchmark ls -R -a -l on deep directory structures
71 #[divan::bench(args = [(100, 4)])]
72 fn ls_recursive_long_all_deep_tree(bencher: Bencher, (depth, files_per_level): (usize, usize)) {
73 let temp_dir = TempDir::new().unwrap();
74 fs_tree::create_deep_tree(temp_dir.path(), depth, files_per_level);
75 bench_ls_with_args(bencher, &temp_dir, &["-a", "-l"]);
76 }
77
78 /// Benchmark ls -R on mixed file types (comprehensive real-world test)
79 #[divan::bench]
80 fn ls_recursive_mixed_tree(bencher: Bencher) {
81 let temp_dir = TempDir::new().unwrap();
82 fs_tree::create_mixed_tree(temp_dir.path());
83
84 for i in 0..10 {
85 let subdir = temp_dir.path().join(format!("mixed_branch_{i}"));
86 fs::create_dir(&subdir).unwrap();
87 fs_tree::create_mixed_tree(&subdir);
88 }
89
90 bench_ls_with_args(bencher, &temp_dir, &[]);
91 }
92
93 /// Benchmark ls -R -a -l on mixed file types (most comprehensive test)
94 #[divan::bench]
95 fn ls_recursive_long_all_mixed_tree(bencher: Bencher) {
96 let temp_dir = TempDir::new().unwrap();
97 fs_tree::create_mixed_tree(temp_dir.path());
98
99 for i in 0..10 {
100 let subdir = temp_dir.path().join(format!("mixed_branch_{i}"));
101 fs::create_dir(&subdir).unwrap();
102 fs_tree::create_mixed_tree(&subdir);
103 }
104
105 bench_ls_with_args(bencher, &temp_dir, &["-a", "-l"]);
106 }
107
108 fn main() {
109 divan::main();
110 }