Skip to content

Commit c81ac1c

Browse files
committed
feat(model): add humanized format for bandwidth
1 parent e1496fa commit c81ac1c

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "netem-trace"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "A library for for generating network emulation trace."
@@ -23,6 +23,7 @@ rand_distr = { version = "0.4.3", optional = true }
2323
serde = { version = "1.0", features = ["derive"], optional = true }
2424
typetag = { version = "0.2.5", optional = true }
2525
humantime-serde = { version = "1.1.1", optional = true }
26+
human-bandwidth = { version = "0.1.1", optional = true }
2627

2728
[dev-dependencies]
2829
serde_json = "1.0"
@@ -35,7 +36,7 @@ delay-model = ["dep:dyn-clone"]
3536
loss-model = ["dep:dyn-clone"]
3637
serde = ["dep:serde", "dep:typetag", "bandwidth/serde"]
3738
mahimahi = ["dep:itertools"]
38-
human = ["serde", "dep:humantime-serde"]
39+
human = ["serde", "dep:humantime-serde", "dep:human-bandwidth", "human-bandwidth/serde"]
3940
full = ["model", "mahimahi", "human"]
4041

4142
[package.metadata.docs.rs]

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
//! # use netem_trace::{Bandwidth, Duration, BwTrace};
2727
//! # #[cfg(not(feature = "human"))]
2828
//! let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":{\"secs\":1,\"nanos\":0}}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":{\"secs\":1,\"nanos\":0}}}],\"count\":2}}";
29-
//! // The content would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}"
29+
//! // The content would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":\"12Mbps\",\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":\"24Mbps\",\"duration\":\"1s\"}}],\"count\":2}}"
3030
//! // if the `human` feature is enabled.
3131
//! # #[cfg(feature = "human")]
32-
//! # let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}";
32+
//! # let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":\"12Mbps\",\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":\"24Mbps\",\"duration\":\"1s\"}}],\"count\":2}}";
3333
//! let des: Box<dyn BwTraceConfig> = serde_json::from_str(config_file_content).unwrap();
3434
//! let mut model = des.into_model();
3535
//! assert_eq!(
@@ -330,7 +330,7 @@ mod test {
330330
#[cfg(not(feature = "human"))]
331331
let des_str = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":{\"secs\":1,\"nanos\":0}}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":{\"secs\":1,\"nanos\":0}}}],\"count\":2}}";
332332
#[cfg(feature = "human")]
333-
let des_str = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}";
333+
let des_str = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":\"12Mbps\",\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":\"24Mbps\",\"duration\":\"1s\"}}],\"count\":2}}";
334334
assert_eq!(ser_str, des_str);
335335
let des: Box<dyn BwTraceConfig> = serde_json::from_str(des_str).unwrap();
336336
let mut model = des.into_model();

src/model/bw.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//! // The content would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}"
3434
//! // if the `human` feature is enabled.
3535
//! # #[cfg(feature = "human")]
36-
//! # let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}";
36+
//! # let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":\"12Mbps\",\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":\"24Mbps\",\"duration\":\"1s\"}}],\"count\":2}}";
3737
//! let des: Box<dyn BwTraceConfig> = serde_json::from_str(config_file_content).unwrap();
3838
//! let mut model = des.into_model();
3939
//! assert_eq!(
@@ -105,6 +105,10 @@ pub struct StaticBw {
105105
#[derive(Debug, Clone, Default)]
106106
pub struct StaticBwConfig {
107107
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
108+
#[cfg_attr(
109+
all(feature = "serde", feature = "human"),
110+
serde(with = "human_bandwidth::serde")
111+
)]
108112
pub bw: Option<Bandwidth>,
109113
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
110114
#[cfg_attr(
@@ -173,12 +177,28 @@ pub struct NormalizedBw {
173177
#[derive(Debug, Clone, Default)]
174178
pub struct NormalizedBwConfig {
175179
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
180+
#[cfg_attr(
181+
all(feature = "serde", feature = "human"),
182+
serde(with = "human_bandwidth::serde")
183+
)]
176184
pub mean: Option<Bandwidth>,
177185
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
186+
#[cfg_attr(
187+
all(feature = "serde", feature = "human"),
188+
serde(with = "human_bandwidth::serde")
189+
)]
178190
pub std_dev: Option<Bandwidth>,
179191
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
192+
#[cfg_attr(
193+
all(feature = "serde", feature = "human"),
194+
serde(with = "human_bandwidth::serde")
195+
)]
180196
pub upper_bound: Option<Bandwidth>,
181197
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
198+
#[cfg_attr(
199+
all(feature = "serde", feature = "human"),
200+
serde(with = "human_bandwidth::serde")
201+
)]
182202
pub lower_bound: Option<Bandwidth>,
183203
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
184204
#[cfg_attr(
@@ -316,8 +336,16 @@ pub struct SawtoothBw {
316336
#[derive(Debug, Clone, Default)]
317337
pub struct SawtoothBwConfig {
318338
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
339+
#[cfg_attr(
340+
all(feature = "serde", feature = "human"),
341+
serde(with = "human_bandwidth::serde")
342+
)]
319343
pub bottom: Option<Bandwidth>,
320344
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
345+
#[cfg_attr(
346+
all(feature = "serde", feature = "human"),
347+
serde(with = "human_bandwidth::serde")
348+
)]
321349
pub top: Option<Bandwidth>,
322350
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
323351
#[cfg_attr(
@@ -342,10 +370,22 @@ pub struct SawtoothBwConfig {
342370
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
343371
pub seed: Option<u64>,
344372
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
373+
#[cfg_attr(
374+
all(feature = "serde", feature = "human"),
375+
serde(with = "human_bandwidth::serde")
376+
)]
345377
pub std_dev: Option<Bandwidth>,
346378
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
379+
#[cfg_attr(
380+
all(feature = "serde", feature = "human"),
381+
serde(with = "human_bandwidth::serde")
382+
)]
347383
pub upper_noise_bound: Option<Bandwidth>,
348384
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
385+
#[cfg_attr(
386+
all(feature = "serde", feature = "human"),
387+
serde(with = "human_bandwidth::serde")
388+
)]
349389
pub lower_noise_bound: Option<Bandwidth>,
350390
}
351391

