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