| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::{hint::black_box, time::Duration}; |
| 8 |
|
| 9 |
use criterion::{criterion_group, criterion_main, Criterion}; |
| 10 |
use neqo_transport::send_stream::RangeTracker; |
| 11 |
|
| 12 |
const CHUNK: u64 = 1000; |
| 13 |
const END: u64 = 100_000; |
| 14 |
fn build_coalesce(len: u64) -> RangeTracker { |
| 15 |
let mut used = RangeTracker::default(); |
| 16 |
let chunk = usize::try_from(CHUNK).expect("should fit"); |
| 17 |
used.mark_acked(0, chunk); |
| 18 |
used.mark_sent(CHUNK, usize::try_from(END).expect("should fit")); |
| 19 |
// leave a gap or it will coalesce here |
| 20 |
for i in 2..=len { |
| 21 |
// These do not get immediately coalesced when marking since they're not at the end or start |
| 22 |
used.mark_acked(i * CHUNK, chunk); |
| 23 |
} |
| 24 |
used |
| 25 |
} |
| 26 |
|
| 27 |
fn coalesce(c: &mut Criterion, count: u64) { |
| 28 |
let chunk = usize::try_from(CHUNK).expect("should fit"); |
| 29 |
c.bench_function( |
| 30 |
&format!("coalesce_acked_from_zero {count}+1 entries"), |
| 31 |
|b| { |
| 32 |
b.iter_batched_ref( |
| 33 |
|| build_coalesce(count), |
| 34 |
black_box(|used: &mut RangeTracker| { |
| 35 |
used.mark_acked(CHUNK, chunk); |
| 36 |
let tail = (count + 1) * CHUNK; |
| 37 |
used.mark_sent(tail, chunk); |
| 38 |
used.mark_acked(tail, chunk); |
| 39 |
}), |
| 40 |
criterion::BatchSize::SmallInput, |
| 41 |
); |
| 42 |
}, |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
fn benchmark_coalesce(c: &mut Criterion) { |
| 47 |
coalesce(c, 1); |
| 48 |
coalesce(c, 3); |
| 49 |
coalesce(c, 10); |
| 50 |
coalesce(c, 1000); |
| 51 |
} |
| 52 |
|
| 53 |
criterion_group! { |
| 54 |
name = benches; |
| 55 |
config = Criterion::default().warm_up_time(Duration::from_secs(5)).measurement_time(Duration::from_secs(60)); |
| 56 |
targets = benchmark_coalesce |
| 57 |
} |
| 58 |
criterion_main!(benches); |