| 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 |
#![expect(clippy::unwrap_used, reason = "OK in a bench.")] |
| 8 |
|
| 9 |
use std::{hint::black_box, iter::repeat_with, time::Duration}; |
| 10 |
|
| 11 |
use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; |
| 12 |
use neqo_crypto::AuthenticationStatus; |
| 13 |
use neqo_http3::{Http3Client, Http3Parameters, Http3Server, Priority}; |
| 14 |
use neqo_transport::{ConnectionParameters, StreamType}; |
| 15 |
use test_fixture::{ |
| 16 |
fixture_init, http3_client_with_params, http3_server_with_params, now, DEFAULT_SERVER_NAME, |
| 17 |
}; |
| 18 |
|
| 19 |
const STREAM_TYPE: StreamType = StreamType::BiDi; |
| 20 |
const STREAMS_MAX: u64 = 1 << 60; |
| 21 |
|
| 22 |
fn exchange_packets(client: &mut Http3Client, server: &mut Http3Server, is_handshake: bool) { |
| 23 |
let mut out = None; |
| 24 |
let mut auth_needed = is_handshake; |
| 25 |
loop { |
| 26 |
out = client.process(out, now()).dgram(); |
| 27 |
let client_out_is_none = out.is_none(); |
| 28 |
if auth_needed && client.peer_certificate().is_some() { |
| 29 |
client.authenticated(AuthenticationStatus::Ok, now()); |
| 30 |
auth_needed = false; |
| 31 |
} |
| 32 |
out = server.process(out, now()).dgram(); |
| 33 |
if client_out_is_none && out.is_none() { |
| 34 |
break; |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
fn use_streams(client: &mut Http3Client, server: &mut Http3Server, streams: usize, data: &[u8]) { |
| 40 |
let stream_ids = repeat_with(|| { |
| 41 |
client |
| 42 |
.fetch( |
| 43 |
now(), |
| 44 |
"GET", |
| 45 |
&("https", DEFAULT_SERVER_NAME, "/"), |
| 46 |
&[], |
| 47 |
Priority::default(), |
| 48 |
) |
| 49 |
.unwrap() |
| 50 |
}) |
| 51 |
.take(streams) |
| 52 |
.collect::<Vec<_>>(); |
| 53 |
exchange_packets(client, server, false); |
| 54 |
for stream_id in &stream_ids { |
| 55 |
client.send_data(*stream_id, data).unwrap(); |
| 56 |
} |
| 57 |
exchange_packets(client, server, false); |
| 58 |
for stream_id in &stream_ids { |
| 59 |
client.stream_close_send(*stream_id).unwrap(); |
| 60 |
} |
| 61 |
exchange_packets(client, server, false); |
| 62 |
} |
| 63 |
|
| 64 |
fn connect() -> (Http3Client, Http3Server) { |
| 65 |
let cp = ConnectionParameters::default() |
| 66 |
.max_streams(STREAM_TYPE, STREAMS_MAX) |
| 67 |
.pmtud(false) |
| 68 |
.pacing(false) |
| 69 |
.mlkem(false) |
| 70 |
.sni_slicing(false); |
| 71 |
let h3p = Http3Parameters::default().connection_parameters(cp); |
| 72 |
let mut client = http3_client_with_params(h3p.clone()); |
| 73 |
let mut server = http3_server_with_params(h3p); |
| 74 |
exchange_packets(&mut client, &mut server, true); |
| 75 |
(client, server) |
| 76 |
} |
| 77 |
|
| 78 |
fn criterion_benchmark(c: &mut Criterion) { |
| 79 |
fixture_init(); |
| 80 |
|
| 81 |
for (streams, data_size) in [(1_000, 1), (1_000, 1_000)] { |
| 82 |
let mut group = c.benchmark_group(format!("{streams} streams of {data_size} bytes")); |
| 83 |
|
| 84 |
// High variance benchmark. Increase default sample size (100). |
| 85 |
group.sample_size(500); |
| 86 |
|
| 87 |
group.bench_function("multistream", |b| { |
| 88 |
let data = vec![0; data_size]; |
| 89 |
b.iter_batched_ref( |
| 90 |
connect, |
| 91 |
|_| black_box(|(client, server)| use_streams(client, server, streams, &data)), |
| 92 |
BatchSize::SmallInput, |
| 93 |
); |
| 94 |
}); |
| 95 |
group.finish(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
criterion_group! { |
| 100 |
name = benches; |
| 101 |
config = Criterion::default().warm_up_time(Duration::from_secs(5)).measurement_time(Duration::from_secs(60)); |
| 102 |
targets = criterion_benchmark |
| 103 |
} |
| 104 |
criterion_main!(benches); |