use std::{
borrow::Cow,
collections::HashMap,
fmt::{Display, Formatter, Result, Write as _},
time::Duration,
};
use crate::recorder::Recorder;
#[derive(Debug)]
pub struct ScenarioReport {
pub name: Cow<'static, str>,
pub concurrency: u64,
pub times: u64,
pub total: Duration,
pub elapsed: Duration,
pub avg: Duration,
pub min: Duration,
pub max: Duration,
pub p50: Duration,
pub p95: Duration,
pub p99: Duration,
pub validate_success: u64,
pub validate_failures: u64,
pub success: u64,
pub failures: u64,
pub error_map: HashMap<Cow<'static, str>, u64>,
pub interrupted: bool,
}
impl ScenarioReport {
pub fn from_recorder(
name: impl Into<Cow<'static, str>>,
mut recorder: Recorder,
concurrency: u64,
elapsed: Duration,
) -> Self {
Self {
name: name.into(),
concurrency,
times: recorder.times(),
total: recorder.total(),
elapsed,
avg: recorder.avg(),
min: recorder.min(),
max: recorder.max(),
p50: recorder.p50(),
p95: recorder.p95(),
p99: recorder.p99(),
validate_success: recorder.validate_success,
validate_failures: recorder.validate_failures,
success: recorder.success,
failures: recorder.failures,
error_map: recorder.error_map,
interrupted: recorder.interrupted,
}
}
}
impl Display for ScenarioReport {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
writeln!(f, "Scenario: {}", self.name)?;
writeln!(f, " Times: {}", self.times)?;
writeln!(f, " Concurrency: {}", self.concurrency)?;
writeln!(f, " Total: {}", fmt_duration(self.total))?;
writeln!(f, " RPS: {}", fmt_rps(self.times, self.elapsed))?;
writeln!(f, " Min: {}", fmt_duration(self.min))?;
writeln!(f, " Avg: {}", fmt_duration(self.avg))?;
writeln!(f, " Max: {}", fmt_duration(self.max))?;
writeln!(f, " P50: {}", fmt_duration(self.p50))?;
writeln!(f, " P95: {}", fmt_duration(self.p95))?;
writeln!(f, " P99: {}", fmt_duration(self.p99))?;
writeln!(f, " Validate Success: {}", self.validate_success)?;
writeln!(f, " Validate Failures: {}", self.validate_failures)?;
writeln!(f, " Success: {}", self.success)?;
writeln!(f, " Failures: {}", self.failures)?;
if self.interrupted {
writeln!(f, " Interrupted: true (graceful shutdown)")?;
}
if !self.error_map.is_empty() {
writeln!(f, " Errors:")?;
for (kind, count) in &self.error_map {
writeln!(f, " {kind}: {count}")?;
}
}
Ok(())
}
}
#[derive(Debug, Clone, Copy)]
pub struct ReportOptions {
pub color: bool,
pub bar_width: usize,
}
impl Default for ReportOptions {
fn default() -> Self {
Self {
color: false,
bar_width: 20,
}
}
}
pub trait Report {
fn report(&self) -> String;
fn report_with(&self, options: ReportOptions) -> String;
fn report_with_color(&self) -> String {
self.report_with(ReportOptions {
color: true,
..ReportOptions::default()
})
}
}
impl Report for ScenarioReport {
fn report(&self) -> String {
render_single(self, &ReportOptions::default())
}
fn report_with(&self, options: ReportOptions) -> String {
render_single(self, &options)
}
}
impl Report for [ScenarioReport] {
fn report(&self) -> String {
render_many(self, &ReportOptions::default())
}
fn report_with(&self, options: ReportOptions) -> String {
render_many(self, &options)
}
}
impl Report for Vec<ScenarioReport> {
fn report(&self) -> String {
self.as_slice().report()
}
fn report_with(&self, options: ReportOptions) -> String {
self.as_slice().report_with(options)
}
}
fn passed_rounds(success: u64, validate_failures: u64) -> u64 {
success.saturating_sub(validate_failures)
}
fn render_single(r: &ScenarioReport, o: &ReportOptions) -> String {
let mut out = String::new();
writeln!(
out,
"{}",
paint("============= Scenario =============", "1;36", o.color)
)
.unwrap();
writeln!(out, "{}", paint(&format!(" {}", r.name), "1", o.color)).unwrap();
writeln!(
out,
"{}",
paint("====================================", "1;36", o.color)
)
.unwrap();
let passed = passed_rounds(r.success, r.validate_failures);
let success_rate = rate_of(passed, r.times);
let fail_rate = rate_of(r.failures, r.times);
writeln!(out, "Requests : {}", r.times).unwrap();
writeln!(out, "Concurrent: {}", r.concurrency).unwrap();
writeln!(out, "Total : {}", fmt_duration(r.total)).unwrap();
writeln!(out, "RPS : {}", fmt_rps(r.times, r.elapsed)).unwrap();
writeln!(
out,
"Success : {} ({:.1}%) {}",
passed,
success_rate * 100.0,
paint(rate_label(success_rate), rate_color(success_rate), o.color),
)
.unwrap();
writeln!(out, "Failures : {} ({:.1}%)", r.failures, fail_rate * 100.0,).unwrap();
writeln!(
out,
"Validate : {} ok / {} fail",
r.validate_success, r.validate_failures,
)
.unwrap();
if r.interrupted {
writeln!(
out,
"{}",
paint("Interrupted: yes (graceful shutdown)", "33", o.color)
)
.unwrap();
}
if r.times > 0 {
let max_ns = r.max.as_nanos().max(1) as f64;
writeln!(out).unwrap();
writeln!(out, "Latency (relative to max):").unwrap();
for (label, d) in [
("Avg", r.avg),
("P50", r.p50),
("P95", r.p95),
("P99", r.p99),
] {
let frac = d.as_nanos() as f64 / max_ns;
writeln!(
out,
" {:<4} {} {}",
label,
bar(frac, o.bar_width),
fmt_duration(d),
)
.unwrap();
}
}
if !r.error_map.is_empty() {
writeln!(out).unwrap();
writeln!(out, "Errors:").unwrap();
let mut errs: Vec<_> = r.error_map.iter().collect();
errs.sort_by(|a, b| b.1.cmp(a.1));
let denom = (r.failures + r.validate_failures).max(1) as f64;
for (kind, count) in errs {
let frac = *count as f64 / denom;
writeln!(
out,
" {:<24} {} {} ({:.1}%)",
kind,
bar(frac, o.bar_width),
count,
frac * 100.0,
)
.unwrap();
}
}
out
}
fn render_many(reports: &[ScenarioReport], o: &ReportOptions) -> String {
let mut out = String::new();
if reports.is_empty() {
return paint("No reports.", "33", o.color);
}
let total_times: u64 = reports.iter().map(|r| r.times).sum();
let total_success: u64 = reports.iter().map(|r| r.success).sum();
let total_failures: u64 = reports.iter().map(|r| r.failures).sum();
let total_validate_success: u64 = reports.iter().map(|r| r.validate_success).sum();
let total_validate_failures: u64 = reports.iter().map(|r| r.validate_failures).sum();
let total_total: Duration = reports.iter().map(|r| r.total).sum();
let total_elapsed: Duration = reports.iter().map(|r| r.elapsed).sum();
let total_passed = total_success.saturating_sub(total_validate_failures);
let first_concurrency = reports[0].concurrency;
let concurrency_label = if reports.iter().all(|r| r.concurrency == first_concurrency) {
first_concurrency.to_string()
} else {
let min = reports.iter().map(|r| r.concurrency).min().unwrap();
let max = reports.iter().map(|r| r.concurrency).max().unwrap();
format!("{min}-{max}")
};
let avg: Duration = if total_times > 0 {
let num: u128 = reports
.iter()
.map(|r| r.avg.as_nanos() * r.times as u128)
.sum();
Duration::from_nanos((num / total_times as u128) as u64)
} else {
Duration::ZERO
};
let total_rate = rate_of(total_passed, total_times);
writeln!(
out,
"{}",
paint("============== Summary ==============", "1;36", o.color)
)
.unwrap();
writeln!(out, "Scenarios : {}", reports.len()).unwrap();
writeln!(out, "Concurrent : {concurrency_label}").unwrap();
writeln!(out, "Requests : {}", total_times).unwrap();
writeln!(out, "Total : {}", fmt_duration(total_total)).unwrap();
writeln!(out, "RPS : {}", fmt_rps(total_times, total_elapsed)).unwrap();
writeln!(out, "Avg : {}", fmt_duration(avg)).unwrap();
writeln!(
out,
"Success : {} ({:.1}%) {}",
total_passed,
total_rate * 100.0,
paint(rate_label(total_rate), rate_color(total_rate), o.color),
)
.unwrap();
writeln!(out, "Failures : {}", total_failures).unwrap();
writeln!(
out,
"Validate : {} ok / {} fail",
total_validate_success, total_validate_failures,
)
.unwrap();
let interrupted_count = reports.iter().filter(|r| r.interrupted).count();
if interrupted_count > 0 {
writeln!(
out,
"{}",
paint(
&format!(
"Interrupted: {interrupted_count}/{} scenarios (graceful shutdown)",
reports.len()
),
"33",
o.color,
)
)
.unwrap();
}
writeln!(
out,
"Overall : {} {}",
bar(total_rate, o.bar_width),
paint(rate_label(total_rate), rate_color(total_rate), o.color),
)
.unwrap();
writeln!(out).unwrap();
writeln!(out, "{}", paint("---- Per-scenario ----", "1;36", o.color)).unwrap();
let name_w = reports
.iter()
.map(|r| r.name.len())
.max()
.unwrap_or(0)
.max(4);
let header = format!(
"{:<name_w$} {:>8} {:>10} {:>10} {:>8} {:>10} {:>10} {:>12}",
"Name",
"Times",
"Success",
"Failures",
"Rate",
"Avg",
"P95",
"Validate",
name_w = name_w,
);
writeln!(out, "{}", paint(&header, "36", o.color)).unwrap();
for r in reports {
let rate = rate_of(passed_rounds(r.success, r.validate_failures), r.times);
writeln!(
out,
"{:<name_w$} {:>8} {:>10} {:>10} {:>8} {:>10} {:>10} {:>12}",
r.name,
r.times,
r.success,
r.failures,
format!("{:.1}%", rate * 100.0),
fmt_duration(r.avg),
fmt_duration(r.p95),
format!("{}/{}", r.validate_success, r.validate_failures),
name_w = name_w,
)
.unwrap();
}
let err_rows: Vec<_> = reports.iter().filter(|r| !r.error_map.is_empty()).collect();
if !err_rows.is_empty() {
writeln!(out).unwrap();
writeln!(out, "{}", paint("---- Errors ----", "1;36", o.color)).unwrap();
for r in err_rows {
writeln!(out, "{}", paint(&format!(" {}.", r.name), "1", o.color)).unwrap();
let mut errs: Vec<_> = r.error_map.iter().collect();
errs.sort_by(|a, b| b.1.cmp(a.1));
let denom = (r.failures + r.validate_failures).max(1) as f64;
for (kind, count) in errs {
let frac = *count as f64 / denom;
writeln!(
out,
" {:<24} {} {} ({:.1}%)",
kind,
bar(frac, o.bar_width),
count,
frac * 100.0,
)
.unwrap();
}
}
}
out
}
pub(crate) fn paint(s: &str, code: &str, enabled: bool) -> String {
if enabled {
format!("\x1b[{code}m{s}\x1b[0m")
} else {
s.to_string()
}
}
fn rate_of(passed: u64, total: u64) -> f64 {
if total == 0 {
0.0
} else {
passed as f64 / total as f64
}
}
fn rate_color(rate: f64) -> &'static str {
if rate >= 0.99 {
"32" } else if rate >= 0.90 {
"33" } else {
"31" }
}
fn rate_label(rate: f64) -> &'static str {
if rate >= 0.99 {
"OK"
} else if rate >= 0.90 {
"WARN"
} else {
"BAD"
}
}
pub(crate) fn bar(fraction: f64, width: usize) -> String {
let width = width.max(1);
let filled = (fraction.clamp(0.0, 1.0) * width as f64).round() as usize;
format!("{}{}", "█".repeat(filled), "░".repeat(width - filled))
}
pub(crate) fn fmt_duration(d: Duration) -> String {
format!("{:.2}ms", d.as_nanos() as f64 / 1e6)
}
pub(crate) fn fmt_rps(times: u64, elapsed: Duration) -> String {
let secs = elapsed.as_secs_f64();
if secs <= 0.0 {
return "0.00".to_string();
}
format!("{:.2}", times as f64 / secs)
}