Skip to content

Commit 98910ff

Browse files
committed
benchmark: move some functions in uucore
1 parent 77c3498 commit 98910ff

File tree

3 files changed

+64
-56
lines changed

3 files changed

+64
-56
lines changed

src/uu/cp/benches/cp_bench.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,11 @@
44
// file that was distributed with this source code.
55

66
use divan::{Bencher, black_box};
7-
use std::fs::{self, File};
8-
use std::io::Write;
7+
use std::fs;
98
use std::path::Path;
109
use tempfile::TempDir;
1110
use uu_cp::uumain;
12-
use uucore::benchmark::{fs_tree, run_util_function};
13-
14-
fn remove_path(path: &Path) {
15-
if !path.exists() {
16-
return;
17-
}
18-
19-
if path.is_dir() {
20-
fs::remove_dir_all(path).unwrap();
21-
} else {
22-
fs::remove_file(path).unwrap();
23-
}
24-
}
11+
use uucore::benchmark::{binary_data, fs_tree, fs_utils, run_util_function};
2512

2613
fn bench_cp_directory<F>(bencher: Bencher, args: &[&str], setup_source: F)
2714
where
@@ -38,7 +25,7 @@ where
3825
let dest_str = dest.to_str().unwrap();
3926

4027
bencher.bench(|| {
41-
remove_path(&dest);
28+
fs_utils::remove_path(&dest);
4229

4330
let mut full_args = Vec::with_capacity(args.len() + 2);
4431
full_args.extend_from_slice(args);
@@ -99,16 +86,13 @@ fn cp_large_file(bencher: Bencher, size_mb: usize) {
9986
let source = temp_dir.path().join("source.bin");
10087
let dest = temp_dir.path().join("dest.bin");
10188

102-
let buffer = vec![b'x'; size_mb * 1024 * 1024];
103-
let mut file = File::create(&source).unwrap();
104-
file.write_all(&buffer).unwrap();
105-
file.sync_all().unwrap();
89+
binary_data::create_file(&source, size_mb, b'x');
10690

10791
let source_str = source.to_str().unwrap();
10892
let dest_str = dest.to_str().unwrap();
10993

11094
bencher.bench(|| {
111-
remove_path(&dest);
95+
fs_utils::remove_path(&dest);
11296

11397
black_box(run_util_function(uumain, &[source_str, dest_str]));
11498
});

src/uu/dd/benches/dd_bench.rs

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,9 @@
44
// file that was distributed with this source code.
55

66
use divan::{Bencher, black_box};
7-
use std::fs::{self, File};
8-
use std::io::Write;
9-
use std::path::Path;
107
use tempfile::TempDir;
118
use uu_dd::uumain;
12-
use uucore::benchmark::run_util_function;
13-
14-
fn create_test_file(path: &Path, size_mb: usize) {
15-
let buffer = vec![b'x'; size_mb * 1024 * 1024];
16-
let mut file = File::create(path).unwrap();
17-
file.write_all(&buffer).unwrap();
18-
file.sync_all().unwrap();
19-
}
20-
21-
fn remove_file(path: &Path) {
22-
if path.exists() {
23-
fs::remove_file(path).unwrap();
24-
}
25-
}
9+
use uucore::benchmark::{binary_data, fs_utils, run_util_function};
2610

2711
/// Benchmark basic dd copy with default settings
2812
#[divan::bench(args = [32])]
@@ -31,13 +15,13 @@ fn dd_copy_default(bencher: Bencher, size_mb: usize) {
3115
let input = temp_dir.path().join("input.bin");
3216
let output = temp_dir.path().join("output.bin");
3317

34-
create_test_file(&input, size_mb);
18+
binary_data::create_file(&input, size_mb, b'x');
3519

3620
let input_str = input.to_str().unwrap();
3721
let output_str = output.to_str().unwrap();
3822

3923
bencher.bench(|| {
40-
remove_file(&output);
24+
fs_utils::remove_path(&output);
4125
black_box(run_util_function(
4226
uumain,
4327
&[
@@ -56,13 +40,13 @@ fn dd_copy_4k_blocks(bencher: Bencher, size_mb: usize) {
5640
let input = temp_dir.path().join("input.bin");
5741
let output = temp_dir.path().join("output.bin");
5842

59-
create_test_file(&input, size_mb);
43+
binary_data::create_file(&input, size_mb, b'x');
6044

6145
let input_str = input.to_str().unwrap();
6246
let output_str = output.to_str().unwrap();
6347

6448
bencher.bench(|| {
65-
remove_file(&output);
49+
fs_utils::remove_path(&output);
6650
black_box(run_util_function(
6751
uumain,
6852
&[
@@ -82,13 +66,13 @@ fn dd_copy_64k_blocks(bencher: Bencher, size_mb: usize) {
8266
let input = temp_dir.path().join("input.bin");
8367
let output = temp_dir.path().join("output.bin");
8468

85-
create_test_file(&input, size_mb);
69+
binary_data::create_file(&input, size_mb, b'x');
8670

8771
let input_str = input.to_str().unwrap();
8872
let output_str = output.to_str().unwrap();
8973

9074
bencher.bench(|| {
91-
remove_file(&output);
75+
fs_utils::remove_path(&output);
9276
black_box(run_util_function(
9377
uumain,
9478
&[
@@ -108,13 +92,13 @@ fn dd_copy_1m_blocks(bencher: Bencher, size_mb: usize) {
10892
let input = temp_dir.path().join("input.bin");
10993
let output = temp_dir.path().join("output.bin");
11094

111-
create_test_file(&input, size_mb);
95+
binary_data::create_file(&input, size_mb, b'x');
11296

11397
let input_str = input.to_str().unwrap();
11498
let output_str = output.to_str().unwrap();
11599

116100
bencher.bench(|| {
117-
remove_file(&output);
101+
fs_utils::remove_path(&output);
118102
black_box(run_util_function(
119103
uumain,
120104
&[
@@ -134,13 +118,13 @@ fn dd_copy_separate_blocks(bencher: Bencher, size_mb: usize) {
134118
let input = temp_dir.path().join("input.bin");
135119
let output = temp_dir.path().join("output.bin");
136120

137-
create_test_file(&input, size_mb);
121+
binary_data::create_file(&input, size_mb, b'x');
138122

139123
let input_str = input.to_str().unwrap();
140124
let output_str = output.to_str().unwrap();
141125

142126
bencher.bench(|| {
143-
remove_file(&output);
127+
fs_utils::remove_path(&output);
144128
black_box(run_util_function(
145129
uumain,
146130
&[
@@ -161,13 +145,13 @@ fn dd_copy_partial(bencher: Bencher, size_mb: usize) {
161145
let input = temp_dir.path().join("input.bin");
162146
let output = temp_dir.path().join("output.bin");
163147

164-
create_test_file(&input, size_mb);
148+
binary_data::create_file(&input, size_mb, b'x');
165149

166150
let input_str = input.to_str().unwrap();
167151
let output_str = output.to_str().unwrap();
168152

169153
bencher.bench(|| {
170-
remove_file(&output);
154+
fs_utils::remove_path(&output);
171155
black_box(run_util_function(
172156
uumain,
173157
&[
@@ -188,13 +172,13 @@ fn dd_copy_with_skip(bencher: Bencher, size_mb: usize) {
188172
let input = temp_dir.path().join("input.bin");
189173
let output = temp_dir.path().join("output.bin");
190174

191-
create_test_file(&input, size_mb);
175+
binary_data::create_file(&input, size_mb, b'x');
192176

193177
let input_str = input.to_str().unwrap();
194178
let output_str = output.to_str().unwrap();
195179

196180
bencher.bench(|| {
197-
remove_file(&output);
181+
fs_utils::remove_path(&output);
198182
black_box(run_util_function(
199183
uumain,
200184
&[
@@ -215,13 +199,13 @@ fn dd_copy_with_seek(bencher: Bencher, size_mb: usize) {
215199
let input = temp_dir.path().join("input.bin");
216200
let output = temp_dir.path().join("output.bin");
217201

218-
create_test_file(&input, size_mb);
202+
binary_data::create_file(&input, size_mb, b'x');
219203

220204
let input_str = input.to_str().unwrap();
221205
let output_str = output.to_str().unwrap();
222206

223207
bencher.bench(|| {
224-
remove_file(&output);
208+
fs_utils::remove_path(&output);
225209
black_box(run_util_function(
226210
uumain,
227211
&[
@@ -242,13 +226,13 @@ fn dd_copy_8k_blocks(bencher: Bencher, size_mb: usize) {
242226
let input = temp_dir.path().join("input.bin");
243227
let output = temp_dir.path().join("output.bin");
244228

245-
create_test_file(&input, size_mb);
229+
binary_data::create_file(&input, size_mb, b'x');
246230

247231
let input_str = input.to_str().unwrap();
248232
let output_str = output.to_str().unwrap();
249233

250234
bencher.bench(|| {
251-
remove_file(&output);
235+
fs_utils::remove_path(&output);
252236
black_box(run_util_function(
253237
uumain,
254238
&[

src/uucore/src/lib/features/benchmark.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,46 @@ pub mod text_data {
289289
}
290290
}
291291

292+
/// Binary data generation utilities for benchmarking
293+
pub mod binary_data {
294+
use std::fs::File;
295+
use std::io::Write;
296+
use std::path::Path;
297+
298+
/// Create a binary file filled with a repeated pattern
299+
///
300+
/// Creates a file of the specified size (in MB) filled with the given byte pattern.
301+
/// This is useful for benchmarking utilities that work with large binary files like dd, cp, etc.
302+
pub fn create_file(path: &Path, size_mb: usize, pattern: u8) {
303+
let buffer = vec![pattern; size_mb * 1024 * 1024];
304+
let mut file = File::create(path).unwrap();
305+
file.write_all(&buffer).unwrap();
306+
file.sync_all().unwrap();
307+
}
308+
}
309+
310+
/// Filesystem utilities for benchmarking
311+
pub mod fs_utils {
312+
use std::fs;
313+
use std::path::Path;
314+
315+
/// Remove a file or directory if it exists
316+
///
317+
/// This is a convenience function for cleaning up between benchmark iterations.
318+
/// It handles both files and directories, and is a no-op if the path doesn't exist.
319+
pub fn remove_path(path: &Path) {
320+
if !path.exists() {
321+
return;
322+
}
323+
324+
if path.is_dir() {
325+
fs::remove_dir_all(path).unwrap();
326+
} else {
327+
fs::remove_file(path).unwrap();
328+
}
329+
}
330+
}
331+
292332
/// Filesystem tree generation utilities for benchmarking
293333
pub mod fs_tree {
294334
use std::fs::{self, File};

0 commit comments

Comments
 (0)