Skip to content

Commit

Permalink
fix (CR): Correct doc, speed processing and refactorize according to …
Browse files Browse the repository at this point in the history
…Code review

Signed-off-by: Jeremy HERGAULT <[email protected]>
  • Loading branch information
reneca committed Aug 22, 2024
1 parent 098d795 commit be5c9ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
44 changes: 22 additions & 22 deletions prosa/src/event/speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Speed {
}

impl Speed {
/// Create a new speed from number of desire sample (5 minimum)
/// Create a new speed using a number of desired samples (5 minimum)
pub fn new(size_for_average: u16) -> Speed {
let size = if size_for_average > 5 {
size_for_average as usize
Expand Down Expand Up @@ -80,33 +80,39 @@ impl Speed {
self.event_speeds.front()
}

/// Getter of the mean time between transaction
/// Accumulate all event speeds as Duration
///
/// <math><mfrac><mi><msub><mi>Σ</mi><mn>t</mn></msub></mi><mi><msub><mi>N</mi><mn>t</mn></msub></mi></mfrac> = mean</math>
pub fn get_mean_duration(&self) -> Duration {
let mut mean_duration = Duration::default();
/// <math><msub><mi>Σ</mi><mn>t</mn></msub></math>
fn accumulate_event_speeds(&self) -> Duration {
let mut duration = Duration::ZERO;
let mut instant = Instant::now();
for event_speed in &self.event_speeds {
mean_duration += instant.duration_since(*event_speed);
duration += instant.duration_since(*event_speed);
instant = *event_speed;
}

mean_duration.div_f32(self.event_speeds.capacity() as f32)
duration
}

/// Getter of the mean time between transaction
///
/// <math><mfrac><mi><msub><mi>Σ</mi><mn>t</mn></msub></mi><mi><msub><mi>N</mi><mn>t</mn></msub></mi></mfrac> = mean</math>
pub fn get_mean_duration(&self) -> Duration {
if !self.event_speeds.is_empty() {
self.accumulate_event_speeds()
.div_f32(self.event_speeds.len() as f32)
} else {
Duration::ZERO
}
}

/// Getter of the current speed of transaction flow
///
/// <math><mfrac><mi>1000 × <msub><mi>N</mi><mn>t</mn></msub></mi><mi><msub><mi>Σ</mi><mn>t</mn></msub></mi></mfrac> = TPS</math>
pub fn get_speed(&self) -> f64 {
let mut sum_duration = Duration::default();
let mut instant = Instant::now();
for event_speed in &self.event_speeds {
sum_duration += instant.duration_since(*event_speed);
instant = *event_speed;
}

let sum_duration = self.accumulate_event_speeds();
if !sum_duration.is_zero() {
(1000 * self.event_speeds.capacity()) as f64 / sum_duration.as_millis() as f64
(1000 * self.event_speeds.len()) as f64 / sum_duration.as_millis() as f64
} else {
0.0
}
Expand All @@ -119,15 +125,9 @@ impl Speed {
///
/// <math><mfrac><mi>1000 × <msub><mi>N</mi><mn>t</mn></msub></mi><mi>TPS</mi></mfrac> + overhead − <msub><mi>Σ</mi><mn>t</mn></msub> = duration</math>
pub fn get_duration_overhead(&self, tps: f64, overhead: Option<Duration>) -> Duration {
let mut sum_duration = Duration::default();
let mut instant = Instant::now();
for event_speed in &self.event_speeds {
sum_duration += instant.duration_since(*event_speed);
instant = *event_speed;
}

let duration =
Duration::from_millis(((1000 * self.event_speeds.len()) as f64 / tps) as u64);
let sum_duration = self.accumulate_event_speeds();
if let Some(overhead) = overhead {
duration
.saturating_add(overhead)
Expand Down
2 changes: 1 addition & 1 deletion prosa/src/inj/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl InjProc {
debug!(name: "resp_inj_proc", target: "prosa::inj::proc", proc_name = name, service = msg.get_service(), response = format!("{:?}", msg.get_data()));
adaptor.process_response(msg.get_data(), msg.get_service())?;

regulator.notify_receive_transaction(Duration::default());
regulator.notify_receive_transaction(msg.elapsed());

// Build the next transaction
let _ = next_transaction.get_or_insert(adaptor.build_transaction());
Expand Down

0 comments on commit be5c9ee

Please sign in to comment.