Debian Codegraph @

third_party/rust/neqo-bin/benches/main.rs

raw
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::{env, hint::black_box, path::PathBuf, str::FromStr as _, time::Duration};
10
11 use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
12 use neqo_bin::{client, server};
13 use tokio::runtime::Builder;
14
15 struct Benchmark {
16 name: String,
17 requests: Vec<usize>,
18 upload: bool,
19 }
20
21 fn transfer(c: &mut Criterion) {
22 neqo_crypto::init_db(PathBuf::from_str("../test-fixture/db").unwrap()).unwrap();
23
24 let done_sender = spawn_server();
25 let mtu = env::var("MTU").map_or_else(|_| String::new(), |mtu| format!("/mtu-{mtu}"));
26 for Benchmark {
27 name,
28 requests,
29 upload,
30 } in [
31 Benchmark {
32 name: format!("1-conn/1-100mb-resp{mtu} (aka. Download)"),
33 requests: vec![100 * 1024 * 1024],
34 upload: false,
35 },
36 Benchmark {
37 name: format!("1-conn/10_000-parallel-1b-resp{mtu} (aka. RPS)"),
38 requests: vec![1; 10_000],
39 upload: false,
40 },
41 Benchmark {
42 name: format!("1-conn/1-1b-resp{mtu} (aka. HPS)"),
43 requests: vec![1; 1],
44 upload: false,
45 },
46 Benchmark {
47 name: format!("1-conn/1-100mb-req{mtu} (aka. Upload)"),
48 requests: vec![100 * 1024 * 1024],
49 upload: true,
50 },
51 ] {
52 let mut group = c.benchmark_group(name);
53 group.throughput(if requests[0] > 1 {
54 assert_eq!(requests.len(), 1);
55 Throughput::Bytes(requests[0] as u64)
56 } else {
57 Throughput::Elements(requests.len() as u64)
58 });
59 group.bench_function("client", |b| {
60 b.to_async(Builder::new_current_thread().enable_all().build().unwrap())
61 .iter_batched(
62 || client::client(client::Args::new(&requests, upload)),
63 |client| {
64 black_box(async move {
65 client.await.unwrap();
66 })
67 },
68 BatchSize::PerIteration,
69 );
70 });
71 group.finish();
72 }
73
74 done_sender.send(()).unwrap();
75 }
76
77 fn spawn_server() -> tokio::sync::oneshot::Sender<()> {
78 let (done_sender, mut done_receiver) = tokio::sync::oneshot::channel();
79 std::thread::spawn(move || {
80 Builder::new_current_thread()
81 .enable_all()
82 .build()
83 .unwrap()
84 .block_on(async {
85 let mut server = Box::pin(server::server(server::Args::default()));
86 tokio::select! {
87 _ = &mut done_receiver => {}
88 res = &mut server => panic!("expect server not to terminate: {res:?}"),
89 };
90 });
91 });
92 done_sender
93 }
94
95 criterion_group! {
96 name = benches;
97 config = Criterion::default().warm_up_time(Duration::from_secs(5)).measurement_time(Duration::from_secs(60));
98 targets = transfer
99 }
100 criterion_main!(benches);