Skip to content

Instantly share code, notes, and snippets.

@oxalica
Created October 4, 2019 09:48
Show Gist options
  • Save oxalica/c4073ecb202c599fe41b7f15f86dc79c to your computer and use it in GitHub Desktop.
Save oxalica/c4073ecb202c599fe41b7f15f86dc79c to your computer and use it in GitHub Desktop.

Revisions

  1. oxalica created this gist Oct 4, 2019.
    76 changes: 76 additions & 0 deletions my_benchmark.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    // statx-sys = "0.3.0"
    // libc = "0.2.62"
    // criterion = "0.3.0"
    //
    // Bench with file `a` but no file `b` in working dir.
    #![feature(const_cstr_unchecked)]
    use libc;
    use statx_sys;
    use std::{ffi::CStr, fs, mem};
    use criterion::*;

    type Result<T> = std::result::Result<T, libc::c_int>;

    fn stat64(path: &CStr) -> Result<libc::stat64> {
    let mut buf: libc::stat64 = unsafe { mem::zeroed() };
    let ret = unsafe { libc::stat64(path.as_ptr(), &mut buf) };
    if ret == 0 {
    Ok(buf)
    } else {
    Err(unsafe { *libc::__errno_location() })
    }
    }

    fn statx(path: &CStr) -> Result<statx_sys::statx> {
    use statx_sys::{statx, STATX_ALL};

    let mut buf: statx = unsafe { mem::zeroed() };
    let ret = unsafe { statx_sys::statx(libc::AT_FDCWD, path.as_ptr(), 0, STATX_ALL, &mut buf) };
    if ret == 0 {
    Ok(buf)
    } else {
    Err(unsafe { *libc::__errno_location() })
    }
    }

    fn statx_cvt(path: &CStr) -> Result<(libc::stat64, Option<(i64, i64)>)> {
    let s = statx(path)?;
    let mut ret: libc::stat64 = unsafe { mem::zeroed() };
    ret.st_dev = unsafe { libc::makedev(s.stx_dev_major, s.stx_dev_minor) };
    ret.st_ino = s.stx_ino;
    ret.st_nlink = s.stx_nlink as u64;
    ret.st_mode = s.stx_mode as u32;
    ret.st_uid = s.stx_uid;
    ret.st_gid = s.stx_gid;
    ret.st_rdev = unsafe { libc::makedev(s.stx_rdev_major, s.stx_rdev_minor) };
    ret.st_size = s.stx_size as i64;
    ret.st_blksize = s.stx_blksize as i64;
    ret.st_blocks = s.stx_blocks as i64;
    ret.st_atime = s.stx_atime.tv_sec;
    ret.st_atime_nsec = s.stx_atime.tv_nsec as i64;
    ret.st_mtime = s.stx_mtime.tv_sec;
    ret.st_mtime_nsec = s.stx_mtime.tv_nsec as i64;
    ret.st_ctime = s.stx_ctime.tv_sec;
    ret.st_ctime_nsec = s.stx_ctime.tv_nsec as i64;
    Ok((ret, Some((s.stx_btime.tv_sec, s.stx_btime.tv_nsec as i64))))
    }

    const F_OK: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"./a\0") };
    const F_ERR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"./b\0") };

    fn stat_bench(c: &mut Criterion) {
    c.bench_function("metadata_ok", |b| b.iter(|| fs::metadata("./a")));
    c.bench_function("metadata_err", |b| b.iter(|| fs::metadata("./b")));

    c.bench_function("stat64_ok", |b| b.iter(|| stat64(F_OK)));
    c.bench_function("stat64_err", |b| b.iter(|| stat64(F_ERR)));

    c.bench_function("statx_ok", |b| b.iter(|| statx(F_OK)));
    c.bench_function("statx_err", |b| b.iter(|| statx(F_ERR)));

    c.bench_function("statx_cvt_ok", |b| b.iter(|| statx_cvt(F_OK)));
    c.bench_function("statx_cvt_err", |b| b.iter(|| statx_cvt(F_ERR)));
    }

    criterion_group!(benches, stat_bench);
    criterion_main!(benches);
    48 changes: 48 additions & 0 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    running 0 tests

    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

    Running target/release/deps/my_benchmark-bb38aeab2c4264f8
    metadata_ok time: [529.41 ns 529.77 ns 530.19 ns]
    change: [+0.0117% +0.4098% +0.7643%] (p = 0.03 < 0.05)
    Change within noise threshold.
    Found 8 outliers among 100 measurements (8.00%)
    4 (4.00%) high mild
    4 (4.00%) high severe

    metadata_err time: [538.71 ns 539.39 ns 540.35 ns]
    change: [-0.6725% -0.3459% -0.0022%] (p = 0.04 < 0.05)
    Change within noise threshold.
    Found 11 outliers among 100 measurements (11.00%)
    5 (5.00%) high mild
    6 (6.00%) high severe

    stat64_ok time: [484.32 ns 484.53 ns 484.75 ns]
    Found 7 outliers among 100 measurements (7.00%)
    4 (4.00%) high mild
    3 (3.00%) high severe

    stat64_err time: [481.77 ns 482.00 ns 482.24 ns]
    Found 5 outliers among 100 measurements (5.00%)
    3 (3.00%) high mild
    2 (2.00%) high severe

    statx_ok time: [488.07 ns 488.35 ns 488.62 ns]
    Found 5 outliers among 100 measurements (5.00%)
    3 (3.00%) high mild
    2 (2.00%) high severe

    statx_err time: [487.74 ns 488.00 ns 488.27 ns]
    Found 5 outliers among 100 measurements (5.00%)
    2 (2.00%) high mild
    3 (3.00%) high severe

    statx_cvt_ok time: [485.05 ns 485.28 ns 485.53 ns]
    Found 4 outliers among 100 measurements (4.00%)
    2 (2.00%) high mild
    2 (2.00%) high severe

    statx_cvt_err time: [485.23 ns 485.45 ns 485.67 ns]
    Found 5 outliers among 100 measurements (5.00%)
    2 (2.00%) high mild
    3 (3.00%) high severe