p99gate 0.1.0

HTTP load testing for CI: open-loop, coordinated-omission-corrected latency, with pass/fail thresholds
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! The result of a run, in a stable, serialisable form.
//!
//! [`RunReport`] is the single source for every output: the terminal report,
//! `--output json`, threshold evaluation, and (in later versions) comparisons
//! between runs. Its JSON shape is documented in `docs/json-schema.md` and
//! versioned by [`SCHEMA_VERSION`].
//!
//! Units are part of the field names: `_us` is integer microseconds, `_s` is
//! fractional seconds, `_per_s` is a rate.

use std::collections::BTreeMap;
use std::time::Duration;

use hdrhistogram::Histogram;
use serde::{Deserialize, Serialize};

use crate::engine::Measurements;
use crate::outcome::ErrorKind;

/// Version of the JSON schema produced by [`RunReport`].
///
/// Adding fields does not change it; renaming, removing or changing the
/// meaning of a field does.
pub const SCHEMA_VERSION: u32 = 1;

/// p99 send lag above which the run is flagged as not having sustained its
/// target rate. Tokio's timer has millisecond granularity and some platforms
/// schedule more coarsely, so small lags are normal and not reported.
pub const SATURATION_LAG: Duration = Duration::from_millis(50);

/// The full result of one run.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RunReport {
    /// Always [`SCHEMA_VERSION`] for reports produced by this version.
    pub schema_version: u32,
    /// Version of p99gate that produced the report.
    pub p99gate_version: String,
    /// What was tested.
    pub target: Target,
    /// The requested load.
    pub config: ConfigReport,
    /// When and for how long the run happened.
    pub run: RunInfo,
    /// Request counts.
    pub requests: Requests,
    /// Achieved rates.
    pub throughput: Throughput,
    /// Fraction of completed requests that failed (0.0 to 1.0).
    pub error_rate: f64,
    /// Latency from intended send time to completion, for every completed request.
    pub latency: LatencyReport,
    /// How late requests were sent relative to the schedule.
    pub send_lag: Percentiles,
    /// Responses per status code.
    pub status_codes: BTreeMap<u16, u64>,
    /// Transport errors per kind.
    pub errors: BTreeMap<ErrorKind, u64>,
    /// Whether the generator failed to sustain the target rate. See [`SATURATION_LAG`].
    pub saturated: bool,
    /// Results of `--fail-if` thresholds, in the order given.
    pub thresholds: Vec<ThresholdResult>,
}

/// What was tested.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Target {
    /// Protocol, currently always `"http"`.
    pub protocol: String,
    /// Request method.
    pub method: String,
    /// Request URL.
    pub url: String,
    /// HTTP version: `"1.1"` or `"2"`.
    pub http_version: String,
    /// The proxy requests went through, without credentials, or `None`.
    pub proxy: Option<String>,
}

/// The requested load.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConfigReport {
    /// Target rate.
    pub rate_per_s: f64,
    /// Configured duration.
    pub duration_s: f64,
    /// Maximum requests in flight.
    pub concurrency: usize,
    /// Per-request timeout.
    pub timeout_s: f64,
}

/// When and for how long the run happened.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RunInfo {
    /// RFC 3339 UTC timestamp of the start.
    pub started_at: String,
    /// From start until sending stopped.
    pub send_window_s: f64,
    /// From start until the last response arrived.
    pub elapsed_s: f64,
    /// Whether the run was stopped early (for example by Ctrl-C).
    pub interrupted: bool,
}

/// Request counts. `completed = succeeded + failed`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Requests {
    /// Requests the schedule called for over the configured duration.
    pub scheduled: u64,
    /// Requests sent.
    pub sent: u64,
    /// Requests that were due but not sent because the generator fell behind.
    pub unsent: u64,
    /// Requests that finished, successfully or not.
    pub completed: u64,
    /// Completed requests with a status below 400.
    pub succeeded: u64,
    /// Completed requests with a transport error or a status of 400 or above.
    pub failed: u64,
}

/// Achieved rates.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Throughput {
    /// The configured rate.
    pub target_per_s: f64,
    /// `sent / send_window_s`.
    pub sent_per_s: f64,
    /// `completed / send_window_s`: responses per second of test. Responses
    /// that arrive while in-flight requests drain after sending stops still
    /// count, so a run where everything completes matches `sent_per_s`.
    pub completed_per_s: f64,
}

/// Latency percentiles plus a coarse histogram.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LatencyReport {
    /// Summary statistics.
    #[serde(flatten)]
    pub percentiles: Percentiles,
    /// Counts per latency range, on a 1-2-5 scale, from the lowest to the
    /// highest non-empty bucket.
    pub histogram: Vec<Bucket>,
}

/// Summary statistics of a distribution, in microseconds.
#[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,
}

/// Number of samples with a value in `(previous bucket's le_us, le_us]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Bucket {
    /// Inclusive upper bound.
    pub le_us: u64,
    /// Samples in the bucket.
    pub count: u64,
}

/// The outcome of one `--fail-if` threshold.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ThresholdResult {
    /// The threshold as written, e.g. `"p99>300ms"`.
    pub expression: String,
    /// The measured value, in `unit`.
    pub observed: f64,
    /// The unit of `observed`.
    pub unit: Unit,
    /// Whether the threshold was violated, i.e. the run should fail.
    pub violated: bool,
}

/// The unit of a threshold's observed value.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Unit {
    /// Microseconds.
    #[serde(rename = "us")]
    Micros,
    /// A fraction from 0.0 to 1.0.
    #[serde(rename = "ratio")]
    Ratio,
    /// Events per second.
    #[serde(rename = "per_s")]
    PerSecond,
}

impl RunReport {
    /// Builds the report for a finished run.
    #[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(),
        }
    }

    /// Whether any threshold was violated.
    #[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();
        }
        // `mean` is a float over integer microseconds; rounding to the
        // nearest microsecond loses nothing meaningful.
        #[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 }
}

/// Upper bounds of the histogram buckets: 1, 2, 5, 10, 20, 50, ... µs.
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])
}

/// Groups samples into 1-2-5 buckets, trimmed to the non-empty range.
///
/// Each recorded value is assigned to exactly one bucket. (Range queries such
/// as `Histogram::count_between` work on HDR equivalence ranges and can count
/// a value near a bucket edge in both neighbouring buckets.)
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| {
            // Include the first bound at or above `max`, then stop.
            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() {
        // At this magnitude HDR stores values in ranges 8 wide, so 10_003
        // straddles the 10_000 bucket edge.
        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);
    }
}