Skip to content

Commit 48819c9

Browse files
committed
df: benchmark table formatting on a synthetic mount table
1 parent bf13a70 commit 48819c9

2 files changed

Lines changed: 46 additions & 29 deletions

File tree

‎src/uu/df/Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ uucore = { workspace = true, features = ["libc", "fsext", "parser-size", "fs"] }
2626
temp-env = { workspace = true }
2727
divan = { workspace = true }
2828
tempfile = { workspace = true }
29-
uucore = { workspace = true, features = ["benchmark"] }
3029

3130
[target.'cfg(unix)'.dependencies]
3231
rustix = { workspace = true, features = ["fs"] }

‎src/uu/df/benches/df_bench.rs‎

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,58 @@
44
// file that was distributed with this source code.
55

66
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::get_bench_args;
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
7+
use std::ffi::OsString;
8+
use std::io::sink;
9+
use uu_df::{Filesystem, Options, uu_app, write_table};
10+
use uucore::fsext::{FsUsage, MountInfo};
11+
12+
/// Build a fixed mount table so the measure does not depend on the mounts of
13+
/// the machine running the benchmark. Sizes span several orders of magnitude
14+
/// to exercise every human-readable suffix.
15+
fn synthetic_filesystems(count: usize) -> Vec<Filesystem> {
16+
(0..count)
17+
.map(|i| {
18+
let scale = 1u64 << (10 + (i % 5) * 6);
19+
let blocks = scale + i as u64 * 7919;
20+
Filesystem {
21+
file: None,
22+
mount_info: MountInfo {
23+
dev_id: i.to_string(),
24+
dev_name: format!("/dev/mapper/vg{}-lv{i}", i % 7),
25+
fs_type: ["ext4", "xfs", "btrfs"][i % 3].to_string(),
26+
mount_root: OsString::from("/"),
27+
mount_dir: OsString::from(format!("/srv/data/volume-{i}")),
28+
mount_option: "rw,relatime".to_string(),
29+
remote: false,
30+
dummy: false,
31+
},
32+
usage: FsUsage {
33+
blocksize: 4096,
34+
blocks,
35+
bfree: blocks / 3,
36+
bavail: blocks / 4,
37+
bavail_top_bit_set: false,
38+
files: blocks / 16,
39+
ffree: blocks / 40,
40+
},
41+
}
42+
})
43+
.collect()
2444
}
2545

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);
46+
/// Format the table of a 50000-entry mount table with each output style.
47+
#[divan::bench(args = ["-k", "-h", "-i", "--total", "--output"])]
48+
fn df_write_table(bencher: Bencher, flag: &str) {
49+
let matches = uu_app().get_matches_from(["df", flag]);
50+
let options = Options::from_matches(&matches, None).unwrap();
51+
let filesystems = synthetic_filesystems(50_000);
3352

3453
bencher
35-
.with_inputs(|| get_bench_args([] as [&str; 0]).into_iter())
36-
.bench_values(|args| black_box(uumain(args)));
37-
38-
env::set_current_dir(original_dir).unwrap();
54+
.with_inputs(|| filesystems.clone())
55+
.bench_values(|filesystems| black_box(write_table(&mut sink(), filesystems, &options)));
3956
}
4057

4158
fn main() {
59+
uucore::locale::setup_localization("df").unwrap();
4260
divan::main();
4361
}

0 commit comments

Comments
 (0)