mockforge-bench 0.3.148

Load and performance testing for MockForge
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
//! Result reporting and formatting

use crate::executor::K6Results;
use crate::parallel_executor::AggregatedResults;
use colored::*;

/// Terminal reporter for bench results
pub struct TerminalReporter;

impl TerminalReporter {
    /// Print a summary of the bench results.
    ///
    /// `cps_mode` is `true` when the bench was invoked with `--cps`. In that
    /// mode each request opens a fresh TCP/TLS connection, so we print an
    /// explicit "Connection Rate" line alongside RPS — Srikanth's round-5
    /// reply on Issue #79: "CPS without RPS Command is Working but Client
    /// dont report CPS Counts".
    pub fn print_summary(results: &K6Results, duration_secs: u64) {
        Self::print_summary_with_mode(results, duration_secs, false);
    }

    /// Like [`print_summary`] but lets the caller opt into the `--cps` view.
    ///
    /// Issue #79 round 6 — the connection-count lines now render unconditionally
    /// whenever k6 reported `http_req_connecting` samples (i.e. it actually
    /// opened TCP sockets), so non-`--cps` runs also surface client-side
    /// connection counts. Without `--cps` k6 reuses sockets, so the count
    /// equals "distinct connections opened", which is what Srikanth wanted
    /// alongside RPS.
    pub fn print_summary_with_mode(results: &K6Results, duration_secs: u64, cps_mode: bool) {
        Self::print_summary_full(results, duration_secs, cps_mode, None);
    }

