memseek-test 0.20.2

Scenario-based async load testing framework with HTTP helpers, HDR histogram latency reports and graceful shutdown
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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>,
    /// 该场景的执行并发度(Manager 分配的任务数)。
    pub concurrency: u64,
    pub times: u64,
    /// 各任务耗时之和(含 setup),`merge` 累加;与墙钟 `elapsed` 不同
    pub total: Duration,
    /// 场景墙钟耗时(Manager 从分发任务到全部结束),用于 RPS 计算
    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 {
    /// 是否启用 ANSI 颜色。默认关闭,可用 `report_with` 显式开启
    pub color: bool,
    /// 条形图宽度(字符数)
    pub bar_width: usize,
}

impl Default for ReportOptions {
    fn default() -> Self {
        Self {
            color: false,
            bar_width: 20,
        }
    }
}

/// 可视化报告入口:让单个场景与多个场景统一支持 `.report()` / `.report_with()` 调用。
///
/// `Vec<ScenarioReport>` 通过本 crate 的 `impl Report for Vec<ScenarioReport>`
/// 直接可用,`&[ScenarioReport]` 亦可。
pub trait Report {
    /// 渲染为可视化文本(使用默认选项:颜色关闭)
    fn report(&self) -> String;

    /// 以自定义选项渲染为可视化文本
    fn report_with(&self, options: ReportOptions) -> String;

    /// 渲染为带 ANSI 颜色的可视化文本(条形图宽度使用默认值)
    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)
    }
}

// ---------- 渲染内核 ----------

/// 有效通过轮数:`run` 成功且业务校验(validate)通过。
/// validate 失败的轮次虽然计入 `success`(HTTP/业务请求本身成功),
/// 但断言未通过,不能视为通过。
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();

    // Success 指有效通过(含 validate):run 成功但断言失败的轮次另见 Validate 行
    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();
        // 默认不展示 Min/Max,聚焦均值与分位数;
        // 完整统计(含 Min/Max)仍在 ScenarioReport 字段与 Display 中可用。
        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();

        // 明细包含 run 失败与 validate 失败;分母为全部失败轮,保证占比对账
        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();
    // 多场景由 Manager 串行执行,汇总墙钟 = 各场景 elapsed 之和
    let total_elapsed: Duration = reports.iter().map(|r| r.elapsed).sum();
    // 有效通过:run 成功且 validate 通过,汇总与 Overall 均以此为准
    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 {
        // Rate 为有效通过率:run 成功且 validate 通过
        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();

            // 明细包含 run 失败与 validate 失败;分母为全部失败轮,保证占比对账
            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
}

// ---------- 渲染辅助 ----------

/// 是否启用颜色的判定:由 `ReportOptions::color` 显式控制。
/// 默认关闭颜色,保证输出可预测、可直接写入日志/文件;
/// 需要彩色时用 `report_with(ReportOptions { color: true, .. })`。
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"
    }
}

/// 生成 ASCII 条形图,`fraction` 为 0.0 ~ 1.0
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))
}

/// 可读化 Duration:一律以毫秒(ms)为单位,便于跨场景对比。
pub(crate) fn fmt_duration(d: Duration) -> String {
    format!("{:.2}ms", d.as_nanos() as f64 / 1e6)
}

/// 每秒请求数(RPS):总请求数 / 墙钟耗时,保留两位小数。
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)
}