---
description: Run scale assessment and load testing (M8)
argument-hint: "[project-path: defaults to .]"
allowed-tools: Bash, Read, Write, Glob, Grep
---

Run scale assessment including load testing, bottleneck detection, and capacity estimation.

## Prerequisites

Install a load testing tool (k6 recommended):
```bash
# macOS
brew install k6

# Linux
sudo apt install k6

# Windows
choco install k6
```

## Steps

1. **Validate project path**
   - Default to `.` if no argument provided
   - Confirm the project exists

2. **Check load testing tools**
   - Use `scale_tools_check` MCP tool
   - Verify k6 or Artillery is installed

3. **Discover load profiles**
   - Look for `.vaspera/load/*.yaml`
   - If no profiles exist, offer to generate sample

4. **Start the application**
   - Use `runtime_launch` to start the dev server
   - Wait for health check

5. **Run load tests**
   - Execute each profile scenario
   - Collect latency, throughput, error metrics

6. **Detect bottlenecks**
   - Scan code for N+1 queries, memory leaks, blocking ops
   - Analyze load test results for slow endpoints

7. **Estimate capacity**
   - Calculate max concurrent users
   - Estimate breakpoint (where system fails)
   - Project infrastructure costs

8. **Stop the application**
   - Clean shutdown of dev server

9. **Present results**
   ```
   Scale Assessment Results
   ========================
   Load Testing Tool: k6

   Profile: production
   ┌─────────────────┬──────────┬──────────┬──────────┐
   │ Scenario        │ VUs      │ RPS      │ p95 (ms) │
   ├─────────────────┼──────────┼──────────┼──────────┤
   │ Ramp Up         │ 1→50     │ 245      │ 89       │
   │ Steady State    │ 50       │ 312      │ 124      │
   │ Spike           │ 50→200   │ 156      │ 456      │
   └─────────────────┴──────────┴──────────┴──────────┘

   Bottlenecks Found: 3
   - [HIGH] N+1 query in src/api/products.ts
   - [MEDIUM] Blocking readFileSync in lib/config.ts
   - [MEDIUM] No connection pooling detected

   Capacity Estimate:
   - Max Concurrent Users: ~250
   - Max Requests/sec: ~400
   - Breakpoint: ~300 VUs (60% confidence)

   Projected Cost: $140/month (2x m5.large)

   Scale Score: 72/100

   Certification Level: 🟡 APPROVED
   → Ship with monitoring
   ```

10. **Write assessment report**
    - Create `.vaspera/scale/` directory
    - Write to `.vaspera/scale/{ISO-timestamp}.json`

## Load Profile Format

Profiles are defined in `.vaspera/load/*.yaml`:

```yaml
name: "production"
description: "Production-like load test"
tool: k6

endpoints:
  - path: "/"
    method: GET
    weight: 50
  - path: "/api/products"
    method: GET
    weight: 30
  - path: "/api/checkout"
    method: POST
    weight: 20
    body:
      items: [{ id: 1, qty: 1 }]

thresholds:
  p95: 500       # 95th percentile < 500ms
  p99: 1000      # 99th percentile < 1s
  errorRate: 0.01  # < 1% errors

scenarios:
  - name: "Ramp Up"
    type: ramp
    duration: "2m"
    vus:
      start: 1
      end: 50

  - name: "Steady State"
    type: ramp
    duration: "5m"
    vus:
      start: 50
      end: 50

  - name: "Spike"
    type: spike
    duration: "30s"
    vus:
      start: 50
      end: 200
```

## Bottleneck Types

| Type | Examples | Severity |
|------|----------|----------|
| database | N+1 queries, missing indexes | High |
| memory | Leaks, unbounded caches | Medium |
| cpu | Blocking operations, sync crypto | Medium |
| endpoint | Slow handlers, no caching | High |
| network | No connection reuse | Low |

## MCP Tools Used

- `scale_tools_check` — Verify load testing tools
- `scale_profiles_list` — Discover profiles
- `scale_profile_generate` — Create sample profile
- `scale_assess` — Full scale assessment
- `scale_bottlenecks` — Quick bottleneck scan

## Important

- Requires k6 or Artillery installed
- Load tests hit the actual app — use a test environment
- Results vary by hardware — run on similar specs to production
- Consider running during off-peak hours for accurate results
