Skip to content

Commit

Permalink
Use runtime arguments in Divan benchmarks
Browse files Browse the repository at this point in the history
This greatly reduces compile times and is not limited to arrays/slices.
  • Loading branch information
nvzqz committed Jan 21, 2024
1 parent 12bbaba commit 2f3a365
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ hyper = { version = "0.14", features = ["client", "http2", "tcp"] }
tokio = { version = "1", features = ["full", "macros"] }
reqwest = "0.11"

divan = "0.1.7"
divan = "0.1.11"

[[bench]]
name = "batch"
Expand Down
70 changes: 35 additions & 35 deletions benches/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ mod futures_unordered {

const SIZES: [usize; 3] = [16, 64, 256];

#[divan::bench(consts = SIZES)]
fn futures<const N: usize>() {
#[divan::bench(args = SIZES)]
fn futures(n: usize) {
// setup a tokio runtime for our tests
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand All @@ -32,38 +32,38 @@ mod futures_unordered {

let mut queue = FuturesUnordered::new();

let total = N * N;
for _ in 0..N {
let total = n * n;
for _ in 0..n {
queue.push(sleep())
}
for _ in N..total {
for _ in n..total {
runtime.block_on(queue.next());
queue.push(sleep())
}
for _ in 0..N {
for _ in 0..n {
runtime.block_on(queue.next());
}
}

#[divan::bench(consts = SIZES)]
fn futures_buffered<const N: usize>() {
#[divan::bench(args = SIZES)]
fn futures_buffered(n: usize) {
// setup a tokio runtime for our tests
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

let mut queue = FuturesUnorderedBounded::new(N);
let mut queue = FuturesUnorderedBounded::new(n);

let total = N * N;
for _ in 0..N {
let total = n * n;
for _ in 0..n {
queue.push(sleep())
}
for _ in N..total {
for _ in n..total {
runtime.block_on(queue.next());
queue.push(sleep())
}
for _ in 0..N {
for _ in 0..n {
runtime.block_on(queue.next());
}
}
Expand All @@ -78,29 +78,29 @@ mod buffer_unordered {

const SIZES: [usize; 3] = [16, 64, 256];

#[divan::bench(consts = SIZES)]
fn futures<const N: usize>() {
#[divan::bench(args = SIZES)]
fn futures(n: usize) {
// setup a tokio runtime for our tests
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

let total = N * N;
let mut s = stream::iter((0..total).map(|_| sleep())).buffer_unordered(N);
let total = n * n;
let mut s = stream::iter((0..total).map(|_| sleep())).buffer_unordered(n);
while runtime.block_on(s.next()).is_some() {}
}

#[divan::bench(consts = SIZES)]
fn futures_buffered<const N: usize>() {
#[divan::bench(args = SIZES)]
fn futures_buffered(n: usize) {
// setup a tokio runtime for our tests
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

let total = N * N;
let mut s = stream::iter((0..total).map(|_| sleep())).buffered_unordered(N);
let total = n * n;
let mut s = stream::iter((0..total).map(|_| sleep())).buffered_unordered(n);
while runtime.block_on(s.next()).is_some() {}
}
}
Expand All @@ -114,29 +114,29 @@ mod buffer_ordered {

const SIZES: [usize; 3] = [16, 64, 256];

#[divan::bench(consts = SIZES)]
fn futures<const N: usize>() {
#[divan::bench(args = SIZES)]
fn futures(n: usize) {
// setup a tokio runtime for our tests
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

let total = N * N;
let mut s = stream::iter((0..total).map(|_| sleep())).buffered(N);
let total = n * n;
let mut s = stream::iter((0..total).map(|_| sleep())).buffered(n);
while runtime.block_on(s.next()).is_some() {}
}

#[divan::bench(consts = SIZES)]
fn futures_buffered<const N: usize>() {
#[divan::bench(args = SIZES)]
fn futures_buffered(n: usize) {
// setup a tokio runtime for our tests
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

let total = N * N;
let mut s = stream::iter((0..total).map(|_| sleep())).buffered_ordered(N);
let total = n * n;
let mut s = stream::iter((0..total).map(|_| sleep())).buffered_ordered(n);
while runtime.block_on(s.next()).is_some() {}
}
}
Expand All @@ -147,27 +147,27 @@ mod join {

const SIZES: [usize; 4] = [16, 64, 256, 1024];

#[divan::bench(consts = SIZES)]
fn futures<const N: usize>() {
#[divan::bench(args = SIZES)]
fn futures(n: usize) {
// setup a tokio runtime for our tests
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

let futs = (0..N * 8).map(|_| sleep());
let futs = (0..n * 8).map(|_| sleep());
runtime.block_on(futures::future::join_all(futs));
}

#[divan::bench(consts = SIZES)]
fn futures_buffered<const N: usize>() {
#[divan::bench(args = SIZES)]
fn futures_buffered(n: usize) {
// setup a tokio runtime for our tests
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

let futs = (0..N * 8).map(|_| sleep());
let futs = (0..n * 8).map(|_| sleep());
runtime.block_on(futures_buffered::join_all(futs));
}
}

0 comments on commit 2f3a365

Please sign in to comment.