Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use criterion #5981

Merged
merged 4 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ fn many_signals(c: &mut Criterion) {
});
}

criterion_group!(signal_group, many_signals,);
criterion_group!(signal_group, many_signals);

criterion_main!(signal_group);
139 changes: 70 additions & 69 deletions benches/sync_mpsc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
use tokio::sync::mpsc;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::measurement::WallTime;
use criterion::{black_box, criterion_group, criterion_main, BenchmarkGroup, Criterion};

#[derive(Debug, Copy, Clone)]
struct Medium([usize; 64]);
impl Default for Medium {
fn default() -> Self {
Medium([0; 64])
}
}

type Medium = [usize; 64];
type Large = [Medium; 64];
#[derive(Debug, Copy, Clone)]
struct Large([Medium; 64]);
impl Default for Large {
fn default() -> Self {
Large([Medium::default(); 64])
}
}

fn rt() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_multi_thread()
Expand All @@ -12,62 +26,32 @@ fn rt() -> tokio::runtime::Runtime {
.unwrap()
}

fn create_1_medium(c: &mut Criterion) {
c.bench_function("create_1_medium", |b| {
b.iter(|| {
black_box(&mpsc::channel::<Medium>(1));
})
});
}

fn create_100_medium(c: &mut Criterion) {
c.bench_function("create_100_medium", |b| {
b.iter(|| {
black_box(&mpsc::channel::<Medium>(100));
})
});
}