    /// Like [`print_summary_with_mode`] but accepts `num_operations` (the count
    /// of operations the bench generated from the spec). When supplied, the
    /// summary surfaces iteration coverage: how many iterations completed and
    /// what fraction of the spec's operations got exercised end-to-end.
    ///
    /// Issue #79 round 10 — Srikanth's 11422-op spec ran for 600s with only
    /// `--vus 5`; many iterations were cancelled mid-way and the final
    /// summary didn't reveal which slice of the spec was actually covered.
    pub fn print_summary_full(
        results: &K6Results,
        duration_secs: u64,
        cps_mode: bool,
        num_operations: Option<u32>,
    ) {
        println!("\n{}", "=".repeat(60).bright_green());
        println!("{}", "Load Test Complete! ✓".bright_green().bold());
        println!("{}\n", "=".repeat(60).bright_green());

        println!("{}", "Summary:".bold());
        println!("  Total Requests:       {}", results.total_requests.to_string().cyan());
        println!(
            "  Successful:           {} ({}%)",
            (results.total_requests - results.failed_requests).to_string().green(),
            format!("{:.2}", results.success_rate()).green()
        );
        println!(
            "  Failed:               {} ({}%)",
            results.failed_requests.to_string().red(),
            format!("{:.2}", results.error_rate()).red()
        );

        println!("\n{}", "Response Times:".bold());
        println!("  Min:                  {}ms", format!("{:.2}", results.min_duration_ms).cyan());
        println!("  Avg:                  {}ms", format!("{:.2}", results.avg_duration_ms).cyan());
        println!("  Med:                  {}ms", format!("{:.2}", results.med_duration_ms).cyan());
        println!("  p90:                  {}ms", format!("{:.2}", results.p90_duration_ms).cyan());
        println!("  p95:                  {}ms", format!("{:.2}", results.p95_duration_ms).cyan());
        println!("  p99:                  {}ms", format!("{:.2}", results.p99_duration_ms).cyan());
        println!("  Max:                  {}ms", format!("{:.2}", results.max_duration_ms).cyan());

        println!("\n{}", "Throughput:".bold());
        if results.rps > 0.0 {
            println!("  RPS:                  {} req/s", format!("{:.1}", results.rps).cyan());
        } else {
            println!(
                "  RPS:                  {} req/s",
                format!("{:.1}", results.total_requests as f64 / duration_secs as f64).cyan()
            );
        }
        if results.vus_max > 0 {
            println!("  Max VUs:              {}", results.vus_max.to_string().cyan());
        }

        // Issue #79 (round 5) — Connections-per-second report. When the user
        // passed `--cps`, k6 ran with `noConnectionReuse: true` so each
        // request opened a new TCP/TLS connection. CPS therefore equals the
        // request rate; show it explicitly, plus connect/handshake timing.
        if cps_mode {
            let cps = if results.rps > 0.0 {
                results.rps
            } else {
                results.total_requests as f64 / duration_secs.max(1) as f64
            };
            println!("  CPS:                  {} conn/s (--cps)", format!("{:.1}", cps).cyan());
            println!("  Total Connections:    {}", results.total_requests.to_string().cyan());
        }

        // Issue #79 round 6 — Always surface client-side connection counts
        // when k6 actually opened sockets. Helpful even without `--cps`
        // because it lets you compare "k6 opened N connections" against
        // the server's `connections_total_opened` and detect whether
        // your proxy is keeping the upstream pool warm.
        //
        // Round 6 follow-up: `tcp_connect_samples` is now sourced from the
        // template's `mockforge_connections_opened` Counter (incremented when
        // `res.timings.connecting > 0`). That gives an accurate count for
        // both `--cps` (≈ total requests) and the pooled-reuse case (≈ vus_max)
        // — Srikanth's "Open/Closed Connection Counter not showing for RPS"
        // report on Issue #79.
        if results.tcp_connect_samples > 0 && !cps_mode {
            // Surface the count in non-CPS mode too — Srikanth's "open
            // connection on the client" ask.
            println!(
                "  Connections opened:   {} ({} conn/s avg)",
                results.tcp_connect_samples.to_string().cyan(),
                format!("{:.1}", results.tcp_connect_samples as f64 / duration_secs.max(1) as f64)
                    .cyan(),
            );

            // Issue #79 round 8 — Srikanth saw 7425 connections opened with
            // --vus 5, expecting ~5 (one per VU under pooled reuse). When
            // `tcp_connect_samples` is much larger than `vus_max`, the target
            // is closing the socket between requests (proxy upstream pool
            // disabled, server `Connection: close`, etc). Without --cps,
            // ratios > 5× indicate connection reuse isn't happening.
            if results.vus_max > 0 {
                let reuse_ratio = results.tcp_connect_samples as f64 / results.vus_max as f64;
                if reuse_ratio > 5.0 {
                    println!(
                        "  {}: {:.0}× more sockets opened than concurrent VUs — \
                         the target is closing connections (proxy pool disabled, \
                         `Connection: close`, or short upstream idle timeout).",
                        "Connection reuse NOT detected".yellow().bold(),
                        reuse_ratio,
                    );
                }
            }
        }
        // Print TCP/TLS timing whenever the avg is non-zero. Don't gate on
        // samples count — k6's Trend metric exposes avg/max in summary.json
        // but not count, so the count check was always false even when k6
        // had real samples. Issue #79 round 6 follow-up.
        if results.tcp_connect_avg_ms > 0.0 || results.tcp_connect_max_ms > 0.0 {
            println!(
                "  TCP connect:          avg {:.2}ms, max {:.2}ms",
                results.tcp_connect_avg_ms, results.tcp_connect_max_ms,
            );
        }
        if results.tls_handshake_avg_ms > 0.0 || results.tls_handshake_max_ms > 0.0 {
            println!(
                "  TLS handshake:        avg {:.2}ms, max {:.2}ms",
                results.tls_handshake_avg_ms, results.tls_handshake_max_ms,
            );
        }
        // Peak concurrent VUs — the upper bound on simultaneously-open
        // connections from the client. For HTTP/1.1 each VU holds at
        // most one socket; for HTTP/2 with multiplexing this is the
        // bound on streams, not sockets. Surface it as an open-connection
        // ceiling so users can sanity-check against the server's
        // `connections_open` gauge.
        if results.vus_max > 0
            && (cps_mode || results.tcp_connect_samples > 0 || results.tcp_connect_avg_ms > 0.0)
        {
            println!(
                "  Peak concurrent VUs:  {} (max open conns from client side)",
                results.vus_max.to_string().cyan(),
            );
        }

        // Issue #79 round 10 — iteration coverage. When --rps is supplied and
        // the spec has many operations per iteration, k6 may cancel iterations
        // mid-walk if the duration ends before a full pass completes. Surface
        // "iterations completed" alongside operation count so users see what
        // fraction of the spec was actually exercised.
        if results.iterations_completed > 0 {
            if let Some(num_ops) = num_operations {
                let expected_reqs_per_iter = num_ops as u64;
                let full_iter_reqs =
                    results.iterations_completed.saturating_mul(expected_reqs_per_iter);
                let partial_iter_reqs = results.total_requests.saturating_sub(full_iter_reqs);
                println!(
                    "  Iterations:           {} complete × {} ops = {} ops fully exercised",
                    results.iterations_completed.to_string().cyan(),
                    num_ops.to_string().cyan(),
                    full_iter_reqs.to_string().cyan(),
                );
                if partial_iter_reqs > 0 && num_ops > 1 {
                    println!(
                        "                        {} extra request(s) from a partially-completed \
                         iteration — duration ended mid-walk; not every op was hit on the last pass.",
                        partial_iter_reqs.to_string().yellow(),
                    );
                }
            } else {
                println!(
                    "  Iterations:           {} complete",
                    results.iterations_completed.to_string().cyan(),
                );
            }
        }

        // Issue #79 — server-injected chaos signals (latency / jitter / faults)
        // observed from MockForge response headers. Surfaces the slice of
        // total wire time that came from the chaos middleware vs the system
        // under test.
        if results.server_injected_latency_samples > 0
            || results.server_injected_jitter_samples > 0
            || results.server_reported_faults > 0
        {
            println!("\n{}", "Server-Injected (chaos):".bold());
            if results.server_injected_latency_samples > 0 {
                println!(
                    "  Latency samples:      {} (avg {:.2}ms, max {:.2}ms)",
                    results.server_injected_latency_samples.to_string().cyan(),
                    results.server_injected_latency_avg_ms,
                    results.server_injected_latency_max_ms,
                );
            }
            if results.server_injected_jitter_samples > 0 {
                println!(
                    "  Jitter samples:       {} (avg {:.2}ms)",
                    results.server_injected_jitter_samples.to_string().cyan(),
                    results.server_injected_jitter_avg_ms,
                );
            }
            if results.server_reported_faults > 0 {
                println!(
                    "  Fault-marked resps:   {}",
                    results.server_reported_faults.to_string().cyan(),
                );
            }
        }

        println!("\n{}", "=".repeat(60).bright_green());
    }

