Debian Codegraph @

third_party/rust/neqo-transport/benches/min_bandwidth.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 //! A simulated transfer benchmark, asserting a minimum bandwidth.
8 //!
9 //! This is using [`test_fixture::sim`], i.e. does no I/O beyond the process
10 //! boundary and runs in simulated time. Given that [`test_fixture::sim`] is
11 //! deterministic, there is no need for multiple benchmark iterations. Still it
12 //! is a Rust benchmark instead of a unit test due to its runtime (> 10s) even
13 //! in Rust release mode.
14
15 use std::time::Duration;
16
17 use neqo_transport::{ConnectionParameters, State};
18 use test_fixture::{
19 boxed,
20 sim::{
21 connection::{ConnectionNode, ReachState, ReceiveData, SendData},
22 network::{Delay, Mtu, TailDrop},
23 Simulator,
24 },
25 };
26
27 #[expect(clippy::cast_precision_loss, reason = "OK in a bench.")]
28 pub fn main() {
29 const MIB: usize = 1_024 * 1_024;
30 const GIB: usize = 1_024 * MIB;
31
32 const MBIT: usize = 1_000 * 1_000;
33 const GBIT: usize = 1_000 * MBIT;
34
35 const TRANSFER_AMOUNT: usize = GIB;
36 const LINK_BANDWIDTH: usize = GBIT;
37 const LINK_RTT_MS: usize = 40;
38 const MINIMUM_EXPECTED_UTILIZATION: f64 = 0.85;
39
40 let gbit_link = || {
41 let rate_byte = LINK_BANDWIDTH / 8;
42 // Router buffer set to bandwidth-delay product.
43 let capacity_byte = rate_byte * LINK_RTT_MS / 1_000;
44 TailDrop::new(rate_byte, capacity_byte, Duration::ZERO)
45 };
46
47 let simulated_time = Simulator::new(
48 "gbit-bandwidth",
49 boxed![
50 ConnectionNode::new_client(
51 ConnectionParameters::default(),
52 boxed![ReachState::new(State::Confirmed)],
53 boxed![ReceiveData::new(TRANSFER_AMOUNT)]
54 ),
55 Mtu::new(1500),
56 gbit_link(),
57 Delay::new(Duration::from_millis(LINK_RTT_MS as u64 / 2)),
58 ConnectionNode::new_server(
59 ConnectionParameters::default(),
60 boxed![ReachState::new(State::Confirmed)],
61 boxed![SendData::new(TRANSFER_AMOUNT)]
62 ),
63 Mtu::new(1500),
64 gbit_link(),
65 Delay::new(Duration::from_millis(LINK_RTT_MS as u64 / 2)),
66 ],
67 )
68 .setup()
69 .run();
70
71 let achieved_bandwidth = TRANSFER_AMOUNT as f64 * 8.0 / simulated_time.as_secs_f64();
72
73 assert!(
74 LINK_BANDWIDTH as f64 * MINIMUM_EXPECTED_UTILIZATION < achieved_bandwidth,
75 "expected to reach {MINIMUM_EXPECTED_UTILIZATION} of maximum bandwidth ({} Mbit/s) but got {} Mbit/s",
76 LINK_BANDWIDTH / MBIT,
77 achieved_bandwidth / MBIT as f64,
78 );
79 }