Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mirromutth committed Dec 18, 2023
1 parent 25e1409 commit 2b9ed41
Show file tree
Hide file tree
Showing 12 changed files with 1,257 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ members = [
"latches",

# Internal
"benches",
"no-std-tests",
]
61 changes: 61 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[package]
name = "benches"
version = "0.0.0"
publish = false
edition = "2021"

[dependencies]
latches = { path = "../latches", default-features = false }
async-std = { version = "1", features = ["unstable"], optional = true }
tokio = { version = "1", features = ["time", "sync"], optional = true }

[dev-dependencies.async-std]
version = "1"
features = ["attributes", "unstable"]

[dev-dependencies.criterion]
version = "0.5"
features = ["async_tokio", "async_std", "html_reports"]

[dev-dependencies.tokio]
version = "1"
features = ["macros", "rt-multi-thread", "time", "sync"]

[features]
default = ["sync", "task", "futex"]
atomic-wait = ["latches/atomic-wait"]
futex = ["latches/futex"]
std = ["latches/std"]
sync = ["latches/sync"]
task = ["latches/task"]
comparison = ["async-std", "tokio"]

[[bench]]
name = "futex"
harness = false
required-features = ["futex"]

[[bench]]
name = "sync"
harness = false
required-features = ["sync"]

[[bench]]
name = "task"
harness = false
required-features = ["task"]

[[bench]]
name = "mutex"
harness = false
required-features = ["comparison"]

[[bench]]
name = "tokio"
harness = false
required-features = ["comparison"]

[[bench]]
name = "async_std"
harness = false
required-features = ["comparison"]
156 changes: 156 additions & 0 deletions benches/benches/async_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use std::{sync::Arc, time::Duration};

use async_std::task::spawn;
use benches::async_std::Latch;
use criterion::{criterion_group, criterion_main};

fn bench_count_down(c: &mut criterion::Criterion) {
c.benchmark_group("Comparison-AsyncStd")
.bench_function("count_down", |b| {
b.iter_batched(
|| Latch::new(16),
|l| l.count_down(),
criterion::BatchSize::SmallInput,
)
});
}

fn bench_arrive(c: &mut criterion::Criterion) {
c.benchmark_group("Comparison-AsyncStd")
.bench_function("arrive", |b| {
b.iter_batched(
|| Latch::new(16),
|l| l.arrive(2),
criterion::BatchSize::SmallInput,
)
});
}

fn bench_wait(c: &mut criterion::Criterion) {
c.benchmark_group("Comparison-AsyncStd")
.bench_function("wait", |b| {
b.to_async(criterion::async_executor::AsyncStdExecutor)
.iter_batched(
|| Latch::new(0),
|l| async move { l.wait().await },
criterion::BatchSize::SmallInput,
)
});
case_wait(c, 1);
case_wait(c, 1024);
}

fn bench_wait_timeout(c: &mut criterion::Criterion) {
const DUR: Duration = Duration::from_millis(24);

c.benchmark_group("Comparison-AsyncStd")
.bench_function("wait_timeout", |b| {
b.to_async(criterion::async_executor::AsyncStdExecutor)
.iter_batched(
|| Latch::new(0),
|l| async move { l.wait_timeout(DUR).await },
criterion::BatchSize::SmallInput,
)
});
case_wait_timeout(c, 1);
case_wait_timeout(c, 1024);
}

fn bench_gate(c: &mut criterion::Criterion) {
case_gate(c, 1);
case_gate(c, 256);
}

criterion_group!(
benches,
bench_count_down,
bench_arrive,
bench_wait,
bench_wait_timeout,
bench_gate,
);
criterion_main!(benches);

fn case_wait(c: &mut criterion::Criterion, tasks: usize) {
c.benchmark_group("Comparison-AsyncStd").bench_with_input(
criterion::BenchmarkId::new("wait-overall", tasks),
&tasks,
|b, t| {
b.to_async(criterion::async_executor::AsyncStdExecutor)
.iter_batched(
|| {
let l = Arc::new(Latch::new(*t));

for _ in 0..*t {
let l = l.clone();

spawn(async move { l.count_down() });
}

l
},
|l| async move { l.wait().await },
criterion::BatchSize::SmallInput,
)
},
);
}

