Skip to content

Commit e1d3336

Browse files
committed
perf(net): add monoio echo bench
1 parent 8d0cca3 commit e1d3336

File tree

3 files changed

+94
-39
lines changed

3 files changed

+94
-39
lines changed

compio/benches/fs.rs

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,9 @@ use tempfile::NamedTempFile;
1414
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
1515

1616
#[cfg(target_os = "linux")]
17-
struct MonoioRuntime(std::cell::RefCell<monoio::Runtime<monoio::IoUringDriver>>);
18-
17+
mod monoio_wrap;
1918
#[cfg(target_os = "linux")]
20-
impl criterion::async_executor::AsyncExecutor for MonoioRuntime {
21-
fn block_on<T>(&self, future: impl futures_util::Future<Output = T>) -> T {
22-
self.0.borrow_mut().block_on(future)
23-
}
24-
}
25-
26-
#[cfg(target_os = "linux")]
27-
impl criterion::async_executor::AsyncExecutor for &MonoioRuntime {
28-
fn block_on<T>(&self, future: impl futures_util::Future<Output = T>) -> T {
29-
self.0.borrow_mut().block_on(future)
30-
}
31-
}
19+
use monoio_wrap::MonoioRuntime;
3220

3321
criterion_group!(fs, read, write);
3422
criterion_main!(fs);
@@ -118,11 +106,7 @@ fn read_compio_join(b: &mut Bencher, (path, offsets): &(&Path, &[u64])) {
118106

119107
#[cfg(target_os = "linux")]
120108
fn read_monoio(b: &mut Bencher, (path, offsets): &(&Path, &[u64])) {
121-
let runtime = MonoioRuntime(std::cell::RefCell::new(
122-
monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
123-
.build()
124-
.unwrap(),
125-
));
109+
let runtime = MonoioRuntime::new();
126110
b.to_async(&runtime).iter_custom(|iter| async move {
127111
let file = monoio::fs::File::open(path).await.unwrap();
128112

@@ -197,11 +181,7 @@ fn read_all_compio(b: &mut Bencher, (path, len): &(&Path, u64)) {
197181

198182
#[cfg(target_os = "linux")]
199183
fn read_all_monoio(b: &mut Bencher, (path, len): &(&Path, u64)) {
200-
let runtime = MonoioRuntime(std::cell::RefCell::new(
201-
monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
202-
.build()
203-
.unwrap(),
204-
));
184+
let runtime = MonoioRuntime::new();
205185
b.to_async(&runtime).iter_custom(|iter| async move {
206186
let file = monoio::fs::File::open(path).await.unwrap();
207187
let mut buffer = Box::new([0u8; BUFFER_SIZE]);
@@ -363,11 +343,7 @@ fn write_compio_join(b: &mut Bencher, (path, offsets, content): &(&Path, &[u64],
363343

364344
#[cfg(target_os = "linux")]
365345
fn write_monoio(b: &mut Bencher, (path, offsets, content): &(&Path, &[u64], &[u8])) {
366-
let runtime = MonoioRuntime(std::cell::RefCell::new(
367-
monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
368-
.build()
369-
.unwrap(),
370-
));
346+
let runtime = MonoioRuntime::new();
371347
let content = content.to_vec();
372348
b.to_async(&runtime).iter_custom(|iter| {
373349
let mut content = content.clone();
@@ -393,11 +369,7 @@ fn write_monoio(b: &mut Bencher, (path, offsets, content): &(&Path, &[u64], &[u8
393369

394370
#[cfg(target_os = "linux")]
395371
fn write_monoio_join(b: &mut Bencher, (path, offsets, content): &(&Path, &[u64], &[u8])) {
396-
let runtime = MonoioRuntime(std::cell::RefCell::new(
397-
monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
398-
.build()
399-
.unwrap(),
400-
));
372+
let runtime = MonoioRuntime::new();
401373
let content = content.to_vec();
402374
b.to_async(&runtime).iter_custom(|iter| {
403375
let content = content.clone();
@@ -505,11 +477,7 @@ fn write_all_compio(b: &mut Bencher, (path, content): &(&Path, &[u8])) {
505477

506478
#[cfg(target_os = "linux")]
507479
fn write_all_monoio(b: &mut Bencher, (path, content): &(&Path, &[u8])) {
508-
let runtime = MonoioRuntime(std::cell::RefCell::new(
509-
monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
510-
.build()
511-
.unwrap(),
512-
));
480+
let runtime = MonoioRuntime::new();
513481
let content = content.to_vec();
514482
b.to_async(&runtime).iter_custom(|iter| {
515483
let mut content = content.clone();

compio/benches/monoio_wrap/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::{cell::RefCell, future::Future};
2+
3+
use criterion::async_executor::AsyncExecutor;
4+
5+
pub struct MonoioRuntime(RefCell<monoio::Runtime<monoio::IoUringDriver>>);
6+
7+
impl Default for MonoioRuntime {
8+
fn default() -> Self {
9+
Self::new()
10+
}
11+
}
12+
13+
impl MonoioRuntime {
14+
pub fn new() -> Self {
15+
Self(RefCell::new(
16+
monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
17+
.build()
18+
.unwrap(),
19+
))
20+
}
21+
}
22+
23+
impl AsyncExecutor for MonoioRuntime {
24+
fn block_on<T>(&self, future: impl Future<Output = T>) -> T {
25+
self.0.borrow_mut().block_on(future)
26+
}
27+
}
28+
29+
impl AsyncExecutor for &MonoioRuntime {
30+
fn block_on<T>(&self, future: impl Future<Output = T>) -> T {
31+
self.0.borrow_mut().block_on(future)
32+
}
33+
}

compio/benches/net.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use std::{net::Ipv4Addr, rc::Rc, time::Instant};
33
use criterion::{criterion_group, criterion_main, Bencher, Criterion, Throughput};
44
use rand::{thread_rng, RngCore};
55

6+
#[cfg(target_os = "linux")]
7+
mod monoio_wrap;
8+
#[cfg(target_os = "linux")]
9+
use monoio_wrap::MonoioRuntime;
10+
611
criterion_group!(net, echo);
712
criterion_main!(net);
813

@@ -90,6 +95,53 @@ fn echo_compio(b: &mut Bencher, content: Rc<[u8; BUFFER_SIZE]>) {
9095
})
9196
}
9297

98+
#[cfg(target_os = "linux")]
99+
fn echo_monoio(b: &mut Bencher, content: &[u8; BUFFER_SIZE]) {
100+
use monoio::io::{AsyncReadRentExt, AsyncWriteRentExt};
101+
102+
let runtime = MonoioRuntime::new();
103+
b.to_async(&runtime).iter_custom(|iter| {
104+
let content = Box::new(*content);
105+
async move {
106+
let listener = monoio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
107+
let addr = listener.local_addr().unwrap();
108+
109+
let start = Instant::now();
110+
for _i in 0..iter {
111+
let (mut tx, (mut rx, _)) = futures_util::try_join!(
112+
monoio::net::TcpStream::connect(addr),
113+
listener.accept()
114+
)
115+
.unwrap();
116+
117+
let client = async {
118+
let mut content = content.clone();
119+
let mut buffer = Box::new([0u8; BUFFER_SIZE]);
120+
let mut res;
121+
for _i in 0..BUFFER_COUNT {
122+
(res, content) = tx.write_all(content).await;
123+
res.unwrap();
124+
(res, buffer) = tx.read_exact(buffer).await;
125+
res.unwrap();
126+
}
127+
};
128+
let server = async move {
129+
let mut buffer = Box::new([0u8; BUFFER_SIZE]);
130+
let mut res;
131+
for _i in 0..BUFFER_COUNT {
132+
(res, buffer) = rx.read_exact(buffer).await;
133+
res.unwrap();
134+
(res, buffer) = rx.write_all(buffer).await;
135+
res.unwrap();
136+
}
137+
};
138+
futures_util::join!(client, server);
139+
}
140+
start.elapsed()
141+
}
142+
})
143+
}
144+
93145
fn echo(c: &mut Criterion) {
94146
let mut rng = thread_rng();
95147

@@ -102,6 +154,8 @@ fn echo(c: &mut Criterion) {
102154

103155
group.bench_function("tokio", |b| echo_tokio(b, &content));
104156
group.bench_function("compio", |b| echo_compio(b, content.clone()));
157+
#[cfg(target_os = "linux")]
158+
group.bench_function("monoio", |b| echo_monoio(b, &content));
105159

106160
group.finish();
107161
}

0 commit comments

Comments
 (0)