44// file that was distributed with this source code.
55
66use divan:: { Bencher , black_box} ;
7- use std:: fs:: { self , File } ;
8- use std:: io:: Write ;
9- use std:: path:: Path ;
107use tempfile:: TempDir ;
118use 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 & [
0 commit comments