|
4 | 4 | // file that was distributed with this source code. |
5 | 5 |
|
6 | 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::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(¤t).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() |
24 | 44 | } |
25 | 45 |
|
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); |
33 | 52 |
|
34 | 53 | 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))); |
39 | 56 | } |
40 | 57 |
|
41 | 58 | fn main() { |
| 59 | + uucore::locale::setup_localization("df").unwrap(); |
42 | 60 | divan::main(); |
43 | 61 | } |
0 commit comments