    /// Print header information
    pub fn print_header(
        spec_file: &str,
        target: &str,
        num_operations: usize,
        scenario: &str,
        duration_secs: u64,
    ) {
        println!("\n{}\n", "MockForge Bench - Load Testing Mode".bright_green().bold());
        println!("{}", "".repeat(60).bright_black());

        println!("{}: {}", "Specification".bold(), spec_file.cyan());
        println!("{}: {}", "Target".bold(), target.cyan());
        println!("{}: {} endpoints", "Operations".bold(), num_operations.to_string().cyan());
        println!("{}: {}", "Scenario".bold(), scenario.cyan());
        println!("{}: {}s", "Duration".bold(), duration_secs.to_string().cyan());

        println!("{}\n", "".repeat(60).bright_black());
    }

    /// Print progress message
    pub fn print_progress(message: &str) {
        println!("{} {}", "".bright_green().bold(), message);
    }

    /// Print error message
    pub fn print_error(message: &str) {
        eprintln!("{} {}", "".bright_red().bold(), message.red());
    }

    /// Print success message
    pub fn print_success(message: &str) {
        println!("{} {}", "".bright_green().bold(), message.green());
    }

    /// Print warning message
    pub fn print_warning(message: &str) {
        println!("{} {}", "".bright_yellow().bold(), message.yellow());
    }

