Skip to content

Commit

Permalink
Merge pull request #469 from hatoo/refactor-monitor
Browse files Browse the repository at this point in the history
Refactor Monitor to use MinMaxMean for calculating statistics
  • Loading branch information
hatoo authored Apr 20, 2024
2 parents 7666428 + 9e01dfa commit eb483f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
37 changes: 9 additions & 28 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{collections::BTreeMap, io};
use crate::{
client::{ClientError, RequestResult},
printer::PrintMode,
result_data::ResultData,
result_data::{MinMaxMean, ResultData},
timescale::{TimeLabel, TimeScale},
};

Expand Down Expand Up @@ -236,42 +236,23 @@ impl Monitor {
&success[index..]
};

let last_1_minmaxmean: MinMaxMean = last_1_timescale
.iter()
.map(|r| r.duration().as_secs_f64())
.collect();

let stats_text = vec![
Line::from(format!("Requests : {}", last_1_timescale.len())),
Line::from(vec![Span::styled(
format!(
"Slowest: {:.4} secs",
last_1_timescale
.iter()
.map(|r| r.duration())
.max()
.map(|d| d.as_secs_f64())
.unwrap_or(f64::NAN)
),
format!("Slowest: {:.4} secs", last_1_minmaxmean.max(),),
Style::default().fg(colors.yellow.unwrap_or(Color::Reset)),
)]),
Line::from(vec![Span::styled(
format!(
"Fastest: {:.4} secs",
last_1_timescale
.iter()
.map(|r| r.duration())
.min()
.map(|d| d.as_secs_f64())
.unwrap_or(f64::NAN)
),
format!("Fastest: {:.4} secs", last_1_minmaxmean.min(),),
Style::default().fg(colors.green.unwrap_or(Color::Reset)),
)]),
Line::from(vec![Span::styled(
format!(
"Average: {:.4} secs",
last_1_timescale
.iter()
.map(|r| r.duration())
.sum::<std::time::Duration>()
.as_secs_f64()
/ last_1_timescale.len() as f64
),
format!("Average: {:.4} secs", last_1_minmaxmean.mean(),),
Style::default().fg(colors.light_blue.unwrap_or(Color::Reset)),
)]),
Line::from(format!(
Expand Down
6 changes: 6 additions & 0 deletions src/result_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ impl MinMaxMean {
}
}

impl FromIterator<f64> for MinMaxMean {
fn from_iter<I: IntoIterator<Item = f64>>(iter: I) -> Self {
Self(MinMaxMeanInner::from_iter(iter))
}
}

pub struct Statistics {
pub percentiles: Vec<(f64, f64)>,
pub histogram: Vec<(f64, usize)>,
Expand Down

0 comments on commit eb483f0

Please sign in to comment.