-
-
Save Pauan/404f88d4945e14ebf7ecc509ac5a377b to your computer and use it in GitHub Desktop.
futures-signals microbenchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::Cell; | |
use std::sync::atomic::Ordering::Relaxed; | |
use std::sync::atomic::AtomicUsize; | |
use futures_signals::signal::{Mutable, SignalExt}; | |
use criterion::{black_box, criterion_group, criterion_main, Criterion, BatchSize}; | |
use criterion::async_executor::{FuturesExecutor, AsyncStdExecutor, SmolExecutor}; | |
fn criterion_benchmark(c: &mut Criterion) { | |
let mut group = c.benchmark_group("Add"); | |
group.bench_function("Signal futures", |b| { | |
b.to_async(FuturesExecutor).iter_batched( | |
|| { | |
let m = Mutable::new(black_box(5)); | |
let s = m.signal(); | |
(m, s) | |
}, | |
|(m, s)| { | |
{ | |
let mut l = m.lock_mut(); | |
*l += black_box(1); | |
} | |
s.for_each(|_value| { async {} }) | |
}, | |
BatchSize::SmallInput, | |
); | |
}); | |
group.bench_function("Signal async-std", |b| { | |
b.to_async(AsyncStdExecutor).iter_batched( | |
|| { | |
let m = Mutable::new(black_box(5)); | |
let s = m.signal(); | |
(m, s) | |
}, | |
|(m, s)| { | |
{ | |
let mut l = m.lock_mut(); | |
*l += black_box(1); | |
} | |
s.for_each(|_value| { async {} }) | |
}, | |
BatchSize::SmallInput, | |
); | |
}); | |
group.bench_function("Signal smol", |b| { | |
b.to_async(SmolExecutor).iter_batched( | |
|| { | |
let m = Mutable::new(black_box(5)); | |
let s = m.signal(); | |
(m, s) | |
}, | |
|(m, s)| { | |
{ | |
let mut l = m.lock_mut(); | |
*l += black_box(1); | |
} | |
s.for_each(|_value| { async {} }) | |
}, | |
BatchSize::SmallInput, | |
); | |
}); | |
group.bench_function("Mutable", |b| { | |
let m = Mutable::new(black_box(5)); | |
b.iter(|| { | |
let mut l = m.lock_mut(); | |
*l += black_box(1); | |
}); | |
}); | |
group.bench_function("atomic fetch", |b| { | |
let m = AtomicUsize::new(black_box(5)); | |
b.iter(|| { | |
m.fetch_add(black_box(1), Relaxed); | |
}); | |
}); | |
group.bench_function("normal", |b| { | |
let m = Cell::new(black_box(5)); | |
b.iter(|| { | |
m.set(m.get() + black_box(1)); | |
}); | |
}); | |
} | |
criterion_group!(benches, criterion_benchmark); | |
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "benchmark" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
[dev-dependencies] | |
futures-signals = "0.3.28" | |
[dev-dependencies.criterion] | |
version = "0.3" | |
features = [ | |
"async_std", | |
"async_smol", | |
"async_futures", | |
] | |
[[bench]] | |
name = "my_benchmark" | |
harness = false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment