Skip to main content

iroh_netbench/
config.rs

1//! Benchmark configuration and presets.
2
3use std::time::Duration;
4
5pub(crate) const LOADED_LATENCY_INTERVAL: Duration = Duration::from_millis(200);
6pub(crate) const THROUGHPUT_SAMPLE_INTERVAL: Duration = Duration::from_millis(250);
7pub(crate) const MAX_PROBE_RATE_PER_SECOND: u32 = 1_000;
8
9/// Parameters for one complete benchmark run.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct NetBenchConfig {
12    /// Hard wall-clock limit for the complete benchmark flow, including negotiation and cleanup.
13    pub overall_timeout: Duration,
14    /// Maximum time to wait for a direct path before measurements begin.
15    pub path_stabilization_timeout: Duration,
16    /// Idle RTT measurement duration, at least one millisecond.
17    pub latency_duration: Duration,
18    /// Delay between idle RTT probes, at least one millisecond.
19    pub latency_interval: Duration,
20    /// Datagram timeout measurement duration, at least one millisecond.
21    pub loss_duration: Duration,
22    /// Number of Datagram probes sent per second, within `1..=1000`.
23    pub loss_rate_per_second: u32,
24    /// Time after the last probe before outstanding probes time out.
25    pub probe_timeout: Duration,
26    /// Warm-up before download accounting starts.
27    pub download_warmup: Duration,
28    /// Accounted download duration, at least one millisecond.
29    pub download_duration: Duration,
30    /// Warm-up before upload accounting starts.
31    pub upload_warmup: Duration,
32    /// Accounted upload duration, at least one millisecond.
33    pub upload_duration: Duration,
34    /// Non-zero parallel unidirectional stream count per throughput direction.
35    pub parallel_streams: u16,
36    /// Non-zero payload size written at a time.
37    pub chunk_size: u32,
38}
39
40/// Parameters for a low-bandwidth latency and loss probe run.
41///
42/// Probe runs never open throughput streams and remain available when the peer denies
43/// bandwidth-saturating measurements.
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct NetBenchProbeConfig {
46    /// Hard wall-clock limit for the complete low-bandwidth probe run.
47    pub overall_timeout: Duration,
48    /// Maximum time to wait for a direct path before measurements begin.
49    pub path_stabilization_timeout: Duration,
50    /// Idle RTT measurement duration, at least one millisecond.
51    pub latency_duration: Duration,
52    /// Delay between idle RTT probes, at least one millisecond.
53    pub latency_interval: Duration,
54    /// Datagram timeout measurement duration, at least one millisecond.
55    pub loss_duration: Duration,
56    /// Number of Datagram probes sent per second, within `1..=1000`.
57    pub loss_rate_per_second: u32,
58    /// Time after the last probe before outstanding probes time out.
59    pub probe_timeout: Duration,
60}
61
62impl NetBenchConfig {
63    /// A roughly 15-second interactive preset.
64    #[must_use]
65    pub fn quick() -> Self {
66        Self {
67            overall_timeout: Duration::from_secs(45),
68            path_stabilization_timeout: Duration::from_secs(3),
69            latency_duration: Duration::from_secs(2),
70            latency_interval: Duration::from_millis(100),
71            loss_duration: Duration::from_secs(2),
72            loss_rate_per_second: 100,
73            probe_timeout: Duration::from_secs(2),
74            download_warmup: Duration::from_secs(1),
75            download_duration: Duration::from_secs(5),
76            upload_warmup: Duration::from_secs(2),
77            upload_duration: Duration::from_secs(5),
78            parallel_streams: 4,
79            chunk_size: 64 * 1024,
80        }
81    }
82
83    /// A roughly 32-second default preset.
84    #[must_use]
85    pub fn standard() -> Self {
86        Self {
87            overall_timeout: Duration::from_secs(75),
88            latency_duration: Duration::from_secs(3),
89            loss_duration: Duration::from_secs(5),
90            download_warmup: Duration::from_secs(2),
91            download_duration: Duration::from_secs(10),
92            upload_warmup: Duration::from_secs(2),
93            upload_duration: Duration::from_secs(10),
94            ..Self::quick()
95        }
96    }
97}
98
99impl Default for NetBenchConfig {
100    fn default() -> Self {
101        Self::standard()
102    }
103}
104
105impl NetBenchProbeConfig {
106    /// A short low-bandwidth diagnostic preset.
107    #[must_use]
108    pub fn quick() -> Self {
109        let full = NetBenchConfig::quick();
110        Self {
111            overall_timeout: Duration::from_secs(15),
112            ..Self::from(&full)
113        }
114    }
115
116    /// A longer low-bandwidth diagnostic preset.
117    #[must_use]
118    pub fn standard() -> Self {
119        let full = NetBenchConfig::standard();
120        Self {
121            overall_timeout: Duration::from_secs(25),
122            ..Self::from(&full)
123        }
124    }
125}
126
127impl From<&NetBenchConfig> for NetBenchProbeConfig {
128    fn from(config: &NetBenchConfig) -> Self {
129        Self {
130            overall_timeout: config.overall_timeout,
131            path_stabilization_timeout: config.path_stabilization_timeout,
132            latency_duration: config.latency_duration,
133            latency_interval: config.latency_interval,
134            loss_duration: config.loss_duration,
135            loss_rate_per_second: config.loss_rate_per_second,
136            probe_timeout: config.probe_timeout,
137        }
138    }
139}
140
141impl Default for NetBenchProbeConfig {
142    fn default() -> Self {
143        Self::standard()
144    }
145}