Skip to content

Commit a1b4995

Browse files
authored
feat(model): add a per-packet delay trace trait and models (#21)
1 parent f1a389f commit a1b4995

8 files changed

Lines changed: 883 additions & 33 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/target
22
/Cargo.lock
3-
.vscode
3+
.vscode
4+
5+
.pre-commit-config.yaml

Cargo.toml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,47 @@ homepage = "https://github.com/stack-rs/netem-trace"
99
repository = "https://github.com/stack-rs/netem-trace"
1010
keywords = ["emulation", "trace", "network", "utility", "model"]
1111
documentation = "https://docs.rs/netem-trace"
12-
categories = [
13-
"network-programming",
14-
"config",
15-
"development-tools",
16-
"simulation",
17-
]
18-
12+
categories = ["network-programming", "config", "development-tools", "simulation"]
1913
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2014

2115
[dependencies]
2216
bandwidth = "0.3.0"
2317
dyn-clone = { version = "1.0.10", optional = true }
18+
human-bandwidth = { version = "0.1.3", optional = true }
19+
humantime-serde = { version = "1.1.1", optional = true }
2420
itertools = { version = "0.14.0", optional = true }
2521
once_cell = { version = "1.17.0", optional = true }
2622
rand = { version = "0.9.0", optional = true }
2723
rand_distr = { version = "0.5.0", optional = true }
2824
serde = { version = "1.0", features = ["derive"], optional = true }
29-
typetag = { version = "0.2.5", optional = true }
30-
humantime-serde = { version = "1.1.1", optional = true }
31-
human-bandwidth = { version = "0.1.3", optional = true }
3225
statrs = { version = "0.18.0", optional = true }
26+
typetag = { version = "0.2.5", optional = true }
3327

3428
[dev-dependencies]
35-
serde_json = "1.0"
3629
figment = { version = "0.10.19", features = ["json"] }
37-
30+
serde_json = "1.0"
3831

3932
[features]
4033
default = ["model"]
41-
model = ["bw-model", "delay-model", "loss-model", "duplicate-model"]
34+
model = [
35+
"bw-model",
36+
"delay-model",
37+
"delay-per-packet-model",
38+
"loss-model",
39+
"duplicate-model"
40+
]
4241
bw-model = ["dep:rand", "dep:rand_distr", "dep:once_cell", "dep:dyn-clone"]
4342
delay-model = ["dep:dyn-clone"]
43+
delay-per-packet-model = ["dep:dyn-clone"]
4444
loss-model = ["dep:dyn-clone"]
4545
duplicate-model = ["dep:dyn-clone"]
4646
serde = ["dep:serde", "dep:typetag", "bandwidth/serde"]
4747
mahimahi = ["dep:itertools"]
4848
human = [
49-
"serde",
50-
"dep:humantime-serde",
51-
"dep:human-bandwidth",
52-
"human-bandwidth/serde",
49+
"serde",
50+
"dep:humantime-serde",
51+
"dep:human-bandwidth",
52+
"human-bandwidth/serde"
5353
]
5454
full = ["model", "mahimahi", "human", "truncated-normal"]
5555
truncated-normal = ["statrs"]

src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! If you want to use the pre-defined models, please enable the `model` or `bw-model` feature.
66
//!
77
//! And if you want read configuration from file, `serde` feature should also be enabled.
8-
//! We alse recommend you to enable `human` feature to make the configuration file more human-readable.
8+
//! We else recommend you to enable `human` feature to make the configuration file more human-readable.
99
//!
1010
//! An example to build model from configuration:
1111
//!
@@ -158,7 +158,7 @@ pub type LossPattern = Vec<f64>;
158158
/// the packet 101 is not duplicated, then the probability of packet 102 being duplicated is still 0.8.
159159
///
160160
/// If both packet 101 and 102 were duplicated, then the probability of packet 103 being duplicated
161-
/// is still 0.1, and as long as the packets were duplicated, the propability of the next packet
161+
/// is still 0.1, and as long as the packets were duplicated, the probability of the next packet
162162
/// being duplicated is always the last element - in this case, 0.1.
163163
pub type DuplicatePattern = Vec<f64>;
164164

@@ -192,6 +192,20 @@ pub trait DelayTrace: Send {
192192
fn next_delay(&mut self) -> Option<(Delay, Duration)>;
193193
}
194194

195+
/// This is a trait that represents a trace of per-packet delays.
196+
///
197+
/// The trace is a sequence of `delay`.
198+
/// The delay describes how long the packet is delayed when going through.
199+
///
200+
/// For example, if the sequence is [10ms, 20ms, 30ms],
201+
/// then the delay will be 10ms for the first packet, then 20ms for second, then 30ms for third.
202+
///
203+
/// The next_delay function either returns **the next delay**
204+
/// in the sequence, or **None** if the trace goes to end.
205+
pub trait DelayPerPacketTrace: Send {
206+
fn next_delay(&mut self) -> Option<Delay>;
207+
}
208+
195209
/// This is a trait that represents a trace of loss patterns.
196210
///
197211
/// The trace is a sequence of `(loss_pattern, duration)` pairs.

src/model/bw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ impl NormalizedBwConfig {
989989
impl NormalizedBwConfig {
990990
/// This is another implementation for converting NormalizedBwConfig into NormalizedBw, where the impact
991991
/// of truncation (`lower_bound` and `upper_bound` field) on the mathematical expectation of the distribution
992-
/// is taking account by modifing the center of the distribution.
992+
/// is taking account by modifying the center of the distribution.
993993
///
994994
/// ## Examples
995995
///

src/model/delay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl DelayTrace for RepeatedDelayPattern {
222222
self.current_model = Some(self.pattern[self.current_pattern].clone().into_model());
223223
}
224224
match self.current_model.as_mut().unwrap().next_delay() {
225-
Some(bw) => Some(bw),
225+
Some(delay) => Some(delay),
226226
None => {
227227
self.current_model = None;
228228
self.current_pattern += 1;

0 commit comments

Comments
 (0)