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//! ```sh
7//! RUSTFLAGS=--cfg=criterion cargo bench --features=fs,time,process,stdio
8//! ```
9
10#[cfg(any(
11 not(criterion),
12 not(feature = "fs"),
13 not(feature = "process"),
14 not(feature = "time"),
15 not(feature = "stdio"),
16 windows,
17 target_os = "emscripten",
18 target_os = "redox",
19 target_os = "wasi",
20))]
21fn main() {
22 unimplemented!(
23 "Add --cfg=criterion to RUSTFLAGS and enable the \"fs\", \"time\", \"process\", and \"stdio\" cargo \
24 features."
25 )
26}
27
28#[cfg(not(any(
29 not(criterion),
30 not(feature = "fs"),
31 not(feature = "process"),
32 not(feature = "time"),
33 not(feature = "stdio"),
34 windows,
35 target_os = "emscripten",
36 target_os = "redox",
37 target_os = "wasi",
38)))]
39use criterion::{criterion_group, criterion_main};
40
41#[cfg(not(any(
42 not(criterion),
43 not(feature = "fs"),
44 not(feature = "process"),
45 not(feature = "time"),
46 not(feature = "stdio"),
47 windows,
48 target_os = "emscripten",
49 target_os = "redox",
50 target_os = "wasi",
51)))]
52mod suite {
53 use criterion::Criterion;
54
55 pub(super) fn simple_statat(c: &mut Criterion) {
56 use rustix::fs::{statat, AtFlags, CWD};
57
58 c.bench_function("simple statat", |b| {
59 b.iter(|| {
60 statat(CWD, "/", AtFlags::empty()).unwrap();
61 })
62 });
63 }
64
65 pub(super) fn simple_statat_libc(c: &mut Criterion) {
66 c.bench_function("simple statat libc", |b| {
67 b.iter(|| {
68 let mut s = std::mem::MaybeUninit::<libc::stat>::uninit();
69 unsafe {
70 assert_eq!(
71 libc::fstatat(
72 libc::AT_FDCWD,
73 std::ffi::CString::new("/").unwrap().as_c_str().as_ptr() as _,
74 s.as_mut_ptr(),
75 0
76 ),
77 0
78 );
79 }
80 })
81 });
82 }
83
84 pub(super) fn simple_statat_libc_cstr(c: &mut Criterion) {
85 c.bench_function("simple statat libc cstr", |b| {
86 b.iter(|| {
87 let mut s = std::mem::MaybeUninit::<libc::stat>::uninit();
88 unsafe {
89 assert_eq!(
90 libc::fstatat(
91 libc::AT_FDCWD,
92 rustix::cstr!("/").as_ptr() as _,
93 s.as_mut_ptr(),
94 0
95 ),
96 0
97 );
98 }
99 })
100 });
101 }
102
103 pub(super) fn simple_statat_cstr(c: &mut Criterion) {
104 use rustix::fs::{statat, AtFlags, CWD};
105
106 c.bench_function("simple statat cstr", |b| {
107 b.iter(|| {
108 statat(CWD, rustix::cstr!("/"), AtFlags::empty()).unwrap();
109 })
110 });
111 }
112
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
134 #[cfg(not(target_os = "wasi"))]
135 pub(super) fn simple_clock_gettime(c: &mut Criterion) {
136 use rustix::time::{clock_gettime, ClockId};
137
138 c.bench_function("simple clock_gettime", |b| {
139 b.iter(|| {
140 let _ = clock_gettime(ClockId::Monotonic);
141 })
142 });
143 }
144
145 #[cfg(not(target_os = "wasi"))]
146 pub(super) fn simple_clock_gettime_libc(c: &mut Criterion) {
147 c.bench_function("simple clock_gettime libc", |b| {
148 b.iter(|| {
149 let mut s = std::mem::MaybeUninit::<libc::timespec>::uninit();
150 unsafe {
151 assert_eq!(
152 libc::clock_gettime(libc::CLOCK_MONOTONIC, s.as_mut_ptr()),
153 0
154 );
155 let _ = s.assume_init();
156 }
157 })
158 });
159 }
160
161 #[cfg(not(target_os = "wasi"))]
162 pub(super) fn simple_getpid(c: &mut Criterion) {
163 use rustix::process::getpid;
164
165 c.bench_function("simple getpid", |b| {
166 b.iter(|| {
167 let _ = getpid();
168 })
169 });
170 }
171
172 #[cfg(not(target_os = "wasi"))]
173 pub(super) fn simple_getpid_libc(c: &mut Criterion) {
174 c.bench_function("simple getpid libc", |b| {
175 b.iter(|| unsafe {
176 let _ = libc::getpid();
177 })
178 });
179 }
180}
181
182#[cfg(not(any(
183 not(criterion),
184 not(feature = "fs"),
185 not(feature = "process"),
186 not(feature = "time"),
187 not(feature = "stdio"),
188 windows,
189 target_os = "emscripten",
190 target_os = "redox",
191 target_os = "wasi",
192)))]
193criterion_group!(
194 benches,
195 suite::simple_statat,
196 suite::simple_statat_libc,
197 suite::simple_statat_libc_cstr,
198 suite::simple_statat_cstr,
199 suite::simple_fstat,
200 suite::simple_fstat_libc,
201 suite::simple_clock_gettime,
202 suite::simple_clock_gettime_libc,
203 suite::simple_getpid,
204 suite::simple_getpid_libc
205);
206#[cfg(not(any(
207 not(criterion),
208 not(feature = "fs"),
209 not(feature = "process"),
210 not(feature = "time"),
211 not(feature = "stdio"),
212 windows,
213 target_os = "emscripten",
214 target_os = "redox",
215 target_os = "wasi",
216)))]
217criterion_main!(benches);
218