Skip to main content

mockforge_bench/
executor.rs

1//! k6 execution and output handling
2
3use crate::error::{BenchError, Result};
4use indicatif::{ProgressBar, ProgressStyle};
5use std::path::Path;
6use std::process::Stdio;
7use std::sync::Arc;
8use tokio::io::{AsyncBufReadExt, BufReader};
9use tokio::process::Command as TokioCommand;
10
11/// Extract a `MOCKFORGE_<KIND>:` JSON payload from a k6 output line.
12///
13/// k6 emits these via `console.log`, which goes through one of two paths
14/// depending on the runner config:
15/// - **Raw**: `MOCKFORGE_EXCHANGE:{"check":"...", ...}` straight to stdout.
16/// - **Logfmt**: `time="..." level=info msg="MOCKFORGE_EXCHANGE:{...}" source=console`
17///   where the JSON's `"` are escaped as `\"` and `\` as `\\` so it fits
18///   inside the `msg="..."` field.
19///
20/// Round 46 (#79) — Srikanth on 0.3.190: a multipart upload landed `[]`
21/// in `conformance-requests.json` even though `MOCKFORGE_EXCHANGE:` was
22/// present in the k6 log. Root cause: the previous parser used a naive
23/// `replace("\\\\", "\\").replace("\\\"", "\"")` chain. With binary
24/// multipart bytes the JSON content includes sequences like `\\"`
25/// (literal backslash followed by literal quote inside a JSON string),
26/// which logfmt-escapes to `\\\\\"`. The replace chain processed `\\`
27/// → `\` first, leaving `\\\"`, then `\"` → `"`, mangling the JSON.
28/// Replaced with a single character walk that consumes one logfmt
29/// escape at a time. Also rewrote the suffix-strip to scan for the
30/// matching closing `"` of the `msg="..."` field instead of a
31/// fixed-string suffix so we tolerate any trailing logfmt fields k6
32/// might add.
33fn extract_mockforge_marker_json(line: &str, marker: &str) -> Option<String> {
34    let start = line.find(marker)?;
35    let json_start = start + marker.len();
36    let rest = &line[json_start..];
37
38    // Is this the logfmt-wrapped form? The `msg="` opener sits 5 bytes
39    // before the marker. (Plain `msg=MOCKFORGE_...` would also be valid
40    // logfmt for a value with no spaces, but k6 always quote-wraps.)
41    let is_logfmt = start >= 5 && line.as_bytes().get(start - 5..start) == Some(b"msg=\"");
42    if is_logfmt {
43        // Walk forward until the unescaped closing `"` of msg=. Inside
44        // the field, `\\` is one escaped backslash and `\"` is one
45        // escaped quote — those bytes belong to the JSON content. Any
46        // unescaped `"` is the field terminator.
47        let bytes = rest.as_bytes();
48        let mut i = 0;
49        let mut out = String::with_capacity(rest.len());
50        while i < bytes.len() {
51            let b = bytes[i];
52            if b == b'"' {
53                // End of msg= field.
54                return Some(out);
55            }
56            if b == b'\\' && i + 1 < bytes.len() {
57                let next = bytes[i + 1];
58                match next {
59                    b'"' => out.push('"'),
60                    b'\\' => out.push('\\'),
61                    // Other escapes (`\n`, `\r`, `\t`, `\uXXXX`) are
62                    // PART of the JSON content — keep them verbatim so
63                    // serde_json::from_str interprets them.
64                    other => {
65                        out.push('\\');
66                        out.push(other as char);
67                    }
68                }
69                i += 2;
70                continue;
71            }
72            // Non-ASCII multi-byte UTF-8 codepoint or plain ASCII char.
73            // `rest` is a `&str` so we can rely on UTF-8 boundaries.
74            let ch_start = i;
75            // Advance i past the codepoint.
76            i += 1;
77            while i < bytes.len() && (bytes[i] & 0b1100_0000) == 0b1000_0000 {
78                i += 1;
79            }
80            out.push_str(&rest[ch_start..i]);
81        }
82        // Reached EOL without a closing quote — return what we have so
83        // the downstream parser can decide whether to keep it.
84        if out.is_empty() {
85            None
86        } else {
87            Some(out)
88        }
89    } else {
90        // Raw form: rest of the line is the JSON, possibly with trailing
91        // whitespace. No escape processing needed.
92        let trimmed = rest.trim();
93        if trimmed.is_empty() {
94            None
95        } else {
96            Some(trimmed.to_string())
97        }
98    }
99}
100
101/// Extract `MOCKFORGE_EXCHANGE:` JSON payload from a k6 output line (--export-requests).
102fn extract_exchange_json(line: &str) -> Option<String> {
103    extract_mockforge_marker_json(line, "MOCKFORGE_EXCHANGE:")
104}
105
106/// Extract `MOCKFORGE_FAILURE:` JSON payload from a k6 output line.
107fn extract_failure_json(line: &str) -> Option<String> {
108    extract_mockforge_marker_json(line, "MOCKFORGE_FAILURE:")
109}
110
111/// Round 47 (#79) — extract `MOCKFORGE_NETWORK_EVENT:` JSON payload.
112/// Emitted by the k6 captureExchange when `res.status === 0`, capturing
113/// the wire-level failure with a classified `kind`.
114fn extract_network_event_json(line: &str) -> Option<String> {
115    extract_mockforge_marker_json(line, "MOCKFORGE_NETWORK_EVENT:")
116}
117
118/// k6 executor
119pub struct K6Executor {
120    k6_path: String,
121    /// Comma-joined IPs/ranges/CIDRs forwarded to `k6 run --local-ips`.
122    /// Empty → flag omitted. Populated by callers that pass through the
123    /// CLI's `--source-ip`; lets a VU make requests from one of several
124    /// bound interfaces (k6 supports this natively, contrary to my
125    /// round-22 warning).
126    local_ips: String,
127    /// Round 56 (#79) — when true, set `K6_DISCARD_RESPONSE_BODIES=true` so k6
128    /// does not buffer every response body in memory. On long, high-concurrency
129    /// multi-target runs (Srikanth on 0.3.203 saw `k6 ... signal: 9 (SIGKILL)`,
130    /// i.e. the OOM killer) the buffered bodies plus k6's own metric
131    /// accumulation exhaust RAM. Plain load only checks status codes, so
132    /// dropping the bodies is safe there.
133    discard_response_bodies: bool,
134}
135
136impl K6Executor {
137    /// Create a new k6 executor
138    pub fn new() -> Result<Self> {
139        let k6_path = which::which("k6")
140            .map_err(|_| BenchError::K6NotFound)?
141            .to_string_lossy()
142            .to_string();
143
144        Ok(Self {
145            k6_path,
146            local_ips: String::new(),
147            discard_response_bodies: false,
148        })
149    }
150
151    /// Set the `--local-ips` value for subsequent k6 invocations.
152    /// Accepts a comma-joined list of IPs, ranges (`10.0.0.1-10.0.0.5`),
153    /// and/or CIDRs (`192.168.0.0/24`) - same syntax k6 expects.
154    pub fn with_local_ips(mut self, local_ips: impl Into<String>) -> Self {
155        self.local_ips = local_ips.into();
156        self
157    }
158
159    /// Round 56 (#79) — enable `K6_DISCARD_RESPONSE_BODIES` so k6 does not hold
160    /// response bodies in memory. Use for plain load runs (status-only checks);
161    /// do NOT use where the script inspects/extracts response bodies.
162    pub fn with_discard_response_bodies(mut self, discard: bool) -> Self {
163        self.discard_response_bodies = discard;
164        self
165    }
166
167    /// Check if k6 is installed
168    pub fn is_k6_installed() -> bool {
169        which::which("k6").is_ok()
170    }
171
172    /// Get k6 version
173    pub async fn get_version(&self) -> Result<String> {
174        let output = TokioCommand::new(&self.k6_path)
175            .arg("version")
176            .output()
177            .await
178            .map_err(|e| BenchError::K6ExecutionFailed(e.to_string()))?;
179
180        if !output.status.success() {
181            return Err(BenchError::K6ExecutionFailed("Failed to get k6 version".to_string()));
182        }
183
184        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
185    }
186
187    /// Execute a k6 script.
188    ///
189    /// `api_port` — when set, overrides k6's default API server address (`localhost:6565`)
190    /// to `localhost:<api_port>`. This prevents "address already in use" errors when
191    /// running multiple k6 instances in parallel (e.g., multi-target bench).
192    /// Pass `None` for single-target runs (uses k6's default).
193    pub async fn execute(
194        &self,
195        script_path: &Path,
196        output_dir: Option<&Path>,
197        verbose: bool,
198    ) -> Result<K6Results> {
199        self.execute_with_port(script_path, output_dir, verbose, None).await
200    }
201
202    /// Execute a k6 script with an optional custom API server port.
203    pub async fn execute_with_port(
204        &self,
205        script_path: &Path,
206        output_dir: Option<&Path>,
207        verbose: bool,
208        api_port: Option<u16>,
209    ) -> Result<K6Results> {
210        println!("Starting load test...\n");
211
212        let mut cmd = TokioCommand::new(&self.k6_path);
213        cmd.arg("run");
214
215        // When running multiple k6 instances in parallel, each needs its own API server port
216        // to avoid "bind: address already in use" on the default port 6565.
217        if let Some(port) = api_port {
218            cmd.arg("--address").arg(format!("localhost:{}", port));
219        }
220
221        // `--local-ips` rotates each VU through a pool of source IPs that
222        // must already be bound on the host (CIDRs/ranges accepted). This
223        // gives the k6 path the same source-IP coverage as the native
224        // self-test driver's `--source-ip`.
225        if !self.local_ips.is_empty() {
226            cmd.arg("--local-ips").arg(&self.local_ips);
227        }
228
229        // Round 56 (#79) — drop response bodies to bound k6's memory on long,
230        // high-concurrency runs (guards against the SIGKILL/OOM Srikanth hit).
231        if self.discard_response_bodies {
232            cmd.env("K6_DISCARD_RESPONSE_BODIES", "true");
233        }
234
235        // summary.json is written by the k6 script's handleSummary() function
236        // (relative to CWD, set to output_dir below). We no longer use
237        // --summary-export as it's deprecated in newer k6 versions and
238        // conflicts with handleSummary when both try to write the same file.
239
240        // Add verbosity
241        if verbose {
242            cmd.arg("--verbose");
243        }
244
245        // Use absolute path for the script so it's found regardless of CWD.
246        let abs_script =
247            std::fs::canonicalize(script_path).unwrap_or_else(|_| script_path.to_path_buf());
248        cmd.arg(&abs_script);
249
250        // Set working directory to output dir so handleSummary's relative
251        // "summary.json" path lands next to the script.
252        if let Some(dir) = output_dir {
253            cmd.current_dir(dir);
254        }
255
256        cmd.stdout(Stdio::piped());
257        cmd.stderr(Stdio::piped());
258
259        let mut child = cmd.spawn().map_err(|e| BenchError::K6ExecutionFailed(e.to_string()))?;
260
261        let stdout = child
262            .stdout
263            .take()
264            .ok_or_else(|| BenchError::K6ExecutionFailed("Failed to capture stdout".to_string()))?;
265
266        let stderr = child
267            .stderr
268            .take()
269            .ok_or_else(|| BenchError::K6ExecutionFailed("Failed to capture stderr".to_string()))?;
270
271        // Stream output
272        let stdout_reader = BufReader::new(stdout);
273        let stderr_reader = BufReader::new(stderr);
274
275        let mut stdout_lines = stdout_reader.lines();
276        let mut stderr_lines = stderr_reader.lines();
277
278        // Create progress indicator
279        let spinner = ProgressBar::new_spinner();
280        spinner.set_style(
281            ProgressStyle::default_spinner().template("{spinner:.green} {msg}").unwrap(),
282        );
283        spinner.set_message("Running load test...");
284
285        // Collect failure details from k6's console.log output
286        // k6 may emit console.log to either stdout or stderr depending on version/config
287        let failure_details: Arc<tokio::sync::Mutex<Vec<String>>> =
288            Arc::new(tokio::sync::Mutex::new(Vec::new()));
289        let fd_stdout = Arc::clone(&failure_details);
290        let fd_stderr = Arc::clone(&failure_details);
291
292        // Collect request/response exchanges for --export-requests
293        let exchange_details: Arc<tokio::sync::Mutex<Vec<String>>> =
294            Arc::new(tokio::sync::Mutex::new(Vec::new()));
295        let ex_stdout = Arc::clone(&exchange_details);
296        let ex_stderr = Arc::clone(&exchange_details);
297
298        // Round 47 (#79) — collect wire-level network events the
299        // k6 script emits on status=0 (connect / tls / timeout). Same
300        // shape as the native + self-test sinks so we can write a
301        // unified `conformance-network-events.json`.
302        let network_events: Arc<tokio::sync::Mutex<Vec<String>>> =
303            Arc::new(tokio::sync::Mutex::new(Vec::new()));
304        let ne_stdout = Arc::clone(&network_events);
305        let ne_stderr = Arc::clone(&network_events);
306
307        // Collect all k6 output for saving to a log file
308        let log_lines: Arc<tokio::sync::Mutex<Vec<String>>> =
309            Arc::new(tokio::sync::Mutex::new(Vec::new()));
310        let log_stdout = Arc::clone(&log_lines);
311        let log_stderr = Arc::clone(&log_lines);
312
313        // Read stdout lines, capturing MOCKFORGE_FAILURE / MOCKFORGE_EXCHANGE / MOCKFORGE_NETWORK_EVENT markers
314        let stdout_handle = tokio::spawn(async move {
315            while let Ok(Some(line)) = stdout_lines.next_line().await {
316                log_stdout.lock().await.push(format!("[stdout] {}", line));
317                if let Some(json_str) = extract_failure_json(&line) {
318                    fd_stdout.lock().await.push(json_str);
319                } else if let Some(json_str) = extract_exchange_json(&line) {
320                    ex_stdout.lock().await.push(json_str);
321                } else if let Some(json_str) = extract_network_event_json(&line) {
322                    ne_stdout.lock().await.push(json_str);
323                } else {
324                    spinner.set_message(line.clone());
325                    if !line.is_empty() && !line.contains("running") && !line.contains("default") {
326                        println!("{}", line);
327                    }
328                }
329            }
330            spinner.finish_and_clear();
331        });
332
333        // Read stderr lines, capturing MOCKFORGE_FAILURE / MOCKFORGE_EXCHANGE / MOCKFORGE_NETWORK_EVENT markers
334        let stderr_handle = tokio::spawn(async move {
335            while let Ok(Some(line)) = stderr_lines.next_line().await {
336                if !line.is_empty() {
337                    log_stderr.lock().await.push(format!("[stderr] {}", line));
338                    if let Some(json_str) = extract_failure_json(&line) {
339                        fd_stderr.lock().await.push(json_str);
340                    } else if let Some(json_str) = extract_exchange_json(&line) {
341                        ex_stderr.lock().await.push(json_str);
342                    } else if let Some(json_str) = extract_network_event_json(&line) {
343                        ne_stderr.lock().await.push(json_str);
344                    } else {
345                        eprintln!("{}", line);
346                    }
347                }
348            }
349        });
350
351        // Wait for completion
352        let status =
353            child.wait().await.map_err(|e| BenchError::K6ExecutionFailed(e.to_string()))?;
354
355        // Wait for both reader tasks to finish processing all lines
356        let _ = stdout_handle.await;
357        let _ = stderr_handle.await;
358
359        // k6 exit code 99 = thresholds crossed. The test DID run and summary.json
360        // should still be present. Only treat non-99 failures as hard errors.
361        let exit_code = status.code().unwrap_or(-1);
362        if !status.success() && exit_code != 99 {
363            return Err(BenchError::K6ExecutionFailed(format!(
364                "k6 exited with status: {}",
365                status
366            )));
367        }
368        if exit_code == 99 {
369            tracing::warn!("k6 thresholds crossed (exit code 99) — results will still be parsed");
370        }
371
372        // Write failure details to file if any were captured
373        if let Some(dir) = output_dir {
374            let details = failure_details.lock().await;
375            if !details.is_empty() {
376                let failure_path = dir.join("conformance-failure-details.json");
377                let parsed: Vec<serde_json::Value> =
378                    details.iter().filter_map(|s| serde_json::from_str(s).ok()).collect();
379                if let Ok(json) = serde_json::to_string_pretty(&parsed) {
380                    let _ = std::fs::write(&failure_path, json);
381                }
382            }
383
384            // Write exchange details (--export-requests) if any were captured
385            let exchanges = exchange_details.lock().await;
386            if !exchanges.is_empty() {
387                let exchange_path = dir.join("conformance-requests.json");
388                let parsed: Vec<serde_json::Value> =
389                    exchanges.iter().filter_map(|s| serde_json::from_str(s).ok()).collect();
390                if let Ok(json) = serde_json::to_string_pretty(&parsed) {
391                    let _ = std::fs::write(&exchange_path, json);
392                    tracing::info!(
393                        "Exported {} request/response pairs to {}",
394                        parsed.len(),
395                        exchange_path.display()
396                    );
397                }
398            }
399
400            // Round 47 (#79) — write the wire-level events sink. We
401            // ALWAYS write the file (empty array when nothing failed)
402            // so a caller can tell "everything succeeded" from "nobody
403            // looked" at a glance.
404            let net_events = network_events.lock().await;
405            let net_path = dir.join("conformance-network-events.json");
406            let parsed: Vec<serde_json::Value> =
407                net_events.iter().filter_map(|s| serde_json::from_str(s).ok()).collect();
408            if let Ok(json) = serde_json::to_string_pretty(&parsed) {
409                let _ = std::fs::write(&net_path, json);
410                if !parsed.is_empty() {
411                    tracing::warn!(
412                        "Recorded {} wire-level network event(s) to {}",
413                        parsed.len(),
414                        net_path.display()
415                    );
416                }
417            }
418
419            // Save full k6 output to a log file for debugging
420            let lines = log_lines.lock().await;
421            if !lines.is_empty() {
422                let log_path = dir.join("k6-output.log");
423                let _ = std::fs::write(&log_path, lines.join("\n"));
424                println!("k6 output log saved to: {}", log_path.display());
425            }
426        }
427
428        // Parse results if output directory was specified
429        let results = if let Some(dir) = output_dir {
430            Self::parse_results(dir)?
431        } else {
432            K6Results::default()
433        };
434
435        Ok(results)
436    }
437
438    /// Parse k6 results from JSON output
439    fn parse_results(output_dir: &Path) -> Result<K6Results> {
440        let summary_path = output_dir.join("summary.json");
441
442        if !summary_path.exists() {
443            return Ok(K6Results::default());
444        }
445
446        let content = std::fs::read_to_string(summary_path)
447            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
448
449        let json: serde_json::Value = serde_json::from_str(&content)
450            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
451
452        let duration_values = &json["metrics"]["http_req_duration"]["values"];
453
454        let server_latency = &json["metrics"]["mockforge_server_injected_latency_ms"]["values"];
455        let server_jitter = &json["metrics"]["mockforge_server_injected_jitter_ms"]["values"];
456        let server_fault = &json["metrics"]["mockforge_server_fault_total"]["values"]["count"];
457
458        // Issue #79 (round 5) — surface TCP connect / TLS handshake stats and
459        // a connection-rate count for `--cps` runs.
460        //
461        // Round 6 follow-up: k6's `http_req_connecting` Trend doesn't expose a
462        // `count` field in summary.json (only avg/min/med/max/p90/p95), so we
463        // can't use it for "connections opened". The template now feeds a
464        // dedicated Counter, `mockforge_connections_opened`, every time a
465        // request's `res.timings.connecting > 0`. That gives us an accurate
466        // count for both `--cps` (≈ total_requests) and pooled-reuse (≈ vus_max)
467        // runs. The Trend is still useful for the avg/max timing display.
468        let tcp_connecting = &json["metrics"]["http_req_connecting"]["values"];
469        let tls_handshake = &json["metrics"]["http_req_tls_handshaking"]["values"];
470        let mf_conns_opened = &json["metrics"]["mockforge_connections_opened"]["values"]["count"];
471
472        Ok(K6Results {
473            total_requests: json["metrics"]["http_reqs"]["values"]["count"].as_u64().unwrap_or(0),
474            // k6 Rate metric: `passes` = count of non-zero values.
475            // For http_req_failed, non-zero means the request failed.
476            // So `passes` = failed request count, `fails` = successful request count.
477            failed_requests: json["metrics"]["http_req_failed"]["values"]["passes"]
478                .as_u64()
479                .unwrap_or(0),
480            avg_duration_ms: duration_values["avg"].as_f64().unwrap_or(0.0),
481            p95_duration_ms: duration_values["p(95)"].as_f64().unwrap_or(0.0),
482            p99_duration_ms: duration_values["p(99)"].as_f64().unwrap_or(0.0),
483            rps: json["metrics"]["http_reqs"]["values"]["rate"].as_f64().unwrap_or(0.0),
484            vus_max: json["metrics"]["vus_max"]["values"]["value"].as_u64().unwrap_or(0) as u32,
485            min_duration_ms: duration_values["min"].as_f64().unwrap_or(0.0),
486            max_duration_ms: duration_values["max"].as_f64().unwrap_or(0.0),
487            med_duration_ms: duration_values["med"].as_f64().unwrap_or(0.0),
488            p90_duration_ms: duration_values["p(90)"].as_f64().unwrap_or(0.0),
489            server_injected_latency_samples: server_latency["count"].as_u64().unwrap_or(0),
490            server_injected_latency_avg_ms: server_latency["avg"].as_f64().unwrap_or(0.0),
491            server_injected_latency_max_ms: server_latency["max"].as_f64().unwrap_or(0.0),
492            server_injected_jitter_samples: server_jitter["count"].as_u64().unwrap_or(0),
493            server_injected_jitter_avg_ms: server_jitter["avg"].as_f64().unwrap_or(0.0),
494            server_reported_faults: server_fault.as_u64().unwrap_or(0),
495            // Counter from the template, not the Trend's count (which is
496            // absent in k6 summary JSON).
497            tcp_connect_samples: mf_conns_opened.as_u64().unwrap_or(0),
498            tcp_connect_avg_ms: tcp_connecting["avg"].as_f64().unwrap_or(0.0),
499            tcp_connect_max_ms: tcp_connecting["max"].as_f64().unwrap_or(0.0),
500            // TLS handshake Trend has no `count` either; gate display on avg>0.
501            tls_handshake_samples: if tls_handshake["avg"].as_f64().unwrap_or(0.0) > 0.0 {
502                // Use connection count as a proxy — every new TLS session
503                // requires a handshake.
504                mf_conns_opened.as_u64().unwrap_or(0)
505            } else {
506                0
507            },
508            tls_handshake_avg_ms: tls_handshake["avg"].as_f64().unwrap_or(0.0),
509            tls_handshake_max_ms: tls_handshake["max"].as_f64().unwrap_or(0.0),
510            iterations_completed: json["metrics"]["iterations"]["values"]["count"]
511                .as_u64()
512                .unwrap_or(0),
513        })
514    }
515}
516
517impl Default for K6Executor {
518    fn default() -> Self {
519        Self::new().expect("k6 not found")
520    }
521}
522
523/// k6 test results
524#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
525pub struct K6Results {
526    pub total_requests: u64,
527    pub failed_requests: u64,
528    pub avg_duration_ms: f64,
529    pub p95_duration_ms: f64,
530    pub p99_duration_ms: f64,
531    pub rps: f64,
532    pub vus_max: u32,
533    pub min_duration_ms: f64,
534    pub max_duration_ms: f64,
535    pub med_duration_ms: f64,
536    pub p90_duration_ms: f64,
537    /// Issue #79 — client-side visibility into MockForge-injected latency,
538    /// parsed from the `X-Mockforge-Injected-Latency-Ms` response header that
539    /// the chaos middleware sets. Zero when chaos isn't firing or the target
540    /// isn't MockForge.
541    #[serde(default)]
542    pub server_injected_latency_samples: u64,
543    #[serde(default)]
544    pub server_injected_latency_avg_ms: f64,
545    #[serde(default)]
546    pub server_injected_latency_max_ms: f64,
547    #[serde(default)]
548    pub server_injected_jitter_samples: u64,
549    #[serde(default)]
550    pub server_injected_jitter_avg_ms: f64,
551    /// Count of responses that carried an `X-Mockforge-Fault` header.
552    #[serde(default)]
553    pub server_reported_faults: u64,
554    /// Issue #79 (round 5) — TCP connect samples / timing. With `--cps`
555    /// (`noConnectionReuse: true`) k6 records one connect per request, so
556    /// `tcp_connect_samples` equals connections opened. Without `--cps` this
557    /// is typically a small count (k6 reuses pooled connections), so it tells
558    /// you whether reuse was actually happening.
559    #[serde(default)]
560    pub tcp_connect_samples: u64,
561    #[serde(default)]
562    pub tcp_connect_avg_ms: f64,
563    #[serde(default)]
564    pub tcp_connect_max_ms: f64,
565    /// TLS handshake samples / timing — same shape as TCP connect, but only
566    /// non-zero for HTTPS targets.
567    #[serde(default)]
568    pub tls_handshake_samples: u64,
569    #[serde(default)]
570    pub tls_handshake_avg_ms: f64,
571    #[serde(default)]
572    pub tls_handshake_max_ms: f64,
573    /// Issue #79 round 10 — k6 iteration counter from `iterations.values.count`.
574    /// For `constant-arrival-rate` (`--rps`), this is the number of full
575    /// iterations completed within the duration. When `iterations × num_ops`
576    /// is much less than `total_requests`, mid-iteration cancellation truncated
577    /// the run and not every operation in the spec was exercised.
578    #[serde(default)]
579    pub iterations_completed: u64,
580}
581
582impl K6Results {
583    /// Get error rate as a percentage
584    pub fn error_rate(&self) -> f64 {
585        if self.total_requests == 0 {
586            return 0.0;
587        }
588        (self.failed_requests as f64 / self.total_requests as f64) * 100.0
589    }
590
591    /// Get success rate as a percentage
592    pub fn success_rate(&self) -> f64 {
593        100.0 - self.error_rate()
594    }
595}
596
597#[cfg(test)]
598mod tests {
599    use super::*;
600
601    #[test]
602    fn test_k6_results_error_rate() {
603        let results = K6Results {
604            total_requests: 100,
605            failed_requests: 5,
606            avg_duration_ms: 100.0,
607            p95_duration_ms: 200.0,
608            p99_duration_ms: 300.0,
609            ..Default::default()
610        };
611
612        assert_eq!(results.error_rate(), 5.0);
613        assert_eq!(results.success_rate(), 95.0);
614    }
615
616    #[test]
617    fn test_k6_results_zero_requests() {
618        let results = K6Results::default();
619        assert_eq!(results.error_rate(), 0.0);
620    }
621
622    #[test]
623    fn discard_response_bodies_defaults_off_and_builder_flips_it() {
624        // Round 56 (#79) — guards the OOM fix for multi-target load runs.
625        // Default must stay off so body-inspecting paths (extract/conformance)
626        // are unaffected; the builder opts a run in.
627        let exec = K6Executor {
628            k6_path: "k6".to_string(),
629            local_ips: String::new(),
630            discard_response_bodies: false,
631        };
632        assert!(!exec.discard_response_bodies);
633        let exec = exec.with_discard_response_bodies(true);
634        assert!(exec.discard_response_bodies);
635    }
636
637    #[test]
638    fn test_extract_failure_json_raw() {
639        let line = r#"MOCKFORGE_FAILURE:{"check":"test","expected":"status === 200"}"#;
640        let result = extract_failure_json(line).unwrap();
641        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
642        assert_eq!(parsed["check"], "test");
643    }
644
645    #[test]
646    fn test_extract_failure_json_logfmt() {
647        let line = r#"time="2026-01-01T00:00:00Z" level=info msg="MOCKFORGE_FAILURE:{\"check\":\"test\",\"response\":{\"body\":\"{\\\"key\\\":\\\"val\\\"}\"}} " source=console"#;
648        let result = extract_failure_json(line).unwrap();
649        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
650        assert_eq!(parsed["check"], "test");
651        assert_eq!(parsed["response"]["body"], r#"{"key":"val"}"#);
652    }
653
654    #[test]
655    fn test_extract_failure_json_no_marker() {
656        assert!(extract_failure_json("just a regular log line").is_none());
657    }
658
659    /// Round 46 (#79) — regression: Srikanth's multipart upload landed
660    /// `[]` in `conformance-requests.json` because the old
661    /// `replace("\\\\","\\").replace("\\\"","\"")` chain misparsed
662    /// adjacent backslashes inside the JSON body (binary multipart
663    /// bytes encoded as `\\u00XX` etc.). Pin both shapes here.
664    #[test]
665    fn test_extract_exchange_logfmt_with_backslash_escapes() {
666        // A JSON body that contains a JSON-encoded `` (one escape
667        // sequence the validator survives). Logfmt wraps it: each `\`
668        // becomes `\\`, each `"` becomes `\"`.
669        let line = r#"time="2026-06-26T10:00:00Z" level=info msg="MOCKFORGE_EXCHANGE:{\"check\":\"u\",\"request\":{\"body\":\"--bnd\\r\\n\\u001a\"}}" source=console"#;
670        let result = extract_exchange_json(line).unwrap();
671        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
672        assert_eq!(parsed["check"], "u");
673        // The unescape preserves the JSON's `\r\n` and `` so the
674        // downstream consumer can interpret them as JSON escapes.
675        assert_eq!(parsed["request"]["body"], "--bnd\r\n\u{001a}");
676    }
677
678    #[test]
679    fn test_extract_exchange_raw_no_logfmt_wrapping() {
680        let line =
681            r#"MOCKFORGE_EXCHANGE:{"check":"x","request":{"body":""},"response":{"status":200}}"#;
682        let result = extract_exchange_json(line).unwrap();
683        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
684        assert_eq!(parsed["check"], "x");
685        assert_eq!(parsed["response"]["status"], 200);
686    }
687
688    /// The end of `msg="..."` is a single unescaped `"`, not the old
689    /// fixed-string `" source=console`. If k6 ever appends another
690    /// logfmt field (or omits source=), we still get the JSON out.
691    #[test]
692    fn test_extract_exchange_logfmt_tolerates_extra_trailing_fields() {
693        let line = r#"msg="MOCKFORGE_EXCHANGE:{\"check\":\"t\"}" source=console vu=1 iter=0"#;
694        let result = extract_exchange_json(line).unwrap();
695        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
696        assert_eq!(parsed["check"], "t");
697    }
698
699    /// Round 46 — JSON-encoded backslash inside a JSON string (`\\u00XX`
700    /// in the JSON, `\\\\u00XX` in logfmt) must round-trip cleanly.
701    /// The naive `.replace` chain choked on this exact pattern.
702    #[test]
703    fn test_extract_exchange_double_backslash_followed_by_quote() {
704        // JSON content: `\\"x"` is `\` then `"x"`. Logfmt:
705        // `\\\\\"x\"` (4 backslashes + escaped quote + x + escaped quote).
706        let line = r#"msg="MOCKFORGE_EXCHANGE:{\"k\":\"a\\\\\\\"x\\\"\"}" source=console"#;
707        let result = extract_exchange_json(line).unwrap();
708        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
709        assert_eq!(parsed["k"], r#"a\"x""#);
710    }
711}