@@ -369,7 +409,7 @@ pub struct SawtoothBwConfig {
369409
/// // The content would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}"
370410
/// // if the `human` feature is enabled.
371411
/// # #[cfg(feature = "human")]
372-
/// # let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}";
412+
/// # let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":\"12Mbps\",\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":\"24Mbps\",\"duration\":\"1s\"}}],\"count\":2}}";
373413
/// let des: Box<dyn BwTraceConfig> = serde_json::from_str(config_file_content).unwrap();
374414
/// let mut model = des.into_model();
375415
/// assert_eq!(
@@ -412,10 +452,10 @@ pub struct SawtoothBwConfig {
412452
/// let ser_str = serde_json::to_string(&ser).unwrap();
413453
/// # #[cfg(not(feature = "human"))]
414454
/// let json_str = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":{\"secs\":1,\"nanos\":0}}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":{\"secs\":1,\"nanos\":0}}}],\"count\":2}}";
415-
/// // The json string would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}"
455+
/// // The json string would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":\"12Mbps\",\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":\"24Mbps\",\"duration\":\"1s\"}}],\"count\":2}}"
416456
/// // if the `human` feature is enabled.
417457
/// # #[cfg(feature = "human")]
418-
/// # let json_str = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}";
458+
/// # let json_str = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":\"12Mbps\",\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":\"24Mbps\",\"duration\":\"1s\"}}],\"count\":2}}";
419459
/// assert_eq!(ser_str, json_str);
420460
/// ```
421461
pub struct RepeatedBwPattern {

0 commit comments

Comments
 (0)