use std::{
borrow::Cow,
collections::{BTreeMap, HashMap},
time::Duration,
};
use hdrhistogram::Histogram;
use tracing::warn;
#[cfg(feature = "tui")]
use crate::progress::Progress;
use crate::{error::ScenarioError, runner::RunnerConfig};
pub struct Recorder {
histogram: Histogram<u64>,
total: Duration,
#[cfg(feature = "tui")]
progress: Option<Progress>,
pub success: u64,
pub failures: u64,
pub timeouts: u64,
pub error_map: HashMap<Cow<'static, str>, u64>,
pub status_counts: BTreeMap<u16, u64>,
pub validate_success: u64,
pub validate_failures: u64,
pub setup_total: Duration,
pub validate_total: Duration,
pub teardown_total: Duration,
pub teardown_success: u64,
pub teardown_failures: u64,
pub interrupted: bool,
}
impl Default for Recorder {
fn default() -> Self {
Self::new()
}
}
impl Recorder {
pub fn new() -> Self {
Self {
histogram: Histogram::new(3).unwrap(),
total: Duration::ZERO,
#[cfg(feature = "tui")]
progress: None,
success: 0,
failures: 0,
timeouts: 0,
error_map: HashMap::new(),
status_counts: BTreeMap::new(),
validate_success: 0,
validate_failures: 0,
setup_total: Duration::ZERO,
validate_total: Duration::ZERO,
teardown_total: Duration::ZERO,
teardown_success: 0,
teardown_failures: 0,
interrupted: false,
}
}
pub fn from_config(_config: &RunnerConfig) -> Self {
Self {
#[cfg(feature = "tui")]
progress: _config.progress.clone(),
..Self::new()
}
}
pub fn total(&self) -> Duration {
self.total
}
pub fn times(&self) -> u64 {
self.histogram.len()
}
pub fn avg(&self) -> Duration {
if self.histogram.is_empty() {
return Duration::ZERO;
}
Duration::from_micros(self.histogram.mean() as u64)
}
pub fn min(&self) -> Duration {
if self.histogram.is_empty() {
Duration::ZERO
} else {
Duration::from_micros(self.histogram.min())
}
}
pub fn max(&self) -> Duration {
if self.histogram.is_empty() {
Duration::ZERO
} else {
Duration::from_micros(self.histogram.max())
}
}
pub fn p50(&mut self) -> Duration {
Duration::from_micros(self.histogram.value_at_quantile(0.50))
}
pub fn p95(&mut self) -> Duration {
Duration::from_micros(self.histogram.value_at_quantile(0.95))
}
pub fn p99(&mut self) -> Duration {
Duration::from_micros(self.histogram.value_at_quantile(0.99))
}
pub fn merge(&mut self, other: Self) {
self.histogram
.add(other.histogram)
.expect("merge Recorder failed");
self.total += other.total;
self.success += other.success;
self.failures += other.failures;
self.timeouts += other.timeouts;
for (kind, count) in other.error_map {
*self.error_map.entry(kind).or_insert(0) += count;
}
for (status, count) in other.status_counts {
*self.status_counts.entry(status).or_insert(0) += count;
}
self.validate_success += other.validate_success;
self.validate_failures += other.validate_failures;
self.setup_total += other.setup_total;
self.validate_total += other.validate_total;
self.teardown_total += other.teardown_total;
self.teardown_success += other.teardown_success;
self.teardown_failures += other.teardown_failures;
self.interrupted |= other.interrupted;
#[cfg(feature = "tui")]
{
self.progress = None;
}
}
#[cfg(feature = "tui")]
pub fn begin_round(&mut self) {
if let Some(progress) = &self.progress {
progress.begin_round();
}
}
#[cfg(feature = "tui")]
pub fn end_round(&mut self) {
if let Some(progress) = &self.progress {
progress.end_round();
}
}
#[cfg(feature = "tui")]
pub fn begin_setup(&mut self) {
if let Some(progress) = &self.progress {
progress.begin_setup();
}
}
#[cfg(feature = "tui")]
pub fn end_setup(&mut self) {
if let Some(progress) = &self.progress {
progress.end_setup();
}
}
pub fn record_duration(&mut self, duration: Duration) {
self.histogram
.record(duration.as_micros() as u64)
.expect("duration is out of histogram range");
self.total += duration;
#[cfg(feature = "tui")]
if let Some(progress) = &self.progress {
progress.round_recorded();
}
}
pub fn record_result<T, E>(&mut self, result: &std::result::Result<T, E>)
where
E: ScenarioError,
{
match result {
Ok(_) => self.success += 1,
Err(err) => {
self.log_internal(format!("{err:?}"));
if err.is_timeout() {
self.timeouts += 1;
#[cfg(feature = "tui")]
if let Some(progress) = &self.progress {
progress.timeout_recorded();
}
} else {
self.failures += 1;
#[cfg(feature = "tui")]
if let Some(progress) = &self.progress {
progress.result_recorded(false);
}
}
self.error_map
.entry(err.kind())
.and_modify(|count| *count += 1)
.or_insert(1);
self.record_status(err.status_code());
}
}
#[cfg(feature = "tui")]
if let Some(progress) = &self.progress {
progress.result_recorded(result.is_ok());
}
}
pub fn record_validate(&mut self, validate_result: bool) {
match validate_result {
true => {
self.validate_success += 1;
}
false => {
self.validate_failures += 1;
}
}
#[cfg(feature = "tui")]
if let Some(progress) = &self.progress {
progress.validate_recorded(validate_result);
}
}
pub fn record_validate_failure(&mut self, kind: Cow<'static, str>) {
self.record_validate_failure_with_status(kind, None);
}
pub fn record_validate_failure_with_status(
&mut self,
kind: Cow<'static, str>,
status: Option<u16>,
) {
self.validate_failures += 1;
self.error_map
.entry(kind)
.and_modify(|count| *count += 1)
.or_insert(1);
self.record_status(status);
#[cfg(feature = "tui")]
if let Some(progress) = &self.progress {
progress.validate_recorded(false);
}
}
pub(crate) fn log_internal(&self, message: String) {
#[cfg(feature = "tui")]
if self.progress.as_ref().is_some_and(|p| p.push_log(&message)) {
return;
}
warn!("{message}");
}
pub fn record_setup_duration(&mut self, duration: Duration) {
self.setup_total += duration;
}
pub fn record_validate_duration(&mut self, duration: Duration) {
self.validate_total += duration;
}
pub fn record_teardown_duration(&mut self, duration: Duration) {
self.teardown_total += duration;
}
pub fn record_teardown(&mut self, ok: bool) {
if ok {
self.teardown_success += 1;
} else {
self.teardown_failures += 1;
}
#[cfg(feature = "tui")]
if let Some(progress) = &self.progress {
progress.teardown_recorded(ok);
}
}
pub fn record_teardown_error(&mut self, kind: Cow<'static, str>) {
self.record_teardown_error_with_status(kind, None);
}
pub fn record_teardown_error_with_status(
&mut self,
kind: Cow<'static, str>,
status: Option<u16>,
) {
let key: Cow<'static, str> = format!("teardown:{kind}").into();
self.error_map
.entry(key)
.and_modify(|count| *count += 1)
.or_insert(1);
self.record_status(status);
}
fn record_status(&mut self, status: Option<u16>) {
if let Some(status) = status {
*self.status_counts.entry(status).or_insert(0) += 1;
}
}
pub fn record_interrupted(&mut self) {
self.interrupted = true;
}
}