4
4
// file that was distributed with this source code.
5
5
6
6
use divan:: { Bencher , black_box} ;
7
- use std:: fs:: File ;
8
- use std:: io:: { BufWriter , Write } ;
9
- use tempfile:: TempDir ;
7
+ use uu_tsort:: uumain;
8
+ use uucore:: benchmark:: { create_test_file, run_util_function} ;
10
9
11
10
/// Generate topological sort test data with different characteristics
12
11
fn generate_linear_chain ( num_nodes : usize ) -> Vec < u8 > {
@@ -117,42 +116,17 @@ fn generate_wide_dag(num_nodes: usize) -> Vec<u8> {
117
116
data
118
117
}
119
118
120
- /// Create a temporary file with test data
121
- fn create_test_file ( data : & [ u8 ] , temp_dir : & TempDir ) -> std:: path:: PathBuf {
122
- let file_path = temp_dir. path ( ) . join ( "test_data.txt" ) ;
123
- let file = File :: create ( & file_path) . unwrap ( ) ;
124
- let mut writer = BufWriter :: new ( file) ;
125
- writer. write_all ( data) . unwrap ( ) ;
126
- writer. flush ( ) . unwrap ( ) ;
127
- file_path
128
- }
129
-
130
- /// Run uutils tsort with given arguments
131
- fn run_uutils_tsort ( args : & [ & str ] ) -> i32 {
132
- use std:: process:: { Command , Stdio } ;
133
-
134
- // Use the binary instead of calling uumain directly to avoid stdout issues
135
- let output = Command :: new ( "../../../target/release/coreutils" )
136
- . args ( [ "tsort" ] . iter ( ) . chain ( args. iter ( ) ) )
137
- . stdout ( Stdio :: null ( ) )
138
- . stderr ( Stdio :: null ( ) )
139
- . status ( )
140
- . expect ( "Failed to execute tsort command" ) ;
141
-
142
- i32:: from ( !output. success ( ) )
143
- }
144
-
145
119
/// Benchmark linear chain graphs of different sizes
146
120
/// This tests the performance improvements mentioned in PR #8694
147
121
#[ divan:: bench( args = [ 1_000 , 10_000 , 100_000 , 1_000_000 ] ) ]
148
122
fn tsort_linear_chain ( bencher : Bencher , num_nodes : usize ) {
149
123
let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
150
124
let data = generate_linear_chain ( num_nodes) ;
151
- let file_path = create_test_file ( & data, & temp_dir) ;
125
+ let file_path = create_test_file ( & data, temp_dir. path ( ) ) ;
152
126
let file_path_str = file_path. to_str ( ) . unwrap ( ) ;
153
127
154
128
bencher. bench ( || {
155
- black_box ( run_uutils_tsort ( & [ file_path_str] ) ) ;
129
+ black_box ( run_util_function ( uumain , & [ file_path_str] ) ) ;
156
130
} ) ;
157
131
}
158
132
@@ -161,11 +135,11 @@ fn tsort_linear_chain(bencher: Bencher, num_nodes: usize) {
161
135
fn tsort_tree_dag ( bencher : Bencher , ( depth, branching) : ( usize , usize ) ) {
162
136
let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
163
137
let data = generate_tree_dag ( depth, branching) ;
164
- let file_path = create_test_file ( & data, & temp_dir) ;
138
+ let file_path = create_test_file ( & data, temp_dir. path ( ) ) ;
165
139
let file_path_str = file_path. to_str ( ) . unwrap ( ) ;
166
140
167
141
bencher. bench ( || {
168
- black_box ( run_uutils_tsort ( & [ file_path_str] ) ) ;
142
+ black_box ( run_util_function ( uumain , & [ file_path_str] ) ) ;
169
143
} ) ;
170
144
}
171
145
@@ -174,11 +148,11 @@ fn tsort_tree_dag(bencher: Bencher, (depth, branching): (usize, usize)) {
174
148
fn tsort_complex_dag ( bencher : Bencher , num_nodes : usize ) {
175
149
let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
176
150
let data = generate_complex_dag ( num_nodes) ;
177
- let file_path = create_test_file ( & data, & temp_dir) ;
151
+ let file_path = create_test_file ( & data, temp_dir. path ( ) ) ;
178
152
let file_path_str = file_path. to_str ( ) . unwrap ( ) ;
179
153
180
154
bencher. bench ( || {
181
- black_box ( run_uutils_tsort ( & [ file_path_str] ) ) ;
155
+ black_box ( run_util_function ( uumain , & [ file_path_str] ) ) ;
182
156
} ) ;
183
157
}
184
158
@@ -188,11 +162,11 @@ fn tsort_complex_dag(bencher: Bencher, num_nodes: usize) {
188
162
fn tsort_wide_dag ( bencher : Bencher , num_nodes : usize ) {
189
163
let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
190
164
let data = generate_wide_dag ( num_nodes) ;
191
- let file_path = create_test_file ( & data, & temp_dir) ;
165
+ let file_path = create_test_file ( & data, temp_dir. path ( ) ) ;
192
166
let file_path_str = file_path. to_str ( ) . unwrap ( ) ;
193
167
194
168
bencher. bench ( || {
195
- black_box ( run_uutils_tsort ( & [ file_path_str] ) ) ;
169
+ black_box ( run_util_function ( uumain , & [ file_path_str] ) ) ;
196
170
} ) ;
197
171
}
198
172
@@ -213,11 +187,11 @@ fn tsort_input_parsing_heavy(bencher: Bencher, num_edges: usize) {
213
187
}
214
188
}
215
189
216
- let file_path = create_test_file ( & data, & temp_dir) ;
190
+ let file_path = create_test_file ( & data, temp_dir. path ( ) ) ;
217
191
let file_path_str = file_path. to_str ( ) . unwrap ( ) ;
218
192
219
193
bencher. bench ( || {
220
- black_box ( run_uutils_tsort ( & [ file_path_str] ) ) ;
194
+ black_box ( run_util_function ( uumain , & [ file_path_str] ) ) ;
221
195
} ) ;
222
196
}
223
197
0 commit comments