Debian Codegraph @

src/uu/cp/benches/cp_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 std::path::Path;
9 use tempfile::TempDir;
10 use uu_cp::uumain;
11 use uucore::benchmark::{binary_data, fs_tree, fs_utils, get_bench_args, run_util_function};
12
13 fn bench_cp_directory<F>(bencher: Bencher, args: &[&str], setup_source: F)
14 where
15 F: Fn(&Path),
16 {
17 let temp_dir = TempDir::new().unwrap();
18 let source = temp_dir.path().join("source");
19 let dest = temp_dir.path().join("dest");
20
21 fs::create_dir(&source).unwrap();
22 setup_source(&source);
23
24 let source_str = source.to_str().unwrap();
25 let dest_str = dest.to_str().unwrap();
26
27 bencher.bench(|| {
28 fs_utils::remove_path(&dest);
29
30 let mut full_args = Vec::with_capacity(args.len() + 2);
31 full_args.extend_from_slice(args);
32 full_args.push(source_str);
33 full_args.push(dest_str);
34
35 black_box(run_util_function(uumain, &full_args));
36 });
37 }
38
39 #[divan::bench(args = [(5, 4, 10)])]
40 fn cp_recursive_balanced_tree(
41 bencher: Bencher,
42 (depth, dirs_per_level, files_per_dir): (usize, usize, usize),
43 ) {
44 bench_cp_directory(bencher, &["-R"], |source| {
45 fs_tree::create_balanced_tree(source, depth, dirs_per_level, files_per_dir);
46 });
47 }
48
49 #[divan::bench(args = [(5, 4, 10)])]
50 fn cp_archive_balanced_tree(
51 bencher: Bencher,
52 (depth, dirs_per_level, files_per_dir): (usize, usize, usize),
53 ) {
54 bench_cp_directory(bencher, &["-a"], |source| {
55 fs_tree::create_balanced_tree(source, depth, dirs_per_level, files_per_dir);
56 });
57 }
58
59 #[divan::bench(args = [(6000, 800)])]
60 fn cp_recursive_wide_tree(bencher: Bencher, (total_files, total_dirs): (usize, usize)) {
61 bench_cp_directory(bencher, &["-R"], |source| {
62 fs_tree::create_wide_tree(source, total_files, total_dirs);
63 });
64 }
65
66 #[divan::bench(args = [(120, 4)])]
67 fn cp_recursive_deep_tree(bencher: Bencher, (depth, files_per_level): (usize, usize)) {
68 bench_cp_directory(bencher, &["-R"], |source| {
69 fs_tree::create_deep_tree(source, depth, files_per_level);
70 });
71 }
72
73 #[divan::bench(args = [(5, 4, 10)])]
74 fn cp_preserve_metadata(
75 bencher: Bencher,
76 (depth, dirs_per_level, files_per_dir): (usize, usize, usize),
77 ) {
78 bench_cp_directory(bencher, &["-R", "--preserve=mode,timestamps"], |source| {
79 fs_tree::create_balanced_tree(source, depth, dirs_per_level, files_per_dir);
80 });
81 }
82
83 #[divan::bench(args = [16])]
84 fn cp_large_file(bencher: Bencher, size_mb: usize) {
85 bencher
86 .with_inputs(|| {
87 let temp_dir = TempDir::new().unwrap();
88 let source = temp_dir.path().join("source.bin");
89 binary_data::create_file(&source, size_mb, b'x');
90 // Use unique destination name to avoid filesystem allocation variance
91 let dest = temp_dir
92 .path()
93 .join(format!("dest_{}.bin", (&raw const temp_dir).addr()));
94 get_bench_args(&[&source, &dest]).into_iter()
95 })
96 .counter(divan::counter::BytesCount::new(size_mb * 1024 * 1024))
97 .bench_values(|args| black_box(uumain(args)));
98 }
99
100 fn main() {
101 divan::main();
102 }