# Frontend Result Metrics Specification

A practical specification for rendering backend telemetry into rich visual components in Svelte 5.

---

## 1. Response Data Mapping

All endpoints return a unified payload. Components consume fields as follows:

| Component | Backend Source Field | Keys Used |
| :--- | :--- | :--- |
| **Executive Cards** | `metrics.latency`, `metrics.dataprocessed` | `count`, `p50`, `p99`, `sent`, `received` |
| **Latency Profiler** | `metrics.latency` | `min`, `p50`, `p75`, `p90`, `p95`, `p99`, `max`, `average` |
| **Diagnostics & Errors**| `data`, `metrics.summary` | Status codes, error messages, request/response bodies |
| **Cycle Progression** | `metrics.cycles` *(Concurrency only)* | `requestcyclenum`, `currentConcurrency`, `successfulrequests`, `totalrequestfailed`, `totalrequestssent` |
| **Sequence Waterfall** | SSE stream: `checkpoint` events *(Sequence only)*| `jobid`, step name, step status, step duration |

---

## 2. Component Specifications

### 2.1. Executive KPI Summary Cards (Universal)
Mounted at the top of the results view for all three engines.

* **Health Badge:**
  * 🟢 **HEALTHY:** Error rate = 0% AND `p99` < 3× `p50`.
  * 🟡 **DEGRADED:** Error rate between 0.1% – 5% OR `p99` ≥ 3× `p50`.
  * 🔴 **CRITICAL:** Error rate > 5%.
* **Requests & Success Rate:**
  * Displays total requests count and `Success %` calculated as `(successful / total) * 100`.
* **Latency Divergence:**
  * Displays median (`p50`) alongside tail latency (`p99`).
  * Shows **Divergence Multiplier**: `p99 / p50` (e.g. `4.2× slower for p99`).
* **Bandwidth & Payload Overhead:**
  * Displays formatted transfer: bytes $\to$ KB/MB (`sent` / `received`).
  * Displays average payload size per request: `sent / count` and `received / count`.

---

## 2.2. Latency Percentile Profiler Chart (Universal)
Exposes tail latency anomalies and SLA compliance across all test runs.

* **Chart Type:** Spline area chart or connected step curve.
* **X-Axis:** `Min` → `p50` → `p75` → `p90` → `p95` → `p99` → `Max`.
* **Y-Axis:** Response duration in milliseconds (`ms`).
* **Color Zones:**
  * Green gradient from `Min` to `p75`.
  * Amber gradient between `p75` and `p95`.
  * Red gradient from `p95` to `Max`.
* **Target SLA Overlay:**
  * A horizontal reference line (configurable threshold, default `300ms`).
  * Breaches visually highlight where the curve crosses the SLA line with an alert badge (e.g., *"SLA breached at p92"*).

---

## 2.3. Concurrency Cycle Progression Chart (`metrics.cycles` Active)
Renders when `metrics.cycles` is non-empty. Visualizes system degradation as load ramps up.

* **Chart Layout (Dual-Axis Combo):**
  * **X-Axis:** Sequential cycle number and active concurrency level: `Cycle 1 (10 VUs)`, `Cycle 2 (25 VUs)`, etc.
  * **Left Y-Axis (Stacked Bars):**
    * 🟩 Green segment: `successfulrequests`.
    * 🟥 Red segment: `totalrequestfailed`.
  * **Right Y-Axis (Line Graph):**
    * Line tracking `currentConcurrency` ramp or cycle failure percentage.
* **Automated Breaking Point Callout:**
  * Triggers on the earliest cycle where:
    * Cycle failure rate exceeds 2%, OR
    * Concurrency increases but successful request count plateaus/drops.
  * Callout banner: *"Saturation detected at Cycle 4 (Concurrency: 150) — Failure rate spiked to 14.2%"*.
* **Interactive Filtering:**
  * Clicking any cycle bar filters the **Diagnostics & Error Table** below to show only failures that occurred in that specific cycle.

---

## 2.4. Cycle-over-Cycle Delta Comparator (Concurrency Test)
Allows side-by-side comparison between any two cycles (e.g., baseline Cycle 1 vs peak Cycle 5):

* **Selector:** Dropdowns for Cycle $A$ and Cycle $B$.
* **Delta Cards:**
  * **$\Delta$ Concurrency:** `(Concurrency B - Concurrency A)` (+% increase).
  * **$\Delta$ Failure Rate:** `(Failure Rate B - Failure Rate A)` (+% point increase).
  * **$\Delta$ Throughput:** Change in successful completions per batch.
  * **$\Delta$ Tail Impact:** Flags if failures began emerging between the selected cycles.

---

## 2.5. Sequence Real-Time Waterfall (Sequence Test with SSE)
Renders during `/api/sequencetest` execution using the SSE stream (`/api/sequencetest/events/:jobid`):

* **Live Step Pipeline:**
  * Displays sequential steps as horizontal timeline bars.
  * Step states: ⏳ Pending, 🔄 Running (animated pulse), ✅ Passed, ❌ Failed.
* **Duration Breakdown:**
  * Bar width proportional to step duration relative to total sequence time.
  * Immediately identifies the bottleneck step in multi-stage workflows.
* **Transition to Final Metrics:**
  * Once the sequence finishes or aborts, cumulative metrics automatically populate the universal Executive Cards and Latency Profiler.

---

## 2.6. Diagnostics & Failure Triage (Universal)
Located at the base of the dashboard, parsing errors and HTTP statuses from `data`:

* **HTTP Status Distribution Pills:**
  * Pill badges displaying counts: `2xx Success`, `4xx Client Errors`, `5xx Server Errors`.
* **Collapsible Failure Triage Rows:**
  * Each failed request shows:
    * HTTP Status code and method.
    * Target endpoint URL.
    * Error message or timeout reason.
    * Response body snippet (collapsible JSON preview).

---

## 3. Formulas & Calculation Reference

| Metric | Calculation | Purpose |
| :--- | :--- | :--- |
| **Success Rate** | `(successful / total) * 100` | Run health percentage |
| **Failure Rate** | `(failed / total) * 100` | Error percentage |
| **Divergence Multiplier** | `p99 / p50` | Tail latency penalty |
| **Average Sent Payload** | `dataprocessed.sent / count` | Outbound bytes per request |
| **Average Recv Payload** | `dataprocessed.received / count` | Inbound bytes per request |
| **Cycle Error Rate** | `(totalrequestfailed / totalrequestssent) * 100` | Per-cycle degradation |
| **Breaking Point Trigger** | First cycle with `failure_rate > 2%` or `success_count_delta <= 0` with `concurrency_delta > 0` | Auto-detects saturation |
