1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
//! This module contains some predefined bandwidth trace models.
//!
//! Enabled with feature `bw-model` or `model`.
//!
//! ## Predefined models
//!
//! - [`StaticBw`]: A trace model with static bandwidth.
//! - [`NormalizedBw`]: A trace model whose bandwidth subjects to a normal distribution (can set upper and lower bounds).
//! - [`RepeatedBwPattern`]: A trace model with a repeated bandwidth pattern.
//!
//! ## Examples
//!
//! An example to build model from configuration:
//!
//! ```
//! # use netem_trace::model::StaticBwConfig;
//! # use netem_trace::{Bandwidth, Duration, BwTrace};
//! let mut static_bw = StaticBwConfig::new()
//! .bw(Bandwidth::from_mbps(24))
//! .duration(Duration::from_secs(1))
//! .build();
//! assert_eq!(static_bw.next_bw(), Some((Bandwidth::from_mbps(24), Duration::from_secs(1))));
//! assert_eq!(static_bw.next_bw(), None);
//! ```
//!
//! A more common use case is to build model from a configuration file (e.g. json file):
//!
//! ```
//! # use netem_trace::model::{StaticBwConfig, BwTraceConfig};
//! # use netem_trace::{Bandwidth, Duration, BwTrace};
//! # #[cfg(not(feature = "human"))]
//! 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}}";
//! // The content would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}"
//! // if the `human` feature is enabled.
//! # #[cfg(feature = "human")]
//! # let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}";
//! let des: Box<dyn BwTraceConfig> = serde_json::from_str(config_file_content).unwrap();
//! let mut model = des.into_model();
//! assert_eq!(
//! model.next_bw(),
//! Some((Bandwidth::from_mbps(12), Duration::from_secs(1)))
//! );
//! assert_eq!(
//! model.next_bw(),
//! Some((Bandwidth::from_mbps(24), Duration::from_secs(1)))
//! );
//! assert_eq!(
//! model.next_bw(),
//! Some((Bandwidth::from_mbps(12), Duration::from_secs(1)))
//! );
//! assert_eq!(
//! model.next_bw(),
//! Some((Bandwidth::from_mbps(24), Duration::from_secs(1)))
//! );
//! assert_eq!(model.next_bw(), None);
//! ```
use crate::{Bandwidth, BwTrace, Duration};
use dyn_clone::DynClone;
use rand::rngs::StdRng;
use rand::SeedableRng;
use rand_distr::{Distribution, Normal};
use std::collections::VecDeque;
const DEFAULT_RNG_SEED: u64 = 42;
/// This trait is used to convert a bandwidth trace configuration into a bandwidth trace model.
///
/// Since trace model is often configured with files and often has inner states which
/// is not suitable to be serialized/deserialized, this trait makes it possible to
/// separate the configuration part into a simple struct for serialization/deserialization, and
/// construct the model from the configuration.
#[cfg_attr(feature = "serde", typetag::serde)]
pub trait BwTraceConfig: DynClone {
fn into_model(self: Box<Self>) -> Box<dyn BwTrace>;
}
dyn_clone::clone_trait_object!(BwTraceConfig);
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// The model of a static bandwidth trace.
///
/// ## Examples
///
/// ```
/// # use netem_trace::model::StaticBwConfig;
/// # use netem_trace::{Bandwidth, Duration, BwTrace};
/// let mut static_bw = StaticBwConfig::new()
/// .bw(Bandwidth::from_mbps(24))
/// .duration(Duration::from_secs(1))
/// .build();
/// assert_eq!(static_bw.next_bw(), Some((Bandwidth::from_mbps(24), Duration::from_secs(1))));
/// assert_eq!(static_bw.next_bw(), None);
/// ```
#[derive(Debug, Clone)]
pub struct StaticBw {
pub bw: Bandwidth,
pub duration: Option<Duration>,
}
/// The configuration struct for [`StaticBw`].
///
/// See [`StaticBw`] for more details.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(default))]
#[derive(Debug, Clone, Default)]
pub struct StaticBwConfig {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub bw: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
all(feature = "serde", feature = "human"),
serde(with = "humantime_serde")
)]
pub duration: Option<Duration>,
}
/// The model of a bandwidth trace subjects to a normal distribution.
///
/// The bandwidth will subject to N(mean, std_dev), but bounded within [lower_bound, upper_bound] (optional)
///
/// ## Examples
///
/// A simple example without any bound on bandwidth:
///
/// ```
/// # use netem_trace::model::NormalizedBwConfig;
/// # use netem_trace::{Bandwidth, Duration, BwTrace};
/// let mut normal_bw = NormalizedBwConfig::new()
/// .mean(Bandwidth::from_mbps(12))
/// .std_dev(Bandwidth::from_mbps(1))
/// .duration(Duration::from_secs(1))
/// .step(Duration::from_millis(100))
/// .seed(42)
/// .build();
/// assert_eq!(normal_bw.next_bw(), Some((Bandwidth::from_bps(12069427), Duration::from_millis(100))));
/// assert_eq!(normal_bw.next_bw(), Some((Bandwidth::from_bps(12132938), Duration::from_millis(100))));
/// ```
///
/// A more complex example with bounds on bandwidth:
///
/// ```
/// # use netem_trace::model::NormalizedBwConfig;
/// # use netem_trace::{Bandwidth, Duration, BwTrace};
/// let mut normal_bw = NormalizedBwConfig::new()
/// .mean(Bandwidth::from_mbps(12))
/// .std_dev(Bandwidth::from_mbps(1))
/// .duration(Duration::from_secs(1))
/// .step(Duration::from_millis(100))
/// .seed(42)
/// .upper_bound(Bandwidth::from_kbps(12100))
/// .lower_bound(Bandwidth::from_kbps(11900))
/// .build();
/// assert_eq!(normal_bw.next_bw(), Some((Bandwidth::from_bps(12069427), Duration::from_millis(100))));
/// assert_eq!(normal_bw.next_bw(), Some((Bandwidth::from_bps(12100000), Duration::from_millis(100))));
/// ```
#[derive(Debug, Clone)]
pub struct NormalizedBw {
pub mean: Bandwidth,
pub std_dev: Bandwidth,
pub upper_bound: Option<Bandwidth>,
pub lower_bound: Option<Bandwidth>,
pub duration: Duration,
pub step: Duration,
pub seed: u64,
rng: StdRng,
normal: Normal<f64>,
}
/// The configuration struct for [`NormalizedBw`].
///
/// See [`NormalizedBw`] for more details.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(default))]
#[derive(Debug, Clone, Default)]
pub struct NormalizedBwConfig {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub mean: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub std_dev: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub upper_bound: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub lower_bound: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
all(feature = "serde", feature = "human"),
serde(with = "humantime_serde")
)]
pub duration: Option<Duration>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
all(feature = "serde", feature = "human"),
serde(with = "humantime_serde")
)]
pub step: Option<Duration>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub seed: Option<u64>,
}
/// The model of a bandwidth trace whose waveform is sawtooth.
///
/// The lowest value of the sawtooth is set by `bottom` while the highest value is set by `top`.
/// The `interval` describes how long a sawtooth lasts. The `duty_ratio` describes how much the rising time of a sawtooth
/// occupies the `interval`.
///
/// The `step` describes how long between two consecutive bandwidth samples.
///
/// The noise of the sawtooth bandwidth will subject to N(0, std_dev), but bounded within [-lower_noise_bound, upper_noise_bound] (optional)
///
/// ## Examples
///
/// A simple example without any bound on bandwidth:
///
/// ```
/// # use netem_trace::model::SawtoothBwConfig;
/// # use netem_trace::{Bandwidth, Duration, BwTrace};
/// let mut sawtooth_bw = SawtoothBwConfig::new()
/// .bottom(Bandwidth::from_mbps(12))
/// .top(Bandwidth::from_mbps(16))
/// .duration(Duration::from_secs(1))
/// .step(Duration::from_millis(100))
/// .interval(Duration::from_millis(500))
/// .duty_ratio(0.8)
/// .build();
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(12), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(13), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(14), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(15), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(16), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(12), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(13), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(14), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(15), Duration::from_millis(100)))
/// );
/// ```
///
/// A more complex example with bounds on noise:
///
/// ```
/// # use netem_trace::model::SawtoothBwConfig;
/// # use netem_trace::{Bandwidth, Duration, BwTrace};
/// let mut sawtooth_bw = SawtoothBwConfig::new()
/// .bottom(Bandwidth::from_mbps(12))
/// .top(Bandwidth::from_mbps(16))
/// .duration(Duration::from_secs(1))
/// .step(Duration::from_millis(100))
/// .interval(Duration::from_millis(500))
/// .duty_ratio(0.8)
/// .std_dev(Bandwidth::from_mbps(5))
/// .upper_noise_bound(Bandwidth::from_mbps(1))
/// .lower_noise_bound(Bandwidth::from_kbps(500))
/// .build();
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_bps(12347139), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_bps(13664690), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_mbps(15), Duration::from_millis(100)))
/// );
/// assert_eq!(
/// sawtooth_bw.next_bw(),
/// Some((Bandwidth::from_bps(14500000), Duration::from_millis(100)))
/// );
/// ```
#[derive(Debug, Clone)]
pub struct SawtoothBw {
pub bottom: Bandwidth,
pub top: Bandwidth,
pub interval: Duration,
pub duty_ratio: f64,
pub duration: Duration,
pub step: Duration,
pub seed: u64,
pub std_dev: Bandwidth,
pub upper_noise_bound: Option<Bandwidth>,
pub lower_noise_bound: Option<Bandwidth>,
current: Duration,
rng: StdRng,
noise: Normal<f64>,
}
/// The configuration struct for [`SawtoothBw`].
///
/// See [`SawtoothBw`] for more details.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(default))]
#[derive(Debug, Clone, Default)]
pub struct SawtoothBwConfig {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub bottom: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub top: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
all(feature = "serde", feature = "human"),
serde(with = "humantime_serde")
)]
pub interval: Option<Duration>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub duty_ratio: Option<f64>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
all(feature = "serde", feature = "human"),
serde(with = "humantime_serde")
)]
pub duration: Option<Duration>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
all(feature = "serde", feature = "human"),
serde(with = "humantime_serde")
)]
pub step: Option<Duration>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub seed: Option<u64>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub std_dev: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub upper_noise_bound: Option<Bandwidth>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub lower_noise_bound: Option<Bandwidth>,
}
/// The model contains an array of bandwidth trace models.
///
/// Combines multiple bandwidth trace models into one bandwidth pattern,
/// and repeat the pattern for `count` times.
///
/// ## Examples
///
/// The most common use case is to read from a configuration file and
/// deserialize it into a [`RepeatedBwPatternConfig`]:
///
/// ```
/// # use netem_trace::model::{StaticBwConfig, BwTraceConfig};
/// # use netem_trace::{Bandwidth, Duration, BwTrace};
/// # #[cfg(not(feature = "human"))]
/// 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}}";
/// // The content would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}"
/// // if the `human` feature is enabled.
/// # #[cfg(feature = "human")]
/// # let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}";
/// let des: Box<dyn BwTraceConfig> = serde_json::from_str(config_file_content).unwrap();
/// let mut model = des.into_model();
/// assert_eq!(
/// model.next_bw(),
/// Some((Bandwidth::from_mbps(12), Duration::from_secs(1)))
/// );
/// assert_eq!(
/// model.next_bw(),
/// Some((Bandwidth::from_mbps(24), Duration::from_secs(1)))
/// );
/// assert_eq!(
/// model.next_bw(),
/// Some((Bandwidth::from_mbps(12), Duration::from_secs(1)))
/// );
/// assert_eq!(
/// model.next_bw(),
/// Some((Bandwidth::from_mbps(24), Duration::from_secs(1)))
/// );
/// assert_eq!(model.next_bw(), None);
/// ```
///
/// You can also build manually:
///
/// ```
/// # use netem_trace::model::{StaticBwConfig, BwTraceConfig, RepeatedBwPatternConfig};
/// # use netem_trace::{Bandwidth, Duration, BwTrace};
/// let pat = vec![
/// Box::new(
/// StaticBwConfig::new()
/// .bw(Bandwidth::from_mbps(12))
/// .duration(Duration::from_secs(1)),
/// ) as Box<dyn BwTraceConfig>,
/// Box::new(
/// StaticBwConfig::new()
/// .bw(Bandwidth::from_mbps(24))
/// .duration(Duration::from_secs(1)),
/// ) as Box<dyn BwTraceConfig>,
/// ];
/// let ser = Box::new(RepeatedBwPatternConfig::new().pattern(pat).count(2)) as Box<dyn BwTraceConfig>;
/// let ser_str = serde_json::to_string(&ser).unwrap();
/// # #[cfg(not(feature = "human"))]
/// 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}}";
/// // 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}}"
/// // if the `human` feature is enabled.
/// # #[cfg(feature = "human")]
/// # let json_str = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}";
/// assert_eq!(ser_str, json_str);
/// ```
pub struct RepeatedBwPattern {
pub pattern: VecDeque<Box<dyn BwTrace>>,
}
/// The configuration struct for [`RepeatedBwPattern`].
///
/// See [`RepeatedBwPattern`] for more details.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(default))]
#[derive(Default, Clone)]
pub struct RepeatedBwPatternConfig {
pub pattern: Vec<Box<dyn BwTraceConfig>>,
pub count: usize,
}
impl BwTrace for StaticBw {
fn next_bw(&mut self) -> Option<(Bandwidth, Duration)> {
if let Some(duration) = self.duration.take() {
if duration.is_zero() {
None
} else {
Some((self.bw, duration))
}
} else {
None
}
}
}
impl BwTrace for NormalizedBw {
fn next_bw(&mut self) -> Option<(Bandwidth, Duration)> {
if self.duration.is_zero() {
None
} else {
let bw = self.sample() as u64;
let mut bw = Bandwidth::from_bps(bw);
if let Some(lower_bound) = self.lower_bound {
bw = bw.max(lower_bound);
}
if let Some(upper_bound) = self.upper_bound {
bw = bw.min(upper_bound);
}
let duration = self.step.min(self.duration);
self.duration -= duration;
Some((bw, duration))
}
}
}
impl BwTrace for SawtoothBw {
fn next_bw(&mut self) -> Option<(Bandwidth, Duration)> {
if self.duration.is_zero() {
None
} else {
let current = self.current.as_secs_f64();
let change_point = self.interval.as_secs_f64() * self.duty_ratio;
let base_bw = if current < change_point {
let ratio = current / change_point;
self.bottom + (self.top - self.bottom).mul_f64(ratio)
} else {
let ratio = (current - change_point) / (self.interval.as_secs_f64() - change_point);
self.top - (self.top - self.bottom).mul_f64(ratio)
};
let mut offset = self.noise.sample(&mut self.rng);
if let Some(upper_noise_bound) = self.upper_noise_bound {
offset = offset.min(upper_noise_bound.as_bps() as f64);
}
if let Some(lower_noise_bound) = self.lower_noise_bound {
offset = offset.max(-(lower_noise_bound.as_bps() as f64));
}
let bw = Bandwidth::from_bps((base_bw.as_bps() as f64 + offset) as u64);
let duration = self.step.min(self.duration);
self.duration -= duration;
self.current += duration;
if self.current >= self.interval {
self.current -= self.interval;
}
Some((bw, duration))
}
}
}
impl BwTrace for RepeatedBwPattern {
fn next_bw(&mut self) -> Option<(Bandwidth, Duration)> {
if self.pattern.is_empty() {
None
} else {
match self.pattern[0].next_bw() {
Some((bw, duration)) => Some((bw, duration)),
None => {
self.pattern.pop_front();
self.next_bw()
}
}
}
}
}
impl NormalizedBw {
pub fn sample(&mut self) -> f64 {
self.normal.sample(&mut self.rng)
}
}
impl StaticBwConfig {
pub fn new() -> Self {
Self {
bw: None,
duration: None,
}
}
pub fn bw(mut self, bw: Bandwidth) -> Self {
self.bw = Some(bw);
self
}
pub fn duration(mut self, duration: Duration) -> Self {
self.duration = Some(duration);
self
}
pub fn build(self) -> StaticBw {
StaticBw {
bw: self.bw.unwrap_or_else(|| Bandwidth::from_mbps(12)),
duration: Some(self.duration.unwrap_or_else(|| Duration::from_secs(1))),
}
}
}
/// Convert a bandwidth to bps as u64 with saturating operation.
macro_rules! saturating_bandwidth_as_bps_u64 {
($bw:expr) => {
$bw.as_gbps()
.saturating_mul(1_000_000_000)
.saturating_add($bw.subgbps_bps() as u64)
};
}
impl NormalizedBwConfig {
pub fn new() -> Self {
Self {
mean: None,
std_dev: None,
upper_bound: None,
lower_bound: None,
duration: None,
step: None,
seed: None,
}
}
pub fn mean(mut self, mean: Bandwidth) -> Self {
self.mean = Some(mean);
self
}
pub fn std_dev(mut self, std_dev: Bandwidth) -> Self {
self.std_dev = Some(std_dev);
self
}
pub fn upper_bound(mut self, upper_bound: Bandwidth) -> Self {
self.upper_bound = Some(upper_bound);
self
}
pub fn lower_bound(mut self, lower_bound: Bandwidth) -> Self {
self.lower_bound = Some(lower_bound);
self
}
pub fn duration(mut self, duration: Duration) -> Self {
self.duration = Some(duration);
self
}
pub fn step(mut self, step: Duration) -> Self {
self.step = Some(step);
self
}
pub fn seed(mut self, seed: u64) -> Self {
self.seed = Some(seed);
self
}
pub fn build(self) -> NormalizedBw {
let mean = self.mean.unwrap_or_else(|| Bandwidth::from_mbps(12));
let std_dev = self.std_dev.unwrap_or_else(|| Bandwidth::from_mbps(0));
let upper_bound = self.upper_bound;
let lower_bound = self.lower_bound;
let duration = self.duration.unwrap_or_else(|| Duration::from_secs(1));
let step = self.step.unwrap_or_else(|| Duration::from_millis(1));
let seed = self.seed.unwrap_or(DEFAULT_RNG_SEED);
let rng = StdRng::seed_from_u64(seed);
let bw_mean = saturating_bandwidth_as_bps_u64!(mean) as f64;
let bw_std_dev = saturating_bandwidth_as_bps_u64!(std_dev) as f64;
let normal: Normal<f64> = Normal::new(bw_mean, bw_std_dev).unwrap();
NormalizedBw {
mean,
std_dev,
upper_bound,
lower_bound,
duration,
step,
seed,
rng,
normal,
}
}
}
impl SawtoothBwConfig {
pub fn new() -> Self {
Self {
bottom: None,
top: None,
interval: None,
duty_ratio: None,
duration: None,
step: None,
seed: None,
std_dev: None,
upper_noise_bound: None,
lower_noise_bound: None,
}
}
pub fn bottom(mut self, bottom: Bandwidth) -> Self {
self.bottom = Some(bottom);
self
}
pub fn top(mut self, top: Bandwidth) -> Self {
self.top = Some(top);
self
}
pub fn interval(mut self, interval: Duration) -> Self {
self.interval = Some(interval);
self
}
pub fn duty_ratio(mut self, duty_ratio: f64) -> Self {
self.duty_ratio = Some(duty_ratio);
self
}
pub fn duration(mut self, duration: Duration) -> Self {
self.duration = Some(duration);
self
}
pub fn step(mut self, step: Duration) -> Self {
self.step = Some(step);
self
}
pub fn seed(mut self, seed: u64) -> Self {
self.seed = Some(seed);
self
}
pub fn std_dev(mut self, std_dev: Bandwidth) -> Self {
self.std_dev = Some(std_dev);
self
}
pub fn upper_noise_bound(mut self, upper_noise_bound: Bandwidth) -> Self {
self.upper_noise_bound = Some(upper_noise_bound);
self
}
pub fn lower_noise_bound(mut self, lower_noise_bound: Bandwidth) -> Self {
self.lower_noise_bound = Some(lower_noise_bound);
self
}
pub fn build(self) -> SawtoothBw {
let bottom = self.bottom.unwrap_or_else(|| Bandwidth::from_mbps(0));
let top = self.top.unwrap_or_else(|| Bandwidth::from_mbps(12));
if bottom > top {
panic!("SawtoothBw: bottom bw must be less than top bw");
}
let interval = self.interval.unwrap_or_else(|| Duration::from_secs(1));
let duty_ratio = self.duty_ratio.unwrap_or(0.5);
let duration = self.duration.unwrap_or_else(|| Duration::from_secs(1));
let step = self.step.unwrap_or_else(|| Duration::from_millis(1));
let seed = self.seed.unwrap_or(DEFAULT_RNG_SEED);
let rng = StdRng::seed_from_u64(seed);
let std_dev = self.std_dev.unwrap_or_else(|| Bandwidth::from_mbps(0));
let upper_noise_bound = self.upper_noise_bound;
let lower_noise_bound = self.lower_noise_bound;
let current = Duration::ZERO;
let bw_std_dev = saturating_bandwidth_as_bps_u64!(std_dev) as f64;
let noise: Normal<f64> = Normal::new(0.0, bw_std_dev).unwrap();
SawtoothBw {
bottom,
top,
interval,
duty_ratio,
duration,
step,
seed,
std_dev,
upper_noise_bound,
lower_noise_bound,
current,
rng,
noise,
}
}
}
impl RepeatedBwPatternConfig {
pub fn new() -> Self {
Self {
pattern: vec![],
count: 1,
}
}
pub fn pattern(mut self, pattern: Vec<Box<dyn BwTraceConfig>>) -> Self {
self.pattern = pattern;
self
}
pub fn count(mut self, count: usize) -> Self {
self.count = count;
self
}
pub fn build(self) -> RepeatedBwPattern {
let pattern = vec![self.pattern; self.count]
.drain(..)
.flatten()
.map(|config| config.into_model())
.collect();
RepeatedBwPattern { pattern }
}
}
macro_rules! impl_bw_trace_config {
($name:ident) => {
#[cfg_attr(feature = "serde", typetag::serde)]
impl BwTraceConfig for $name {
fn into_model(self: Box<$name>) -> Box<dyn BwTrace> {
Box::new(self.build())
}
}
};
}
impl_bw_trace_config!(StaticBwConfig);
impl_bw_trace_config!(NormalizedBwConfig);
impl_bw_trace_config!(SawtoothBwConfig);
impl_bw_trace_config!(RepeatedBwPatternConfig);