NormalizedDelayPerPacketConfig

Struct NormalizedDelayPerPacketConfig 

Source
pub struct NormalizedDelayPerPacketConfig {
    pub mean: Option<Delay>,
    pub std_dev: Option<Delay>,
    pub upper_bound: Option<Delay>,
    pub lower_bound: Option<Delay>,
    pub count: usize,
    pub seed: Option<u64>,
}
Expand description

The configuration struct for NormalizedDelayPerPacket.

See NormalizedDelayPerPacket for more details.

Fields§

§mean: Option<Delay>§std_dev: Option<Delay>§upper_bound: Option<Delay>§lower_bound: Option<Delay>§count: usize§seed: Option<u64>

Implementations§

Source§

impl NormalizedDelayPerPacketConfig

Source

pub fn new() -> Self

Creates an uninitialized config

Source

pub fn mean(self, mean: Delay) -> Self

Sets the mean

If the mean is not set, 10ms will be used.

Source

pub fn std_dev(self, std_dev: Delay) -> Self

Sets the standard deviation

If the standard deviation is not set, 0ms will be used.

Source

pub fn upper_bound(self, upper_bound: Delay) -> Self

Sets the upper bound

If the upper bound is not set, the upper bound will be the one of Delay.

Source

pub fn lower_bound(self, lower_bound: Delay) -> Self

Sets the lower bound

If the lower bound is not set, 0ms will be used.

Source

pub fn count(self, count: usize) -> Self

Sets the number of packets to repeat

If the count is not set, it will be set to 0 (ie, infinite repeat).

Source

pub fn seed(self, seed: u64) -> Self

Set the seed for a random generator

If the seed is not set, 42 will be used.

Source

pub fn random_seed(self) -> Self

Allows to use a randomly generated seed

This is equivalent to: self.seed(rand::random())

Source

pub fn build(self) -> NormalizedDelayPerPacket

Creates a new NormalizedDelayPerPacket corresponding to this config.

The created model will use StdRng as source of randomness (the call is equivalent to self.build_with_rng::<StdRng>()). It should be sufficient for most cases, but StdRng is not a portable random number generator, so one may want to use a portable random number generator like ChaCha, to this end one can use build_with_rng.

Source

pub fn build_with_rng<Rng: RngCore + SeedableRng>( self, ) -> NormalizedDelayPerPacket<Rng>

Creates a new NormalizedDelayPerPacket corresponding to this config.

Unlike build, this method let you choose the random generator.

§Example

let normal_delay = NormalizedDelayPerPacketConfig::new()
    .mean(Delay::from_millis(12))
    .std_dev(Delay::from_millis(1))
    .count(3)
    .seed(42);

let mut default_build = normal_delay.clone().build();
let mut std_build = normal_delay.clone().build_with_rng::<StdRng>();
// ChaCha is deterministic and portable, unlike StdRng
let mut chacha_build = normal_delay.clone().build_with_rng::<ChaCha20Rng>();

for cha in [12044676, 11754367, 11253775] {
    let default = default_build.next_delay();
    let std = std_build.next_delay();
    let chacha = chacha_build.next_delay();

    assert!(default.is_some());
    assert_eq!(default, std);
    assert_ne!(default, chacha);
    assert_eq!(chacha, Some(Delay::from_nanos(cha)));
}

assert_eq!(default_build.next_delay(), None);
assert_eq!(std_build.next_delay(), None);
assert_eq!(chacha_build.next_delay(), None);
Source§

impl NormalizedDelayPerPacketConfig

Source

pub fn build_truncated(self) -> NormalizedDelayPerPacket

This is another implementation for converting NormalizedPerPacketConfig into NormalizedDelayPerPacket, where the impact of truncation (lower_bound and upper_bound field) on the mathematical expectation of the distribution is taking account by modifying the center of the distribution.

§Examples


let normal_delay = NormalizedDelayPerPacketConfig::new()
    .mean(Delay::from_millis(12))
    .std_dev(Delay::from_millis(12))
    .count(1_000_000)
    .seed(42);

let mut default_build = normal_delay.clone().build();
let mut truncate_build = normal_delay.clone().build_truncated();

fn avg_delay(mut model: impl DelayPerPacketTrace) -> Delay {
    let mut count = 0;
    std::iter::from_fn( move ||{
        model.next_delay()
    }).inspect(|_| count += 1).sum::<Delay>() / count
}

assert_eq!(avg_delay(default_build), Delay::from_nanos(12998335)); // significantly higher than the expected mean
assert_eq!(avg_delay(truncate_build), Delay::from_nanos(11998818));

let normal_delay = NormalizedDelayPerPacketConfig::new()
    .mean(Delay::from_millis(12))
    .std_dev(Delay::from_millis(12))
    .lower_bound(Delay::from_millis(8))
    .upper_bound(Delay::from_millis(20))
    .count(1_000_000)
    .seed(42);

let mut default_build = normal_delay.clone().build();
let mut truncate_build = normal_delay.clone().build_truncated();

assert_eq!(avg_delay(default_build),  Delay::from_nanos(13234261)); // significantly higher than the expected mean
assert_eq!(avg_delay(truncate_build), Delay::from_nanos(11999151));
Source

pub fn build_truncated_with_rng<Rng: SeedableRng + RngCore>( self, ) -> NormalizedDelayPerPacket<Rng>

Similar to build_truncated but let you choose the random generator.

See build for details about the reason for using another random number generator than StdRng.

Trait Implementations§

Source§

impl Clone for NormalizedDelayPerPacketConfig

Source§

fn clone(&self) -> NormalizedDelayPerPacketConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NormalizedDelayPerPacketConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NormalizedDelayPerPacketConfig

Source§

fn default() -> NormalizedDelayPerPacketConfig

Returns the “default value” for a type. Read more
Source§

impl DelayPerPacketTraceConfig for NormalizedDelayPerPacketConfig

Source§

impl<'de> Deserialize<'de> for NormalizedDelayPerPacketConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Forever for NormalizedDelayPerPacketConfig

Source§

impl PartialEq for NormalizedDelayPerPacketConfig

Source§

fn eq(&self, other: &NormalizedDelayPerPacketConfig) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for NormalizedDelayPerPacketConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for NormalizedDelayPerPacketConfig

Source§

impl StructuralPartialEq for NormalizedDelayPerPacketConfig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,