Skip to content

Commit 218a214

Browse files
authored
Add a microbenchmark for fstat. (#836)
See [this LWN article] for context. rustix uses the right syscall here. [this LWN article]https://lwn.net/Articles/944214/
1 parent 41063ba commit 218a214

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

benches/mod.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44
//! the "fs", "time", and "process" cargo features.
55
//!
66
//! ```sh
7-
//! RUSTFLAGS=--cfg=criterion cargo bench --features=fs,time,process
7+
//! RUSTFLAGS=--cfg=criterion cargo bench --features=fs,time,process,stdio
88
//! ```
99
1010
#[cfg(any(
1111
not(criterion),
1212
not(feature = "fs"),
1313
not(feature = "process"),
1414
not(feature = "time"),
15+
not(feature = "stdio"),
1516
windows,
1617
target_os = "emscripten",
1718
target_os = "redox",
1819
target_os = "wasi",
1920
))]
2021
fn main() {
2122
unimplemented!(
22-
"Add --cfg=criterion to RUSTFLAGS and enable the \"fs\", \"time\", and \"process\" cargo \
23+
"Add --cfg=criterion to RUSTFLAGS and enable the \"fs\", \"time\", \"process\", and \"stdio\" cargo \
2324
features."
2425
)
2526
}
@@ -29,6 +30,7 @@ fn main() {
2930
not(feature = "fs"),
3031
not(feature = "process"),
3132
not(feature = "time"),
33+
not(feature = "stdio"),
3234
windows,
3335
target_os = "emscripten",
3436
target_os = "redox",
@@ -41,6 +43,7 @@ use criterion::{criterion_group, criterion_main};
4143
not(feature = "fs"),
4244
not(feature = "process"),
4345
not(feature = "time"),
46+
not(feature = "stdio"),
4447
windows,
4548
target_os = "emscripten",
4649
target_os = "redox",
@@ -107,6 +110,27 @@ mod suite {
107110
});
108111
}
109112

113+
pub(super) fn simple_fstat(c: &mut Criterion) {
114+
use rustix::fs::fstat;
115+
116+
c.bench_function("simple fstat", |b| {
117+
b.iter(|| {
118+
fstat(rustix::stdio::stdin()).unwrap();
119+
})
120+
});
121+
}
122+
123+
pub(super) fn simple_fstat_libc(c: &mut Criterion) {
124+
c.bench_function("simple fstat libc", |b| {
125+
b.iter(|| {
126+
let mut s = std::mem::MaybeUninit::<libc::stat>::uninit();
127+
unsafe {
128+
assert_eq!(libc::fstat(libc::STDIN_FILENO, s.as_mut_ptr(),), 0);
129+
}
130+
})
131+
});
132+
}
133+
110134
#[cfg(not(target_os = "wasi"))]
111135
pub(super) fn simple_clock_gettime(c: &mut Criterion) {
112136
use rustix::time::{clock_gettime, ClockId};
@@ -160,6 +184,7 @@ mod suite {
160184
not(feature = "fs"),
161185
not(feature = "process"),
162186
not(feature = "time"),
187+
not(feature = "stdio"),
163188
windows,
164189
target_os = "emscripten",
165190
target_os = "redox",
@@ -171,6 +196,8 @@ criterion_group!(
171196
suite::simple_statat_libc,
172197
suite::simple_statat_libc_cstr,
173198
suite::simple_statat_cstr,
199+
suite::simple_fstat,
200+
suite::simple_fstat_libc,
174201
suite::simple_clock_gettime,
175202
suite::simple_clock_gettime_libc,
176203
suite::simple_getpid,
@@ -181,6 +208,7 @@ criterion_group!(
181208
not(feature = "fs"),
182209
not(feature = "process"),
183210
not(feature = "time"),
211+
not(feature = "stdio"),
184212
windows,
185213
target_os = "emscripten",
186214
target_os = "redox",

0 commit comments

Comments
 (0)