# Sophia Load Tests

k6 suites that assert the Sophia knowledge/RAG SLOs documented in
`docs/performance/baselines.md §3`.

| Script           | Exercises                                                | SLO gate                                    |
| ---------------- | -------------------------------------------------------- | ------------------------------------------- |
| `search-load.js` | `POST /v1/search`, `POST /v1/ask`                        | search p95 ≤ 500 ms, ask p95 ≤ 800 ms       |
| `graph-load.js`  | `POST /v1/resolution/resolve`, `POST /v1/graph/subgraph` | resolve p95 ≤ 300 ms, subgraph p95 ≤ 600 ms |

## Run locally

```bash
# Core services only — assumes search-api on :3500, knowledge-graph on the same host
docker compose -f docker/docker-compose.dev.yml up -d

# Read-heavy retrieval path
k6 run testing/performance/k6/sophia/search-load.js

# Entity resolution → 2-hop subgraph
k6 run testing/performance/k6/sophia/graph-load.js
```

## Environment variables

| Var                 | Default                   | Purpose                                                  |
| ------------------- | ------------------------- | -------------------------------------------------------- |
| `SOPHIA_SEARCH_URL` | `http://localhost:3500`   | search-api origin                                        |
| `SOPHIA_GRAPH_URL`  | `$SOPHIA_SEARCH_URL`      | knowledge-graph origin (set if deployed separately)      |
| `JWT_SECRET`        | `sophia-load-test-secret` | HS256 secret used to mint per-iteration load-test tokens |
| `SOPHIA_ASK_RATIO`  | `0.3`                     | Fraction of search iterations that follow up with `/ask` |

All stage durations and target RPS are overridable via the env vars declared at
the top of each script (`SOPHIA_SEARCH_STAGE_1_RPS`, etc.) so CI can dial the
load envelope per environment.

## What counts as a regression

A run regresses the Sophia SLO if any of the `thresholds` blocks in the scripts
fail — k6 exits non-zero, which is how CI gates on this. The budgets are the
same numbers encoded in `docs/performance/baselines.md` and in the cross-domain
latency Vitest benchmark; changing one requires updating both.
