|
| 1 | +use criterion::{ |
| 2 | + criterion_group, criterion_main, BenchmarkId, Criterion, SamplingMode, Throughput, |
| 3 | +}; |
| 4 | +use std::time::{Duration, Instant}; |
| 5 | + |
| 6 | +use futures::stream::{self, StreamExt}; |
| 7 | + |
| 8 | +#[derive(Clone)] |
| 9 | +struct Options { |
| 10 | + iterations: usize, |
| 11 | + concurrency: usize, |
| 12 | + sq_size: usize, |
| 13 | + cq_size: usize, |
| 14 | +} |
| 15 | + |
| 16 | +impl Default for Options { |
| 17 | + fn default() -> Self { |
| 18 | + Self { |
| 19 | + iterations: 100000, |
| 20 | + concurrency: 1, |
| 21 | + sq_size: 128, |
| 22 | + cq_size: 256, |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn run_no_ops(opts: &Options, count: u64) -> Duration { |
| 28 | + let mut ring_opts = tokio_uring::uring_builder(); |
| 29 | + ring_opts |
| 30 | + .setup_cqsize(opts.cq_size as _) |
| 31 | + // .setup_sqpoll(10) |
| 32 | + // .setup_sqpoll_cpu(1) |
| 33 | + ; |
| 34 | + |
| 35 | + let mut m = Duration::ZERO; |
| 36 | + |
| 37 | + // Run the required number of iterations |
| 38 | + for _ in 0..count { |
| 39 | + m += tokio_uring::builder() |
| 40 | + .entries(opts.sq_size as _) |
| 41 | + .uring_builder(&ring_opts) |
| 42 | + .start(async move { |
| 43 | + let start = Instant::now(); |
| 44 | + stream::iter(0..opts.iterations) |
| 45 | + .for_each_concurrent(Some(opts.concurrency), |_| async move { |
| 46 | + tokio_uring::no_op().await.unwrap(); |
| 47 | + }) |
| 48 | + .await; |
| 49 | + start.elapsed() |
| 50 | + }) |
| 51 | + } |
| 52 | + m |
| 53 | +} |
| 54 | + |
| 55 | +fn bench(c: &mut Criterion) { |
| 56 | + let mut group = c.benchmark_group("no_op"); |
| 57 | + let mut opts = Options::default(); |
| 58 | + for concurrency in [1, 32, 64, 256].iter() { |
| 59 | + opts.concurrency = *concurrency; |
| 60 | + |
| 61 | + // We perform long running benchmarks: this is the best mode |
| 62 | + group.sampling_mode(SamplingMode::Flat); |
| 63 | + |
| 64 | + group.throughput(Throughput::Elements(opts.iterations as u64)); |
| 65 | + group.bench_with_input( |
| 66 | + BenchmarkId::from_parameter(concurrency), |
| 67 | + &opts, |
| 68 | + |b, opts| { |
| 69 | + // Custom iterator used because we don't expose access to runtime, |
| 70 | + // which is required to do async benchmarking with criterion |
| 71 | + b.iter_custom(move |iter| run_no_ops(opts, iter)); |
| 72 | + }, |
| 73 | + ); |
| 74 | + } |
| 75 | + group.finish(); |
| 76 | +} |
| 77 | + |
| 78 | +criterion_group!(benches, bench); |
| 79 | +criterion_main!(benches); |
0 commit comments