Skip to content

Commit 84d0f10

Browse files
committed
Add benchmark of use io::stdout vs libc directly
1 parent 82877fe commit 84d0f10

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ lazy_static = "1.0"
3737
[[bench]]
3838
name = "time_format"
3939
harness = false
40+
41+
[[bench]]
42+
name = "standard_out"
43+
harness = false

benches/standard_out.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::io::{self, stdout, Write};
2+
3+
use criterion::{criterion_group, criterion_main, Criterion};
4+
5+
const MSG: &[u8] = b"2020-01-06T12:00:09.514249Z [REQUEST] simple: url = `/not_found`, method = `/not_found`, status_code = 404, body_size = 9";
6+
7+
fn via_std_lib(c: &mut Criterion) {
8+
c.bench_function("via_std_lib", |b| {
9+
b.iter(|| {
10+
stdout().write(MSG).expect("write error");
11+
})
12+
});
13+
}
14+
15+
fn via_libc_fd(c: &mut Criterion) {
16+
c.bench_function("via_libc_fd", |b| {
17+
b.iter(|| {
18+
match unsafe {
19+
libc::write(
20+
libc::STDOUT_FILENO,
21+
MSG.as_ptr() as *const libc::c_void,
22+
MSG.len(),
23+
)
24+
} {
25+
n if n < 0 => Err(io::Error::last_os_error()),
26+
n => Ok(n as usize),
27+
}
28+
.expect("write error");
29+
})
30+
});
31+
}
32+
33+
criterion_group!(standard_out, via_std_lib, via_libc_fd);
34+
criterion_main!(standard_out);

0 commit comments

Comments
 (0)