Skip to content

Commit 3fe0e84

Browse files
committed
perf(net): add unix bench on unix
1 parent f97ffd9 commit 3fe0e84

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

compio/benches/net.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,50 @@ fn echo_compio_win_named_pipe(b: &mut Bencher, content: Rc<[u8; BUFFER_SIZE]>) {
223223
})
224224
}
225225

226+
#[cfg(unix)]
227+
fn echo_tokio_unix(b: &mut Bencher, content: &[u8; BUFFER_SIZE]) {
228+
use tokio::{
229+
io::{AsyncReadExt, AsyncWriteExt},
230+
net::{UnixListener, UnixStream},
231+
};
232+
233+
let runtime = tokio::runtime::Builder::new_current_thread()
234+
.enable_all()
235+
.build()
236+
.unwrap();
237+
b.to_async(&runtime).iter_custom(|iter| async move {
238+
let dir = tempfile::Builder::new()
239+
.prefix("tokio-uds")
240+
.tempdir()
241+
.unwrap();
242+
let sock_path = dir.path().join("connect.sock");
243+
let listener = UnixListener::bind(&sock_path).unwrap();
244+
245+
let start = Instant::now();
246+
for _i in 0..iter {
247+
let (mut tx, (mut rx, _)) =
248+
tokio::try_join!(UnixStream::connect(&sock_path), listener.accept()).unwrap();
249+
250+
let client = async move {
251+
let mut buffer = [0u8; BUFFER_SIZE];
252+
for _i in 0..BUFFER_COUNT {
253+
tx.write_all(content).await.unwrap();
254+
tx.read_exact(&mut buffer).await.unwrap();
255+
}
256+
};
257+
let server = async move {
258+
let mut buffer = [0u8; BUFFER_SIZE];
259+
for _i in 0..BUFFER_COUNT {
260+
rx.read_exact(&mut buffer).await.unwrap();
261+
rx.write_all(&buffer).await.unwrap();
262+
}
263+
};
264+
tokio::join!(client, server);
265+
}
266+
start.elapsed()
267+
})
268+
}
269+
226270
fn echo_compio_unix(b: &mut Bencher, content: Rc<[u8; BUFFER_SIZE]>) {
227271
use compio::{
228272
io::{AsyncReadExt, AsyncWriteExt},
@@ -268,6 +312,58 @@ fn echo_compio_unix(b: &mut Bencher, content: Rc<[u8; BUFFER_SIZE]>) {
268312
})
269313
}
270314

315+
#[cfg(target_os = "linux")]
316+
fn echo_monoio_unix(b: &mut Bencher, content: &[u8; BUFFER_SIZE]) {
317+
use monoio::{
318+
io::{AsyncReadRentExt, AsyncWriteRentExt},
319+
net::{UnixListener, UnixStream},
320+
};
321+
322+
let runtime = MonoioRuntime::new();
323+
b.to_async(&runtime).iter_custom(|iter| {
324+
let content = Box::new(*content);
325+
async move {
326+
let dir = tempfile::Builder::new()
327+
.prefix("monoio-uds")
328+
.tempdir()
329+
.unwrap();
330+
let sock_path = dir.path().join("connect.sock");
331+
let listener = UnixListener::bind(&sock_path).unwrap();
332+
333+
let start = Instant::now();
334+
for _i in 0..iter {
335+
let (mut tx, (mut rx, _)) =
336+
futures_util::try_join!(UnixStream::connect(&sock_path), listener.accept())
337+
.unwrap();
338+
339+
let client = async {
340+
let mut content = content.clone();
341+
let mut buffer = Box::new([0u8; BUFFER_SIZE]);
342+
let mut res;
343+
for _i in 0..BUFFER_COUNT {
344+
(res, content) = tx.write_all(content).await;
345+
res.unwrap();
346+
(res, buffer) = tx.read_exact(buffer).await;
347+
res.unwrap();
348+
}
349+
};
350+
let server = async move {
351+
let mut buffer = Box::new([0u8; BUFFER_SIZE]);
352+
let mut res;
353+
for _i in 0..BUFFER_COUNT {
354+
(res, buffer) = rx.read_exact(buffer).await;
355+
res.unwrap();
356+
(res, buffer) = rx.write_all(buffer).await;
357+
res.unwrap();
358+
}
359+
};
360+
futures_util::join!(client, server);
361+
}
362+
start.elapsed()
363+
}
364+
})
365+
}
366+
271367
fn echo(c: &mut Criterion) {
272368
let mut rng = thread_rng();
273369

@@ -290,7 +386,11 @@ fn echo(c: &mut Criterion) {
290386
echo_compio_win_named_pipe(b, content.clone())
291387
});
292388

389+
#[cfg(unix)]
390+
group.bench_function("tokio-unix", |b| echo_tokio_unix(b, &content));
293391
group.bench_function("compio-unix", |b| echo_compio_unix(b, content.clone()));
392+
#[cfg(target_os = "linux")]
393+
group.bench_function("monoio-unix", |b| echo_monoio_unix(b, &content));
294394

295395
group.finish();
296396
}

0 commit comments

Comments
 (0)