• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Benchmarks for rustix.
2 ///
3 /// To enable these benchmarks, add `--cfg=criterion` to RUSTFLAGS and enable
4 /// the "fs", "time", and "process" cargo features.
5 
6 #[cfg(any(
7     not(criterion),
8     not(feature = "fs"),
9     not(feature = "process"),
10     not(feature = "time"),
11     windows,
12     target_os = "emscripten",
13     target_os = "redox",
14     target_os = "wasi",
15 ))]
main()16 fn main() {
17     unimplemented!()
18 }
19 
20 #[cfg(not(any(
21     not(criterion),
22     not(feature = "fs"),
23     not(feature = "process"),
24     not(feature = "time"),
25     windows,
26     target_os = "emscripten",
27     target_os = "redox",
28     target_os = "wasi",
29 )))]
30 use criterion::{criterion_group, criterion_main};
31 
32 #[cfg(not(any(
33     not(criterion),
34     not(feature = "fs"),
35     not(feature = "process"),
36     not(feature = "time"),
37     windows,
38     target_os = "emscripten",
39     target_os = "redox",
40     target_os = "wasi",
41 )))]
42 mod suite {
43     use criterion::Criterion;
44 
simple_statat(c: &mut Criterion)45     pub(super) fn simple_statat(c: &mut Criterion) {
46         use rustix::fs::{cwd, statat, AtFlags};
47 
48         c.bench_function("simple statat", |b| {
49             b.iter(|| {
50                 statat(cwd(), "/", AtFlags::empty()).unwrap();
51             })
52         });
53     }
54 
simple_statat_libc(c: &mut Criterion)55     pub(super) fn simple_statat_libc(c: &mut Criterion) {
56         c.bench_function("simple statat libc", |b| {
57             b.iter(|| {
58                 let mut s = std::mem::MaybeUninit::<libc::stat>::uninit();
59                 unsafe {
60                     assert_eq!(
61                         libc::fstatat(
62                             libc::AT_FDCWD,
63                             std::ffi::CString::new("/").unwrap().as_c_str().as_ptr() as _,
64                             s.as_mut_ptr(),
65                             0
66                         ),
67                         0
68                     );
69                 }
70             })
71         });
72     }
73 
simple_statat_libc_cstr(c: &mut Criterion)74     pub(super) fn simple_statat_libc_cstr(c: &mut Criterion) {
75         c.bench_function("simple statat libc cstr", |b| {
76             b.iter(|| {
77                 let mut s = std::mem::MaybeUninit::<libc::stat>::uninit();
78                 unsafe {
79                     assert_eq!(
80                         libc::fstatat(
81                             libc::AT_FDCWD,
82                             rustix::cstr!("/").as_ptr() as _,
83                             s.as_mut_ptr(),
84                             0
85                         ),
86                         0
87                     );
88                 }
89             })
90         });
91     }
92 
simple_statat_cstr(c: &mut Criterion)93     pub(super) fn simple_statat_cstr(c: &mut Criterion) {
94         use rustix::fs::{cwd, statat, AtFlags};
95 
96         c.bench_function("simple statat cstr", |b| {
97             b.iter(|| {
98                 statat(cwd(), rustix::cstr!("/"), AtFlags::empty()).unwrap();
99             })
100         });
101     }
102 
103     #[cfg(not(target_os = "wasi"))]
simple_clock_gettime(c: &mut Criterion)104     pub(super) fn simple_clock_gettime(c: &mut Criterion) {
105         use rustix::time::{clock_gettime, ClockId};
106 
107         c.bench_function("simple clock_gettime", |b| {
108             b.iter(|| {
109                 let _ = clock_gettime(ClockId::Monotonic);
110             })
111         });
112     }
113 
114     #[cfg(not(target_os = "wasi"))]
simple_clock_gettime_libc(c: &mut Criterion)115     pub(super) fn simple_clock_gettime_libc(c: &mut Criterion) {
116         c.bench_function("simple clock_gettime libc", |b| {
117             b.iter(|| {
118                 let mut s = std::mem::MaybeUninit::<libc::timespec>::uninit();
119                 unsafe {
120                     assert_eq!(
121                         libc::clock_gettime(libc::CLOCK_MONOTONIC, s.as_mut_ptr()),
122                         0
123                     );
124                     let _ = s.assume_init();
125                 }
126             })
127         });
128     }
129 
130     #[cfg(not(target_os = "wasi"))]
simple_getpid(c: &mut Criterion)131     pub(super) fn simple_getpid(c: &mut Criterion) {
132         use rustix::process::getpid;
133 
134         c.bench_function("simple getpid", |b| {
135             b.iter(|| {
136                 let _ = getpid();
137             })
138         });
139     }
140 
141     #[cfg(not(target_os = "wasi"))]
simple_getpid_libc(c: &mut Criterion)142     pub(super) fn simple_getpid_libc(c: &mut Criterion) {
143         c.bench_function("simple getpid libc", |b| {
144             b.iter(|| unsafe {
145                 let _ = libc::getpid();
146             })
147         });
148     }
149 }
150 
151 #[cfg(not(any(
152     not(criterion),
153     not(feature = "fs"),
154     not(feature = "process"),
155     not(feature = "time"),
156     windows,
157     target_os = "emscripten",
158     target_os = "redox",
159     target_os = "wasi",
160 )))]
161 criterion_group!(
162     benches,
163     suite::simple_statat,
164     suite::simple_statat_libc,
165     suite::simple_statat_libc_cstr,
166     suite::simple_statat_cstr,
167     suite::simple_clock_gettime,
168     suite::simple_clock_gettime_libc,
169     suite::simple_getpid,
170     suite::simple_getpid_libc
171 );
172 #[cfg(not(any(
173     not(criterion),
174     not(feature = "fs"),
175     not(feature = "process"),
176     not(feature = "time"),
177     windows,
178     target_os = "emscripten",
179     target_os = "redox",
180     target_os = "wasi",
181 )))]
182 criterion_main!(benches);
183