pub struct Speed { /* private fields */ }Expand description
Structure to define a transaction flow speed
use tokio::time::Instant;
use std::time::Duration;
use prosa::event::speed::Speed;
use std::thread::sleep;
const SLEEP_DURATION: Duration = Duration::from_millis(40);
const TPS: f64 = 25.0;
let mut speed = Speed::new(5);
// Send transactions
speed.time();
speed.time();
sleep(SLEEP_DURATION);
speed.time();
sleep(SLEEP_DURATION);
speed.time();
sleep(SLEEP_DURATION);
speed.time();
sleep(SLEEP_DURATION);
// Replace the first transaction time (size > 5)
speed.time();
let mut duration = speed.get_duration(TPS); // 40 miliseconds for the duration to keep the same TPS rate
(25..=44).contains(&duration.as_millis());
sleep(SLEEP_DURATION);
let mean_duration = speed.get_mean_duration(); // 40 miliseconds between each transactions
(25..=44).contains(&mean_duration.as_millis());
let speed_tps = speed.get_speed().round(); // 25 TPS
assert_eq!(TPS, speed_tps);
duration = speed.get_duration(TPS); // 0 miliseconds for the duration to keep the same TPS rate
assert!(duration.as_millis() <= 4);Implementations§
Source§impl Speed
impl Speed
Sourcepub fn new(size_for_average: u16) -> Speed
pub fn new(size_for_average: u16) -> Speed
Create a new speed using a number of desired samples (5 minimum)
Sourcepub fn time_event(&mut self, instant: Instant)
pub fn time_event(&mut self, instant: Instant)
Add an event instant
Sourcepub fn get_last_event(&self) -> Option<&Instant>
pub fn get_last_event(&self) -> Option<&Instant>
Get the last event instant value or None if empty
Sourcepub fn get_mean_duration(&self) -> Duration
pub fn get_mean_duration(&self) -> Duration
Getter of the mean time between transaction
Sourcepub fn get_duration_overhead(
&self,
tps: f64,
overhead: Option<Duration>,
) -> Duration
pub fn get_duration_overhead( &self, tps: f64, overhead: Option<Duration>, ) -> Duration
Returns the duration to wait since the last event to achieve the target TPS (Transactions Per Second).
Includes a small overhead to ensure a conservative duration and avoid overwhelming a remote system.
If TPS is not positive or finite, returns Duration::MAX.
Returns Duration::ZERO if the calculated duration is negative.
Sourcepub fn get_duration(&self, tps: f64) -> Duration
pub fn get_duration(&self, tps: f64) -> Duration
Returns the duration to wait since the last event to achieve the target TPS (Transactions Per Second).
If TPS is not positive or finite, returns Duration::MAX.
Returns Duration::ZERO if the calculated duration is negative.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Speed
impl RefUnwindSafe for Speed
impl Send for Speed
impl Sync for Speed
impl Unpin for Speed
impl UnsafeUnpin for Speed
impl UnwindSafe for Speed
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.