Skip to content

Commit

Permalink
Add benchmarks (#26)
Browse files Browse the repository at this point in the history
* Add Criterion benchmark

* Add benchmark

* Fix CI
  • Loading branch information
mre authored May 10, 2024
1 parent e00c007 commit b7469bd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
on:
- pull_request
- workflow_dispatch

name: criterion benchmark
jobs:
runBenchmark:
name: run benchmark
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: boa-dev/criterion-compare-action@v3
with:
# Use the base branch name as the branch to compare with
branchName: ${{ github.base_ref }}
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ version = "0.6.1"
edition = "2018"
repository = "https://github.com/mre/futures-batch"

[lib]
# https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options
bench = false

[dependencies]
futures = { version = "0.3", features = ["async-await"] }
pin-utils = "0.1.0"
futures-timer = "3.0.2"

[dev-dependencies]
tokio = { version = "1.22", features = ["macros", "rt-multi-thread"] }
criterion = { version = "0.4", features = ["html_reports", "async_tokio"] }

[dev-dependencies.doc-comment]
version = "0.3"

[[bench]]
name = "futures_batch_benchmark"
harness = false
25 changes: 25 additions & 0 deletions benches/futures_batch_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use futures::stream::{self, Stream};
use futures_batch::ChunksTimeoutStreamExt;
use std::time::Duration;
use tokio::runtime::Runtime;

async fn batch(stream: impl Stream<Item = i32> + Unpin) {
let _ = stream.chunks_timeout(5, Duration::new(10, 0));
}

fn criterion_benchmark(c: &mut Criterion) {
let rt = Runtime::new().unwrap();

let mut group = c.benchmark_group("futures_batch");
for &size in &[1000, 10_000, 100_000, 1_000_000, 10_000_000] {
group.throughput(Throughput::Bytes(size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
b.to_async(&rt).iter(|| batch(stream::iter(0..size)));
});
}
group.finish();
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit b7469bd

Please sign in to comment.