Debian Codegraph @

src/uu/rm/benches/rm_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 tempfile::TempDir;
8 use uu_rm::uumain;
9 use uucore::benchmark::{fs_tree, run_util_function};
10
11 /// Benchmark removing a single file (repeated to reach 100ms)
12 #[divan::bench]
13 fn rm_single_file(bencher: Bencher) {
14 bencher
15 .with_inputs(|| {
16 let temp_dir = TempDir::new().unwrap();
17 fs_tree::create_wide_tree(temp_dir.path(), 1000, 0);
18 let paths: Vec<String> = (0..1000)
19 .map(|i| {
20 temp_dir
21 .path()
22 .join(format!("f{i}"))
23 .to_str()
24 .unwrap()
25 .to_string()
26 })
27 .collect();
28 (temp_dir, paths)
29 })
30 .bench_values(|(temp_dir, paths)| {
31 for path in &paths {
32 black_box(run_util_function(uumain, &[path]));
33 }
34 drop(temp_dir);
35 });
36 }
37
38 /// Benchmark removing multiple files
39 #[divan::bench]
40 fn rm_multiple_files(bencher: Bencher) {
41 bencher
42 .with_inputs(|| {
43 let temp_dir = TempDir::new().unwrap();
44 fs_tree::create_wide_tree(temp_dir.path(), 1000, 0);
45 let paths: Vec<String> = (0..1000)
46 .map(|i| {
47 temp_dir
48 .path()
49 .join(format!("f{i}"))
50 .to_str()
51 .unwrap()
52 .to_string()
53 })
54 .collect();
55 (temp_dir, paths)
56 })
57 .bench_values(|(temp_dir, paths)| {
58 let args: Vec<&str> = paths.iter().map(String::as_str).collect();
59 black_box(run_util_function(uumain, &args));
60 drop(temp_dir);
61 });
62 }
63
64 /// Benchmark recursive directory removal
65 #[divan::bench]
66 fn rm_recursive_tree(bencher: Bencher) {
67 bencher
68 .with_inputs(|| {
69 let temp_dir = TempDir::new().unwrap();
70 let test_dir = temp_dir.path().join("test_tree");
71 std::fs::create_dir(&test_dir).unwrap();
72 // Increase depth and width for longer benchmark
73 fs_tree::create_balanced_tree(&test_dir, 5, 5, 10);
74 (temp_dir, test_dir.to_str().unwrap().to_string())
75 })
76 .bench_values(|(temp_dir, path)| {
77 black_box(run_util_function(uumain, &["-r", &path]));
78 drop(temp_dir);
79 });
80 }
81
82 /// Benchmark force removal
83 #[divan::bench]
84 fn rm_force_files(bencher: Bencher) {
85 bencher
86 .with_inputs(|| {
87 let temp_dir = TempDir::new().unwrap();
88 fs_tree::create_wide_tree(temp_dir.path(), 1000, 0);
89 let paths: Vec<String> = (0..1000)
90 .map(|i| {
91 temp_dir
92 .path()
93 .join(format!("f{i}"))
94 .to_str()
95 .unwrap()
96 .to_string()
97 })
98 .collect();
99 (temp_dir, paths)
100 })
101 .bench_values(|(temp_dir, paths)| {
102 let mut args = vec!["-f"];
103 let path_refs: Vec<&str> = paths.iter().map(String::as_str).collect();
104 args.extend(path_refs);
105 black_box(run_util_function(uumain, &args));
106 drop(temp_dir);
107 });
108 }
109
110 fn main() {
111 divan::main();
112 }