Skip to content

Commit 5287738

Browse files
authored
Merge pull request #8716 from sylvestre/refactor
bench: call the function directly to improve the profiling + move the common functions in uucore
2 parents 4a92c9b + fa865ca commit 5287738

File tree

11 files changed

+172
-155
lines changed

11 files changed

+172
-155
lines changed

.github/workflows/CICD.yml

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,6 @@ jobs:
387387
test -h /tmp/usr/local/bin/sha512sum
388388
test -h /tmp/usr/local/bin/shake128sum
389389
test -h /tmp/usr/local/bin/shake256sum
390-
391390
build_rust_stable:
392391
name: Build/stable
393392
needs: [ min_version, deps ]
@@ -1294,20 +1293,40 @@ jobs:
12941293
shell: bash
12951294
run: cargo install cargo-codspeed --locked
12961295

1296+
- name: Get benchmark list
1297+
id: benchmark_list
1298+
shell: bash
1299+
run: |
1300+
echo "Finding all utilities with benchmarks..."
1301+
benchmark_packages=""
1302+
for bench_dir in $(ls -d src/uu/*/benches 2>/dev/null); do
1303+
prog_dir=$(dirname "$bench_dir")
1304+
prog_name=$(basename "$prog_dir")
1305+
echo "Found benchmarks for uu_$prog_name"
1306+
benchmark_packages="$benchmark_packages uu_$prog_name"
1307+
done
1308+
echo "benchmark_packages=${benchmark_packages}" >> $GITHUB_OUTPUT
1309+
echo "Found benchmark packages:${benchmark_packages}"
1310+
1311+
- name: Build benchmarks
1312+
shell: bash
1313+
run: |
1314+
echo "Building benchmarks for packages: ${{ steps.benchmark_list.outputs.benchmark_packages }}"
1315+
for package in ${{ steps.benchmark_list.outputs.benchmark_packages }}; do
1316+
echo "Building benchmarks for $package"
1317+
cargo codspeed build -p $package
1318+
done
1319+
12971320
- name: Run benchmarks
12981321
uses: CodSpeedHQ/action@v4
12991322
env:
13001323
CODSPEED_LOG: debug
13011324
with:
13021325
mode: instrumentation
13031326
run: |
1304-
# Find all utilities with benchmarks and run them individually
1305-
echo "Starting CodSpeed benchmark collection..."
1306-
for bench_dir in $(ls -d src/uu/*/benches 2>/dev/null); do
1307-
prog_dir=$(dirname "$bench_dir")
1308-
prog_name=$(basename "$prog_dir")
1309-
echo "Processing benchmarks for uu_$prog_name"
1310-
cargo codspeed build -p uu_$prog_name
1311-
cargo codspeed run -p uu_$prog_name
1327+
echo "Running benchmarks for packages: ${{ steps.benchmark_list.outputs.benchmark_packages }}"
1328+
for package in ${{ steps.benchmark_list.outputs.benchmark_packages }}; do
1329+
echo "Running benchmarks for $package"
1330+
cargo codspeed run -p $package
13121331
done
13131332
token: ${{ secrets.CODSPEED_TOKEN }}

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deny.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ allow = [
2727
"CC0-1.0",
2828
"Unicode-3.0",
2929
"Zlib",
30+
"MPL-2.0",
3031
]
3132
confidence-threshold = 0.8
3233

@@ -106,6 +107,8 @@ skip = [
106107
{ name = "rand_core", version = "0.6.4" },
107108
# utmp-classic
108109
{ name = "zerocopy", version = "0.7.35" },
110+
# divans/codspeed tooling
111+
{ name = "nix", version = "0.29.0" },
109112
]
110113
# spell-checker: enable
111114

src/uu/tsort/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ path = "src/main.rs"
3030
[dev-dependencies]
3131
divan = { workspace = true }
3232
tempfile = { workspace = true }
33+
uucore = { workspace = true, features = ["benchmark"] }
3334

3435
[[bench]]
3536
name = "tsort_bench"

src/uu/tsort/benches/tsort_bench.rs

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

66
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};
109

1110
/// Generate topological sort test data with different characteristics
1211
fn generate_linear_chain(num_nodes: usize) -> Vec<u8> {
@@ -117,42 +116,17 @@ fn generate_wide_dag(num_nodes: usize) -> Vec<u8> {
117116
data
118117
}
119118

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-
145119
/// Benchmark linear chain graphs of different sizes
146120
/// This tests the performance improvements mentioned in PR #8694
147121
#[divan::bench(args = [1_000, 10_000, 100_000, 1_000_000])]
148122
fn tsort_linear_chain(bencher: Bencher, num_nodes: usize) {
149123
let temp_dir = tempfile::tempdir().unwrap();
150124
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());
152126
let file_path_str = file_path.to_str().unwrap();
153127

154128
bencher.bench(|| {
155-
black_box(run_uutils_tsort(&[file_path_str]));
129+
black_box(run_util_function(uumain, &[file_path_str]));
156130
});
157131
}
158132

@@ -161,11 +135,11 @@ fn tsort_linear_chain(bencher: Bencher, num_nodes: usize) {
161135
fn tsort_tree_dag(bencher: Bencher, (depth, branching): (usize, usize)) {
162136
let temp_dir = tempfile::tempdir().unwrap();
163137
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());
165139
let file_path_str = file_path.to_str().unwrap();
166140

167141
bencher.bench(|| {
168-
black_box(run_uutils_tsort(&[file_path_str]));
142+
black_box(run_util_function(uumain, &[file_path_str]));
169143
});
170144
}
171145

@@ -174,11 +148,11 @@ fn tsort_tree_dag(bencher: Bencher, (depth, branching): (usize, usize)) {
174148
fn tsort_complex_dag(bencher: Bencher, num_nodes: usize) {
175149
let temp_dir = tempfile::tempdir().unwrap();
176150
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());
178152
let file_path_str = file_path.to_str().unwrap();
179153

180154
bencher.bench(|| {
181-
black_box(run_uutils_tsort(&[file_path_str]));
155+
black_box(run_util_function(uumain, &[file_path_str]));
182156
});
183157
}
184158

@@ -188,11 +162,11 @@ fn tsort_complex_dag(bencher: Bencher, num_nodes: usize) {
188162
fn tsort_wide_dag(bencher: Bencher, num_nodes: usize) {
189163
let temp_dir = tempfile::tempdir().unwrap();
190164
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());
192166
let file_path_str = file_path.to_str().unwrap();
193167

194168
bencher.bench(|| {
195-
black_box(run_uutils_tsort(&[file_path_str]));
169+
black_box(run_util_function(uumain, &[file_path_str]));
196170
});
197171
}
198172

@@ -213,11 +187,11 @@ fn tsort_input_parsing_heavy(bencher: Bencher, num_edges: usize) {
213187
}
214188
}
215189

216-
let file_path = create_test_file(&data, &temp_dir);
190+
let file_path = create_test_file(&data, temp_dir.path());
217191
let file_path_str = file_path.to_str().unwrap();
218192

219193
bencher.bench(|| {
220-
black_box(run_uutils_tsort(&[file_path_str]));
194+
black_box(run_util_function(uumain, &[file_path_str]));
221195
});
222196
}
223197

src/uu/wc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ libc = { workspace = true }
3232
[dev-dependencies]
3333
divan = { workspace = true }
3434
tempfile = { workspace = true }
35+
uucore = { workspace = true, features = ["benchmark"] }
3536

3637
[[bin]]
3738
name = "wc"

0 commit comments

Comments
 (0)