fn create_100_000_medium(c: &mut Criterion) {
c.bench_function("create_100_000_medium", |b| {
fn create_medium<const SIZE: usize>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_function(SIZE.to_string(), |b| {
b.iter(|| {
black_box(&mpsc::channel::<Medium>(100_000));
black_box(&mpsc::channel::<Medium>(SIZE));
})
});
}

fn send_medium(c: &mut Criterion) {
fn send_data<T: Default, const SIZE: usize>(g: &mut BenchmarkGroup<WallTime>, prefix: &str) {
let rt = rt();

c.bench_function("send_medium", |b| {
g.bench_function(format!("{}_{}", prefix, SIZE), |b| {
b.iter(|| {
let (tx, mut rx) = mpsc::channel::<Medium>(1000);
let (tx, mut rx) = mpsc::channel::<T>(SIZE);

let _ = rt.block_on(tx.send([0; 64]));
let _ = rt.block_on(tx.send(T::default()));

rt.block_on(rx.recv()).unwrap();
})
});
}

fn send_large(c: &mut Criterion) {
fn contention_bounded(g: &mut BenchmarkGroup<WallTime>) {
let rt = rt();

c.bench_function("send_large", |b| {
b.iter(|| {
let (tx, mut rx) = mpsc::channel::<Large>(1000);

let _ = rt.block_on(tx.send([[0; 64]; 64]));

rt.block_on(rx.recv()).unwrap();
})
});
}

fn contention_bounded(c: &mut Criterion) {
let rt = rt();

c.bench_function("contention_bounded", |b| {
g.bench_function("bounded", |b| {
b.iter(|| {
rt.block_on(async move {
let (tx, mut rx) = mpsc::channel::<usize>(1_000_000);
Expand All @@ -89,10 +73,10 @@ fn contention_bounded(c: &mut Criterion) {
});
}

fn contention_bounded_full(c: &mut Criterion) {
fn contention_bounded_full(g: &mut BenchmarkGroup<WallTime>) {
let rt = rt();

c.bench_function("contention_bounded_full", |b| {
g.bench_function("bounded_full", |b| {
b.iter(|| {
rt.block_on(async move {
let (tx, mut rx) = mpsc::channel::<usize>(100);
Expand All @@ -114,10 +98,10 @@ fn contention_bounded_full(c: &mut Criterion) {
});
}

fn contention_unbounded(c: &mut Criterion) {
fn contention_unbounded(g: &mut BenchmarkGroup<WallTime>) {
let rt = rt();

c.bench_function("contention_unbounded", |b| {
g.bench_function("unbounded", |b| {
b.iter(|| {
rt.block_on(async move {
let (tx, mut rx) = mpsc::unbounded_channel::<usize>();
Expand All @@ -139,10 +123,10 @@ fn contention_unbounded(c: &mut Criterion) {
});
}

fn uncontented_bounded(c: &mut Criterion) {
fn uncontented_bounded(g: &mut BenchmarkGroup<WallTime>) {
let rt = rt();

c.bench_function("uncontented_bounded", |b| {
g.bench_function("bounded", |b| {
b.iter(|| {
rt.block_on(async move {
let (tx, mut rx) = mpsc::channel::<usize>(1_000_000);
Expand All @@ -159,10 +143,10 @@ fn uncontented_bounded(c: &mut Criterion) {
});
}

fn uncontented_unbounded(c: &mut Criterion) {
fn uncontented_unbounded(g: &mut BenchmarkGroup<WallTime>) {
let rt = rt();

c.bench_function("uncontented_unbounded", |b| {
g.bench_function("unbounded", |b| {
b.iter(|| {
rt.block_on(async move {
let (tx, mut rx) = mpsc::unbounded_channel::<usize>();
Expand All @@ -179,22 +163,39 @@ fn uncontented_unbounded(c: &mut Criterion) {
});
}

criterion_group!(
create,
create_1_medium,
create_100_medium,
create_100_000_medium
);

criterion_group!(send, send_medium, send_large);

criterion_group!(
contention,
contention_bounded,
contention_bounded_full,
contention_unbounded,
uncontented_bounded,
uncontented_unbounded
);

criterion_main!(create, send, contention);
fn group_create_medium(c: &mut Criterion) {
let mut group = c.benchmark_group("create_medium");
create_medium::<1>(&mut group);
create_medium::<100>(&mut group);
create_medium::<100_000>(&mut group);
group.finish();
}

fn group_send(c: &mut Criterion) {
let mut group = c.benchmark_group("send");
send_data::<Medium, 1000>(&mut group, "Medium");
send_data::<Large, 1000>(&mut group, "Medium");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not allowed use a same name in the one benchmark group. Do you mean "Large" here?
Wouldn't it be better for us to use lowercase words here, "medium" and "large"?

group.finish();
}

fn group_contention(c: &mut Criterion) {
let mut group = c.benchmark_group("contention");
contention_bounded(&mut group);
contention_bounded_full(&mut group);
contention_unbounded(&mut group);
group.finish();
}

fn group_uncontented(c: &mut Criterion) {
let mut group = c.benchmark_group("uncontented");
uncontented_bounded(&mut group);
uncontented_unbounded(&mut group);
group.finish();
}

criterion_group!(create, group_create_medium);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I belive it's better that the function here starts wich bench_ instead of group_(group_create_medium to bench_create_medium ). Those functions are orginized by group, but they are benchmark function.

criterion_group!(send, group_send);
criterion_group!(contention, group_contention);
criterion_group!(uncontented, group_uncontented);

criterion_main!(create, send, contention, uncontented);
49 changes: 29 additions & 20 deletions benches/sync_notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::sync::Arc;

use tokio::sync::Notify;

use criterion::{criterion_group, criterion_main, Criterion};
use criterion::measurement::WallTime;
use criterion::{criterion_group, criterion_main, BenchmarkGroup, Criterion};

fn rt() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_multi_thread()
Expand All @@ -12,7 +13,7 @@ fn rt() -> tokio::runtime::Runtime {
.unwrap()
}

fn notify_waiters<const N_WAITERS: usize>(c: &mut Criterion) {
fn notify_waiters<const N_WAITERS: usize>(g: &mut BenchmarkGroup<WallTime>) {
let rt = rt();
let notify = Arc::new(Notify::new());
let counter = Arc::new(AtomicUsize::new(0));
Expand All @@ -30,7 +31,7 @@ fn notify_waiters<const N_WAITERS: usize>(c: &mut Criterion) {
}

const N_ITERS: usize = 500;
c.bench_function("notify_waiters", |b| {
g.bench_function(N_WAITERS.to_string(), |b| {
b.iter(|| {
counter.store(0, Ordering::Relaxed);
loop {
Expand All @@ -43,7 +44,7 @@ fn notify_waiters<const N_WAITERS: usize>(c: &mut Criterion) {
});
}

fn notify_one<const N_WAITERS: usize>(c: &mut Criterion) {
fn notify_one<const N_WAITERS: usize>(g: &mut BenchmarkGroup<WallTime>) {
let rt = rt();
let notify = Arc::new(Notify::new());
let counter = Arc::new(AtomicUsize::new(0));
Expand All @@ -61,7 +62,7 @@ fn notify_one<const N_WAITERS: usize>(c: &mut Criterion) {
}

const N_ITERS: usize = 500;
c.bench_function("notify_one", |b| {
g.bench_function(N_WAITERS.to_string(), |b| {
b.iter(|| {
counter.store(0, Ordering::Relaxed);
loop {
Expand All @@ -74,22 +75,30 @@ fn notify_one<const N_WAITERS: usize>(c: &mut Criterion) {
});
}

criterion_group!(
notify_waiters_simple,
notify_waiters::<10>,
notify_waiters::<50>,
notify_waiters::<100>,
notify_waiters::<200>,
notify_waiters::<500>
);
fn group_notify_one(c: &mut Criterion) {
let mut group = c.benchmark_group("notify_one");
notify_one::<10>(&mut group);
notify_one::<50>(&mut group);
notify_one::<100>(&mut group);
notify_one::<200>(&mut group);
notify_one::<500>(&mut group);
group.finish();
}

fn group_notify_waiters(c: &mut Criterion) {
let mut group = c.benchmark_group("notify_waiters");
notify_waiters::<10>(&mut group);
notify_waiters::<50>(&mut group);
notify_waiters::<100>(&mut group);
notify_waiters::<200>(&mut group);
notify_waiters::<500>(&mut group);
group.finish();
}

criterion_group!(
notify_one_simple,
notify_one::<10>,
notify_one::<50>,
notify_one::<100>,
notify_one::<200>,
notify_one::<500>
notify_waiters_simple,
group_notify_one,
group_notify_waiters
);

criterion_main!(notify_waiters_simple, notify_one_simple);
criterion_main!(notify_waiters_simple);
Loading