Debian Codegraph @

src/uu/dd/benches/dd_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_dd::uumain;
9 use uucore::benchmark::{binary_data, fs_utils, run_util_function};
10
11 /// Benchmark basic dd copy with default settings
12 #[divan::bench]
13 fn dd_copy_default(bencher: Bencher) {
14 let size_mb = 32;
15 let temp_dir = TempDir::new().unwrap();
16 let input = temp_dir.path().join("input.bin");
17 let output = temp_dir.path().join("output.bin");
18
19 binary_data::create_file(&input, size_mb, b'x');
20
21 let input_str = input.to_str().unwrap();
22 let output_str = output.to_str().unwrap();
23
24 bencher.bench(|| {
25 fs_utils::remove_path(&output);
26 black_box(run_util_function(
27 uumain,
28 &[
29 &format!("if={input_str}"),
30 &format!("of={output_str}"),
31 "status=none",
32 ],
33 ));
34 });
35 }
36
37 /// Benchmark dd copy with 4KB block size (common page size)
38 #[divan::bench]
39 fn dd_copy_4k_blocks(bencher: Bencher) {
40 let size_mb = 24;
41 let temp_dir = TempDir::new().unwrap();
42 let input = temp_dir.path().join("input.bin");
43 let output = temp_dir.path().join("output.bin");
44
45 binary_data::create_file(&input, size_mb, b'x');
46
47 let input_str = input.to_str().unwrap();
48 let output_str = output.to_str().unwrap();
49
50 bencher.bench(|| {
51 fs_utils::remove_path(&output);
52 black_box(run_util_function(
53 uumain,
54 &[
55 &format!("if={input_str}"),
56 &format!("of={output_str}"),
57 "bs=4K",
58 "status=none",
59 ],
60 ));
61 });
62 }
63
64 /// Benchmark dd copy with 64KB block size
65 #[divan::bench]
66 fn dd_copy_64k_blocks(bencher: Bencher) {
67 let size_mb = 64;
68 let temp_dir = TempDir::new().unwrap();
69 let input = temp_dir.path().join("input.bin");
70 let output = temp_dir.path().join("output.bin");
71
72 binary_data::create_file(&input, size_mb, b'x');
73
74 let input_str = input.to_str().unwrap();
75 let output_str = output.to_str().unwrap();
76
77 bencher.bench(|| {
78 fs_utils::remove_path(&output);
79 black_box(run_util_function(
80 uumain,
81 &[
82 &format!("if={input_str}"),
83 &format!("of={output_str}"),
84 "bs=64K",
85 "status=none",
86 ],
87 ));
88 });
89 }
90
91 /// Benchmark dd copy with 1MB block size
92 #[divan::bench]
93 fn dd_copy_1m_blocks(bencher: Bencher) {
94 let size_mb = 128;
95 let temp_dir = TempDir::new().unwrap();
96 let input = temp_dir.path().join("input.bin");
97 let output = temp_dir.path().join("output.bin");
98
99 binary_data::create_file(&input, size_mb, b'x');
100
101 let input_str = input.to_str().unwrap();
102 let output_str = output.to_str().unwrap();
103
104 bencher.bench(|| {
105 fs_utils::remove_path(&output);
106 black_box(run_util_function(
107 uumain,
108 &[
109 &format!("if={input_str}"),
110 &format!("of={output_str}"),
111 "bs=1M",
112 "status=none",
113 ],
114 ));
115 });
116 }
117
118 /// Benchmark dd copy with separate input and output block sizes
119 #[divan::bench]
120 fn dd_copy_separate_blocks(bencher: Bencher) {
121 let size_mb = 48;
122 let temp_dir = TempDir::new().unwrap();
123 let input = temp_dir.path().join("input.bin");
124 let output = temp_dir.path().join("output.bin");
125
126 binary_data::create_file(&input, size_mb, b'x');
127
128 let input_str = input.to_str().unwrap();
129 let output_str = output.to_str().unwrap();
130
131 bencher.bench(|| {
132 fs_utils::remove_path(&output);
133 black_box(run_util_function(
134 uumain,
135 &[
136 &format!("if={input_str}"),
137 &format!("of={output_str}"),
138 "ibs=8K",
139 "obs=16K",
140 "status=none",
141 ],
142 ));
143 });
144 }
145
146 /// Benchmark dd with count limit (partial copy)
147 #[divan::bench]
148 fn dd_copy_partial(bencher: Bencher) {
149 let size_mb = 32;
150 let temp_dir = TempDir::new().unwrap();
151 let input = temp_dir.path().join("input.bin");
152 let output = temp_dir.path().join("output.bin");
153
154 binary_data::create_file(&input, size_mb, b'x');
155
156 let input_str = input.to_str().unwrap();
157 let output_str = output.to_str().unwrap();
158
159 bencher.bench(|| {
160 fs_utils::remove_path(&output);
161 black_box(run_util_function(
162 uumain,
163 &[
164 &format!("if={input_str}"),
165 &format!("of={output_str}"),
166 "bs=4K",
167 "count=1024",
168 "status=none",
169 ],
170 ));
171 });
172 }
173
174 /// Benchmark dd with skip (seeking in input)
175 #[divan::bench]
176 fn dd_copy_with_skip(bencher: Bencher) {
177 let size_mb = 48;
178 let temp_dir = TempDir::new().unwrap();
179 let input = temp_dir.path().join("input.bin");
180 let output = temp_dir.path().join("output.bin");
181
182 binary_data::create_file(&input, size_mb, b'x');
183
184 let input_str = input.to_str().unwrap();
185 let output_str = output.to_str().unwrap();
186
187 bencher.bench(|| {
188 fs_utils::remove_path(&output);
189 black_box(run_util_function(
190 uumain,
191 &[
192 &format!("if={input_str}"),
193 &format!("of={output_str}"),
194 "bs=4K",
195 "skip=256",
196 "status=none",
197 ],
198 ));
199 });
200 }
201
202 /// Benchmark dd with seek (seeking in output)
203 #[divan::bench]
204 fn dd_copy_with_seek(bencher: Bencher) {
205 let size_mb = 48;
206 let temp_dir = TempDir::new().unwrap();
207 let input = temp_dir.path().join("input.bin");
208 let output = temp_dir.path().join("output.bin");
209
210 binary_data::create_file(&input, size_mb, b'x');
211
212 let input_str = input.to_str().unwrap();
213 let output_str = output.to_str().unwrap();
214
215 bencher.bench(|| {
216 fs_utils::remove_path(&output);
217 black_box(run_util_function(
218 uumain,
219 &[
220 &format!("if={input_str}"),
221 &format!("of={output_str}"),
222 "bs=4K",
223 "seek=256",
224 "status=none",
225 ],
226 ));
227 });
228 }
229
230 /// Benchmark dd with different block sizes for comparison
231 #[divan::bench]
232 fn dd_copy_8k_blocks(bencher: Bencher) {
233 let size_mb = 32;
234 let temp_dir = TempDir::new().unwrap();
235 let input = temp_dir.path().join("input.bin");
236 let output = temp_dir.path().join("output.bin");
237
238 binary_data::create_file(&input, size_mb, b'x');
239
240 let input_str = input.to_str().unwrap();
241 let output_str = output.to_str().unwrap();
242
243 bencher.bench(|| {
244 fs_utils::remove_path(&output);
245 black_box(run_util_function(
246 uumain,
247 &[
248 &format!("if={input_str}"),
249 &format!("of={output_str}"),
250 "bs=8K",
251 "status=none",
252 ],
253 ));
254 });
255 }
256
257 fn main() {
258 divan::main();
259 }