Skip to content

Commit 52b5941

Browse files
committed
df: add benchmarks
1 parent 0004574 commit 52b5941

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// Save original directory
31+
let original_dir = env::current_dir().unwrap();
32+
33+
let temp_dir = TempDir::new().unwrap();
34+
// create_deep_directory changes cwd to the deepest level
35+
let _deep_path = create_deep_directory(temp_dir.path(), DEPTH);
36+
37+
// We're now in the deep directory, run the benchmark
38+
bencher.bench(|| {
39+
black_box(run_util_function(uumain, &[] as &[&str]));
40+
});
41+
42+
// Restore original directory
43+
env::set_current_dir(original_dir).unwrap();
44+
}
45+
46+
#[divan::bench]
47+
fn df_with_path(bencher: Bencher) {
48+
let temp_dir = TempDir::new().unwrap();
49+
let temp_path_str = temp_dir.path().to_str().unwrap();
50+
51+
bencher.bench(|| {
52+
black_box(run_util_function(uumain, &[temp_path_str]));
53+
});
54+
}
55+
56+
fn main() {
57+
divan::main();
58+
}

0 commit comments

Comments
 (0)