    /// Print multi-target summary
    pub fn print_multi_target_summary(results: &AggregatedResults) {
        println!("\n{}", "=".repeat(60).bright_green());
        println!("{}", "Multi-Target Load Test Complete! ✓".bright_green().bold());
        println!("{}\n", "=".repeat(60).bright_green());

        println!("{}", "Overall Summary:".bold());
        println!("  Total Targets:        {}", results.total_targets.to_string().cyan());
        println!(
            "  Successful:           {} ({}%)",
            results.successful_targets.to_string().green(),
            format!(
                "{:.1}",
                (results.successful_targets as f64 / results.total_targets as f64) * 100.0
            )
            .green()
        );
        println!(
            "  Failed:               {} ({}%)",
            results.failed_targets.to_string().red(),
            format!(
                "{:.1}",
                (results.failed_targets as f64 / results.total_targets as f64) * 100.0
            )
            .red()
        );

        println!("\n{}", "Aggregated Metrics:".bold());
        println!(
            "  Total Requests:       {}",
            results.aggregated_metrics.total_requests.to_string().cyan()
        );
        println!(
            "  Failed Requests:      {} ({}%)",
            results.aggregated_metrics.total_failed_requests.to_string().red(),
            format!("{:.2}", results.aggregated_metrics.error_rate).red()
        );
        println!(
            "  Total RPS:            {} req/s",
            format!("{:.1}", results.aggregated_metrics.total_rps).cyan()
        );
        println!(
            "  Avg RPS/target:       {} req/s",
            format!("{:.1}", results.aggregated_metrics.avg_rps).cyan()
        );
        println!(
            "  Total VUs:            {}",
            results.aggregated_metrics.total_vus_max.to_string().cyan()
        );
        println!(
            "  Avg Response Time:    {}ms",
            format!("{:.2}", results.aggregated_metrics.avg_duration_ms).cyan()
        );
        println!(
            "  p95 Response Time:    {}ms",
            format!("{:.2}", results.aggregated_metrics.p95_duration_ms).cyan()
        );
        println!(
            "  p99 Response Time:    {}ms",
            format!("{:.2}", results.aggregated_metrics.p99_duration_ms).cyan()
        );

        // Issue #79 round 12 — multi-target was missing the connection /
        // iteration counters that single-target runs surface. Aggregate
        // across targets and print only when k6 actually opened sockets
        // / completed iterations on at least one target.
        if results.aggregated_metrics.total_connections_opened > 0 {
            println!(
                "  Total Connections:    {}",
                results.aggregated_metrics.total_connections_opened.to_string().cyan()
            );
            if results.aggregated_metrics.total_vus_max > 0 {
                let reuse_ratio = results.aggregated_metrics.total_connections_opened as f64
                    / results.aggregated_metrics.total_vus_max as f64;
                if reuse_ratio > 5.0 {
                    println!(
                        "  {}: {:.0}× more sockets opened than concurrent VUs across all targets — \
                         at least one target is closing connections (proxy pool disabled, \
                         `Connection: close`, or short upstream idle timeout).",
                        "Connection reuse NOT detected".yellow().bold(),
                        reuse_ratio,
                    );
                }
            }
        }
        if results.aggregated_metrics.total_iterations_completed > 0 {
            println!(
                "  Total Iterations:     {} complete (sum across all targets)",
                results.aggregated_metrics.total_iterations_completed.to_string().cyan()
            );
        }

        // Show per-target summary
        let print_target = |result: &crate::parallel_executor::TargetResult| {
            let status = if result.success {
                "".bright_green()
            } else {
                "".bright_red()
            };
            println!("  {} {}", status, result.target_url.cyan());
            if result.success {
                println!(
                    "      Requests: {}  RPS: {}  VUs: {}",
                    result.results.total_requests.to_string().white(),
                    format!("{:.1}", result.results.rps).white(),
                    result.results.vus_max.to_string().white(),
                );
                println!(
                    "      Latency: min={:.1}ms avg={:.1}ms med={:.1}ms p90={:.1}ms p95={:.1}ms p99={:.1}ms max={:.1}ms",
                    result.results.min_duration_ms,
                    result.results.avg_duration_ms,
                    result.results.med_duration_ms,
                    result.results.p90_duration_ms,
                    result.results.p95_duration_ms,
                    result.results.p99_duration_ms,
                    result.results.max_duration_ms,
                );
                // Issue #79 round 12 — per-target connection/iteration counts
                // (missing previously in multi-target output).
                if result.results.tcp_connect_samples > 0 || result.results.iterations_completed > 0
                {
                    println!(
                        "      Connections: {}  Iterations: {}",
                        result.results.tcp_connect_samples.to_string().white(),
                        result.results.iterations_completed.to_string().white(),
                    );
                }
            }
            if let Some(error) = &result.error {
                println!("      Error: {}", error.red());
            }
        };

        if results.total_targets <= 20 {
            println!("\n{}", "Per-Target Results:".bold());
            for result in &results.target_results {
                print_target(result);
            }
        } else {
            // Show top 10 and bottom 10
            println!("\n{}", "Top 10 Targets (by requests):".bold());
            let mut sorted_results = results.target_results.clone();
            sorted_results.sort_by_key(|r| r.results.total_requests);
            sorted_results.reverse();

            for result in sorted_results.iter().take(10) {
                print_target(result);
            }

            println!("\n{}", "Bottom 10 Targets:".bold());
            for result in sorted_results.iter().rev().take(10) {
                print_target(result);
            }
        }

        println!("\n{}", "=".repeat(60).bright_green());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_terminal_reporter_creation() {
        let _reporter = TerminalReporter;
    }
}