Skip to content

Commit 6ca8148

Browse files
authored
Merge pull request #10123 from sylvestre/df-benchmark
df: add benchmarks
2 parents 6d6b050 + bbe9ffa commit 6ca8148

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

.github/workflows/benchmarks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
- { package: uu_cp }
2929
- { package: uu_cut }
3030
- { package: uu_dd }
31+
- { package: uu_df }
3132
- { package: uu_du }
3233
- { package: uu_expand }
3334
- { package: uu_fold }

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.

src/uu/df/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ thiserror = { workspace = true }
2525
fluent = { workspace = true }
2626

2727
[dev-dependencies]
28+
divan = { workspace = true }
2829
tempfile = { workspace = true }
30+
uucore = { workspace = true, features = ["benchmark"] }
2931

3032
[[bin]]
3133
name = "df"
3234
path = "src/main.rs"
35+
36+
[[bench]]
37+
name = "df_bench"
38+
harness = false

src/uu/df/benches/df_bench.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 std::env;
8+
use std::fs;
9+
use std::path::PathBuf;
10+
use tempfile::TempDir;
11+
use uu_df::uumain;
12+
use uucore::benchmark::run_util_function;
13+
14+
fn create_deep_directory(base_dir: &std::path::Path, depth: usize) -> PathBuf {
15+
let mut current = base_dir.to_path_buf();
16+
env::set_current_dir(&current).unwrap();
17+
18+
for _ in 0..depth {
19+
current = current.join("d");
20+
fs::create_dir("d").unwrap();
21+
env::set_current_dir("d").unwrap();
22+
}
23+
current
24+
}
25+
26+
#[divan::bench]
27+
fn df_deep_directory(bencher: Bencher) {
28+
const DEPTH: usize = 20000;
29+
30+
let original_dir = env::current_dir().unwrap();
31+
let temp_dir = TempDir::new().unwrap();
32+
let _deep_path = create_deep_directory(temp_dir.path(), DEPTH);
33+
bencher.bench(|| {
34+
black_box(run_util_function(uumain, &[] as &[&str]));
35+
});
36+
37+
env::set_current_dir(original_dir).unwrap();
38+
}
39+
40+
#[divan::bench]
41+
fn df_with_path(bencher: Bencher) {
42+
let temp_dir = TempDir::new().unwrap();
43+
let temp_path_str = temp_dir.path().to_str().unwrap();
44+
45+
bencher.bench(|| {
46+
black_box(run_util_function(uumain, &[temp_path_str]));
47+
});
48+
}
49+
50+
fn main() {
51+
divan::main();
52+
}

0 commit comments

Comments
 (0)