use std::collections::BTreeMap;
use std::time::Duration;
use hdrhistogram::Histogram;
use serde::{Deserialize, Serialize};
use crate::engine::Measurements;
use crate::outcome::ErrorKind;
pub const SCHEMA_VERSION: u32 = 1;
pub const SATURATION_LAG: Duration = Duration::from_millis(50);
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RunReport {
pub schema_version: u32,
pub p99gate_version: String,
pub target: Target,
pub config: ConfigReport,
pub run: RunInfo,
pub requests: Requests,
pub throughput: Throughput,
pub error_rate: f64,
pub latency: LatencyReport,
pub send_lag: Percentiles,
pub status_codes: BTreeMap<u16, u64>,
pub errors: BTreeMap<ErrorKind, u64>,
pub saturated: bool,
pub thresholds: Vec<ThresholdResult>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Target {
pub protocol: String,
pub method: String,
pub url: String,
pub http_version: String,
pub proxy: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConfigReport {
pub rate_per_s: f64,
pub duration_s: f64,
pub concurrency: usize,
pub timeout_s: f64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RunInfo {
pub started_at: String,
pub send_window_s: f64,
pub elapsed_s: f64,
pub interrupted: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Requests {
pub scheduled: u64,
pub sent: u64,
pub unsent: u64,
pub completed: u64,
pub succeeded: u64,
pub failed: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Throughput {
pub target_per_s: f64,
pub sent_per_s: f64,
pub completed_per_s: f64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LatencyReport {
#[serde(flatten)]
pub percentiles: Percentiles,
pub histogram: Vec<Bucket>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[allow(missing_docs, reason = "field names are self-describing")]
pub struct Percentiles {
pub min_us: u64,
pub mean_us: u64,
pub p50_us: u64,
pub p75_us: u64,
pub p90_us: u64,
pub p95_us: u64,
pub p99_us: u64,
pub p999_us: u64,
pub max_us: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Bucket {
pub le_us: u64,
pub count: u64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ThresholdResult {
pub expression: String,
pub observed: f64,
pub unit: Unit,
pub violated: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Unit {
#[serde(rename = "us")]
Micros,
#[serde(rename = "ratio")]
Ratio,
#[serde(rename = "per_s")]
PerSecond,
}
impl RunReport {
#[must_use]
pub fn new(measurements: &Measurements, target: Target) -> Self {
let m = measurements;
let r = &m.recorder;
let completed = r.completed();
let failed = r.failures();
let send_lag = Percentiles::from_histogram(r.send_lag());
let unsent = m.unsent();
Self {
schema_version: SCHEMA_VERSION,
p99gate_version: env!("CARGO_PKG_VERSION").to_owned(),
target,
config: ConfigReport {
rate_per_s: m.config.rate.get(),
duration_s: m.config.duration.as_secs_f64(),
concurrency: m.config.concurrency.get(),
timeout_s: m.config.timeout.as_secs_f64(),
},
run: RunInfo {
started_at: humantime::format_rfc3339_millis(m.started_at).to_string(),
send_window_s: m.send_window.as_secs_f64(),
elapsed_s: m.elapsed.as_secs_f64(),
interrupted: m.interrupted,
},
requests: Requests {
scheduled: m.scheduled,
sent: m.sent,
unsent,
completed,
succeeded: completed - failed,
failed,
},
throughput: Throughput {
target_per_s: m.config.rate.get(),
sent_per_s: per_second(m.sent, m.send_window),
completed_per_s: per_second(completed, m.send_window),
},
error_rate: if completed == 0 {
0.0
} else {
failed as f64 / completed as f64
},
latency: LatencyReport {
percentiles: Percentiles::from_histogram(r.latency()),
histogram: buckets(r.latency()),
},
send_lag,
status_codes: r.statuses().clone(),
errors: r.errors().clone(),
saturated: unsent > 0 || u128::from(send_lag.p99_us) > SATURATION_LAG.as_micros(),
thresholds: Vec::new(),
}
}
#[must_use]
pub fn any_threshold_violated(&self) -> bool {
self.thresholds.iter().any(|t| t.violated)
}
}
impl Percentiles {
fn from_histogram(h: &Histogram<u64>) -> Self {
if h.is_empty() {
return Self::default();
}
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let mean_us = h.mean().round() as u64;
Self {
min_us: h.min(),
mean_us,
p50_us: h.value_at_quantile(0.50),
p75_us: h.value_at_quantile(0.75),
p90_us: h.value_at_quantile(0.90),
p95_us: h.value_at_quantile(0.95),
p99_us: h.value_at_quantile(0.99),
p999_us: h.value_at_quantile(0.999),
max_us: h.max(),
}
}
}
fn per_second(count: u64, over: Duration) -> f64 {
let secs = over.as_secs_f64();
if secs > 0.0 { count as f64 / secs } else { 0.0 }
}
fn bucket_bounds() -> impl Iterator<Item = u64> {
std::iter::successors(Some(1_u64), |&b| b.checked_mul(10))
.flat_map(|decade| [decade, 2 * decade, 5 * decade])
}
fn buckets(h: &Histogram<u64>) -> Vec<Bucket> {
if h.is_empty() {
return Vec::new();
}
let max = h.max();
let mut out: Vec<Bucket> = bucket_bounds()
.scan(false, |past_max, le_us| {
let done = *past_max;
*past_max = le_us >= max;
(!done).then_some(Bucket { le_us, count: 0 })
})
.collect();
for value in h.iter_recorded() {
let index = out.partition_point(|b| b.le_us < value.value_iterated_to());
out[index].count += value.count_at_value();
}
let first = out.iter().position(|b| b.count > 0).unwrap_or(0);
out.drain(..first);
out
}
#[cfg(test)]
mod tests {
use super::*;
fn histogram(values_us: &[u64]) -> Histogram<u64> {
let mut h = Histogram::new_with_bounds(1, 3_600_000_000, 3).unwrap();
for &v in values_us {
h.record(v).unwrap();
}
h
}
#[test]
fn bucket_bounds_follow_a_1_2_5_scale() {
let bounds: Vec<_> = bucket_bounds().take(7).collect();
assert_eq!(bounds, [1, 2, 5, 10, 20, 50, 100]);
}
#[test]
fn buckets_cover_every_sample_once_and_trim_empty_leading_buckets() {
let h = histogram(&[150, 180, 200, 201, 4_000, 9_000]);
let b = buckets(&h);
assert_eq!(b.first().unwrap().le_us, 200);
assert_eq!(b.last().unwrap().le_us, 10_000);
assert_eq!(b.iter().map(|b| b.count).sum::<u64>(), 6);
let counts: Vec<_> = b.iter().map(|b| (b.le_us, b.count)).collect();
assert_eq!(
counts,
[
(200, 3),
(500, 1),
(1_000, 0),
(2_000, 0),
(5_000, 1),
(10_000, 1)
]
);
}
#[test]
fn values_near_bucket_edges_are_counted_once() {
let h = histogram(&[9_999, 10_000, 10_003, 10_008]);
let b = buckets(&h);
assert_eq!(b.iter().map(|b| b.count).sum::<u64>(), 4);
}
#[test]
fn empty_histograms_produce_zeroes() {
let h = histogram(&[]);
assert_eq!(Percentiles::from_histogram(&h), Percentiles::default());
assert_eq!(buckets(&h), Vec::<Bucket>::new());
}
#[tokio::test(start_paused = true)]
async fn report_from_a_run_round_trips_through_json() {
use std::num::NonZeroUsize;
use crate::engine::{Engine, LoadConfig};
use crate::executor::Executor;
use crate::outcome::Outcome;
use crate::schedule::Rate;
struct Alternating(std::sync::atomic::AtomicU64);
impl Executor for Alternating {
async fn execute(&self) -> Outcome {
tokio::time::sleep(Duration::from_millis(3)).await;
let n = self.0.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Outcome::Response {
status: if n % 4 == 0 { 500 } else { 200 },
}
}
}
let config = LoadConfig {
rate: Rate::per_second(40.0).unwrap(),
duration: Duration::from_secs(1),
concurrency: NonZeroUsize::new(8).unwrap(),
timeout: Duration::from_secs(1),
};
let measurements = Engine::new(config, Alternating(0.into())).run().await;
let report = RunReport::new(
&measurements,
Target {
protocol: "http".into(),
method: "GET".into(),
url: "http://test/".into(),
http_version: "1.1".into(),
proxy: None,
},
);
assert_eq!(report.requests.completed, 40);
assert_eq!(report.requests.failed, 10);
assert!((report.error_rate - 0.25).abs() < f64::EPSILON);
assert_eq!(report.status_codes[&200], 30);
assert!(!report.saturated);
assert!((3_000..=3_010).contains(&report.latency.percentiles.p99_us));
let json = serde_json::to_string(&report).unwrap();
assert!(json.contains(r#""status_codes":{"200":30,"500":10}"#));
assert!(json.contains(r#""p99_us":"#), "percentiles are flattened");
let parsed: RunReport = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, report);
}
#[test]
fn per_second_handles_zero_duration() {
assert!((per_second(10, Duration::from_secs(2)) - 5.0).abs() < f64::EPSILON);
assert!(per_second(10, Duration::ZERO).abs() < f64::EPSILON);
}
}