Skip to content

Commit

Permalink
add benches
Browse files Browse the repository at this point in the history
  • Loading branch information
threadexio committed Dec 25, 2023
1 parent 1cda3cd commit 9ab1187
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ members = [
"channels",
"channels-packet",
"channels-serdes",
"tests-integration",
"benches",
"stress-tests",
"tests-integration",
"examples",
"examples/chat-app",
]
Expand Down
18 changes: 18 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "benches"
version = "0.0.0"
edition = "2021"
publish = false
autobenches = false

[dependencies]
channels = { workspace = true, features = ["full"] }
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
criterion = "0.5.1"

[[bench]]
name = "send"
path = "send.rs"
harness = false
29 changes: 29 additions & 0 deletions benches/send.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use criterion::{
black_box, criterion_group, criterion_main, Criterion,
};

use benches::{complex, simple, Complex, Simple};
use std::io::empty;

fn send_simple(c: &mut Criterion) {
let mut sender = channels::Sender::<Simple, _, _>::new(empty());

let data = simple();

c.bench_function("send simple", |b| {
b.iter(|| sender.send_blocking(black_box(&data)).unwrap())
});
}

fn send_complex(c: &mut Criterion) {
let mut sender = channels::Sender::<Complex, _, _>::new(empty());

let data = complex();

c.bench_function("send complex", |b| {
b.iter(|| sender.send_blocking(black_box(&data)).unwrap())
});
}

criterion_group!(benches, send_simple, send_complex);
criterion_main!(benches);
22 changes: 22 additions & 0 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};

pub type Simple = i32;

#[derive(Debug, Serialize, Deserialize)]
pub struct Complex {
pub a: i32,
pub b: usize,
pub c: String,
}

pub fn simple() -> Simple {
-42
}

pub fn complex() -> Complex {
Complex {
a: 42,
b: 0xbadbeef,
c: "This is my beautiful long test string".into(),
}
}

0 comments on commit 9ab1187

Please sign in to comment.