fn case_wait_timeout(c: &mut criterion::Criterion, tasks: usize) {
const DUR: Duration = Duration::from_millis(24);

c.benchmark_group("Comparison-AsyncStd").bench_with_input(
criterion::BenchmarkId::new("wait_timeout-overall", tasks),
&tasks,
|b, t| {
b.to_async(criterion::async_executor::AsyncStdExecutor)
.iter_batched(
|| {
let l = Arc::new(Latch::new(*t));

for _ in 0..*t {
let l = l.clone();

spawn(async move { l.count_down() });
}

l
},
|l| async move { l.wait_timeout(DUR).await },
criterion::BatchSize::SmallInput,
)
},
);
}

fn case_gate(c: &mut criterion::Criterion, tasks: usize) {
c.benchmark_group("Comparison-AsyncStd").bench_with_input(
criterion::BenchmarkId::new("gate-overall", tasks),
&tasks,
|b, t| {
b.to_async(criterion::async_executor::AsyncStdExecutor)
.iter_batched(
|| {
let l = Arc::new(Latch::new(1));
let d = Arc::new(Latch::new(*t));

for _ in 0..*t {
let l = l.clone();
let d = d.clone();

spawn(async move {
l.wait().await;
d.count_down()
});
}

l.count_down();

d
},
|d| async move { d.wait().await },
criterion::BatchSize::SmallInput,
)
},
);
}
105 changes: 105 additions & 0 deletions benches/benches/futex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::{sync::Arc, thread};

use criterion::{criterion_group, criterion_main};
use latches::futex::Latch;

fn bench_count_down(c: &mut criterion::Criterion) {
c.benchmark_group("Futex")
.bench_function("count_down", |b| {
b.iter_batched(
|| Latch::new(16),
|l| l.count_down(),
criterion::BatchSize::SmallInput,
)
});
}

fn bench_arrive(c: &mut criterion::Criterion) {
c.benchmark_group("Futex").bench_function("arrive", |b| {
b.iter_batched(
|| Latch::new(16),
|l| l.arrive(2),
criterion::BatchSize::SmallInput,
)
});
}

fn bench_wait(c: &mut criterion::Criterion) {
c.benchmark_group("Futex").bench_function("wait", |b| {
b.iter_batched(
|| Latch::new(0),
|l| l.wait(),
criterion::BatchSize::SmallInput,
)
});
case_wait(c, 1);
case_wait(c, 32);
}

fn bench_gate(c: &mut criterion::Criterion) {
case_gate(c, 1);
case_gate(c, 16);
}

criterion_group!(
benches,
bench_count_down,
bench_arrive,
bench_wait,
bench_gate
);
criterion_main!(benches);

fn case_wait(c: &mut criterion::Criterion, threads: u32) {
c.benchmark_group("Futex").bench_with_input(
criterion::BenchmarkId::new("wait-overall", threads),
&threads,
|b, t| {
b.iter_batched(
|| {
let l = Arc::new(Latch::new(*t));

for _ in 0..*t {
let l = l.clone();
thread::spawn(move || l.count_down());
}

l
},
|l| l.wait(),
criterion::BatchSize::SmallInput,
)
},
);
}

fn case_gate(c: &mut criterion::Criterion, threads: u32) {
c.benchmark_group("Futex").bench_with_input(
criterion::BenchmarkId::new("gate-overall", threads),
&threads,
|b, t| {
b.iter_batched(
|| {
let l = Arc::new(Latch::new(1));
let d = Arc::new(Latch::new(*t));

for _ in 0..*t {
let l = l.clone();
let d = d.clone();

thread::spawn(move || {
l.wait();
d.count_down();
});
}

l.count_down();

d
},
|d| d.wait(),
criterion::BatchSize::SmallInput,
)
},
);
}
Loading

0 comments on commit 2b9ed41

Please sign in to comment.