Debian Codegraph @

src/uu/mv/benches/mv_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_mv::uumain;
9 use uucore::benchmark::{fs_tree, run_util_function};
10
11 /// Benchmark moving a single file (repeated to reach 100ms)
12 #[divan::bench]
13 fn mv_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 files: Vec<(String, String)> = (0..1000)
19 .map(|i| {
20 let src = temp_dir.path().join(format!("f{i}"));
21 let dst = temp_dir.path().join(format!("moved_{i}"));
22 (
23 src.to_str().unwrap().to_string(),
24 dst.to_str().unwrap().to_string(),
25 )
26 })
27 .collect();
28 (temp_dir, files)
29 })
30 .bench_values(|(temp_dir, files)| {
31 for (src, dst) in &files {
32 black_box(run_util_function(uumain, &[src, dst]));
33 }
34 drop(temp_dir);
35 });
36 }
37
38 /// Benchmark moving multiple files to directory
39 #[divan::bench]
40 fn mv_multiple_to_dir(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 dest_dir = temp_dir.path().join("dest");
46 std::fs::create_dir(&dest_dir).unwrap();
47
48 let mut args: Vec<String> = (0..1000)
49 .map(|i| {
50 temp_dir
51 .path()
52 .join(format!("f{i}"))
53 .to_str()
54 .unwrap()
55 .to_string()
56 })
57 .collect();
58 args.push(dest_dir.to_str().unwrap().to_string());
59 (temp_dir, args)
60 })
61 .bench_values(|(temp_dir, args)| {
62 let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
63 black_box(run_util_function(uumain, &arg_refs));
64 drop(temp_dir);
65 });
66 }
67
68 /// Benchmark moving directory recursively
69 #[divan::bench]
70 fn mv_directory(bencher: Bencher) {
71 bencher
72 .with_inputs(|| {
73 let temp_dir = TempDir::new().unwrap();
74 let src_dir = temp_dir.path().join("src_tree");
75 std::fs::create_dir(&src_dir).unwrap();
76 // Increase tree size for longer benchmark
77 fs_tree::create_balanced_tree(&src_dir, 5, 5, 10);
78 let dst_dir = temp_dir.path().join("dest_tree");
79 (
80 temp_dir,
81 src_dir.to_str().unwrap().to_string(),
82 dst_dir.to_str().unwrap().to_string(),
83 )
84 })
85 .bench_values(|(temp_dir, src, dst)| {
86 black_box(run_util_function(uumain, &[&src, &dst]));
87 drop(temp_dir);
88 });
89 }
90
91 /// Benchmark force overwrite
92 #[divan::bench]
93 fn mv_force_overwrite(bencher: Bencher) {
94 bencher
95 .with_inputs(|| {
96 let temp_dir = TempDir::new().unwrap();
97 fs_tree::create_wide_tree(temp_dir.path(), 2000, 0);
98 let files: Vec<(String, String)> = (0..1000)
99 .map(|i| {
100 let src = temp_dir.path().join(format!("f{i}"));
101 let dst = temp_dir.path().join(format!("f{}", i + 1000));
102 (
103 src.to_str().unwrap().to_string(),
104 dst.to_str().unwrap().to_string(),
105 )
106 })
107 .collect();
108 (temp_dir, files)
109 })
110 .bench_values(|(temp_dir, files)| {
111 for (src, dst) in &files {
112 black_box(run_util_function(uumain, &["-f", src, dst]));
113 }
114 drop(temp_dir);
115 });
116 }
117
118 fn main() {
119 divan::main();
120 }