# Parallel Runner Design This note scopes Phase 5 Step 1: add multithreaded execution without changing the current CLI, summary JSON, or workflow ABI contracts. ## Goals - scale single-node throughput with worker threads - preserve the current `run` / `validate` behavior and output contracts - keep workflow guests isolated with instance-per-thread ownership - make the single-thread path remain valid as the correctness baseline ## Current Constraints The current runner is intentionally single-threaded: - `src/runner/executor.zig` drives all execution modes from one dispatch loop - `src/runner/context.zig` creates one shared `std.http.Client`, one shared gRPC client, one TLS context, and one shared store for the whole run - `src/template.zig` uses an unsynchronized `VarStore` - `src/workflow/vu.zig` and `src/workflow/host.zig` keep mutable per-VU state in one Wasm instance, cookie jar, PRNG, and variable store - `src/runner/metrics.zig` serializes all metric updates through one mutexed `MetricsAggregator` - `src/runner/control.zig` already exposes process-global atomic stop flags and can stay the cross-thread stop mechanism These constraints are good news for Phase 5 because the mutable workflow state is already grouped per VU. The main work is separating process-wide state from per-worker state and removing the global metrics hot lock. ## Proposed Execution Model Use a coordinator plus worker threads. - The coordinator owns run timing, progress cadence, threshold abort checks, and final summary merge. - Each worker owns: - one HTTP client - one gRPC client - one TLS context / connection pool - one metrics shard - a fixed set of VUs - Workflow VUs stay thread-affine. A Wasm instance is created, used, recreated, and destroyed on one worker only. - `setup.*` data becomes immutable after setup. Workers clone it into per-VU stores and only mutate `vu.*` / `iter.*` locally. ## Scheduling By Mode ### Constant VUs - Assign VU slots to workers up front with a stable `slot % thread_count` mapping. - Each worker runs its own local loop over its assigned slots. - This keeps request order deterministic per VU and avoids work-stealing for the simplest mode. ### Constant Arrival Rate - Keep one coordinator-side arrival ticker. - The coordinator emits iteration tickets into per-worker deques using round-robin or least-lag assignment. - Workers pop local tickets first and optionally steal from neighbors when idle. - Drop accounting stays in the coordinator because it already owns the global schedule. ### Ramping VUs - Pre-allocate the full VU set. - The coordinator updates an active-VU watermark as stages advance. - Workers skip local slots above the current watermark. ## Metrics Plan Replace the global hot mutex with shard-local mutation plus periodic merge. - Introduce `MetricsShard` with the same counters and histograms currently stored in `MetricsWindow`. - Each worker mutates only its local shard with no cross-thread locking. - The coordinator merges shards for: - progress logging - `abort_on_fail` threshold checks - final summary generation The merge path can reuse most of the existing summary code if `MetricsWindow` gains `mergeFrom`. ## Transport And Runtime Ownership - `std.http.Client` should be worker-local, not shared across threads. - The gRPC C++ client wrapper should also be worker-local to avoid hidden cross-thread constraints. - `TlsContext` and connection pools become worker-local because they currently wrap mutable connection state. - WAMR / wasm3 lifecycle remains instance-per-VU. No Wasm instance sharing across threads. ## Workflow Recovery Rules Recovery stays local to the owning worker. - recreate the same VU on the same worker - activate spare VUs from the same worker's local capacity first - report poisoned workers and unhealthy VUs via shard metrics, then merge globally This keeps the existing recovery logic conceptually intact and avoids cross-thread handoff of partially failed guests. ## Rollout Sequence 1. Extract immutable run inputs from `Session` into a coordinator-owned run plan. 2. Add `WorkerContext` with per-worker clients, TLS, and metrics shard. 3. Teach metrics to merge shard state into the existing summary format. 4. Port `simple` mode `constant-vus` first. 5. Port `constant-arrival-rate` with coordinator-issued tickets. 6. Port `ramping-vus`. 7. Port workflow mode and keep recreate / poison logic thread-local. ## Verification Plan - compare single-thread and multithread summaries for the same deterministic fixture - run `1`, `2`, and `4` thread parity checks for thresholds and request counts - verify one VU never changes owning worker during a run - stress recovery paths with poisoned workflow VUs under multithreaded execution - measure lock contention before and after shard-based metrics ## Non-Goals For Step 1 - distributed coordinator / agent execution - plugin ABI changes - gRPC streaming Those belong to later Phase 5 and Phase 6 work after single-node threading is stable.