# GoodGo Platform — k6 Load Tests

EN: Load and performance tests for critical API paths.
VI: Load và performance tests cho các API path quan trọng.

## Prerequisites

```bash
brew install k6        # macOS
# or
choco install k6       # Windows
# or
sudo apt install k6    # Ubuntu/Debian
```

## Test Scripts

| Script | Target | Thresholds |
|--------|--------|------------|
| `order-creation.js` | POST /api/v1/orders | p95 < 500ms, errors < 1% |
| `catalog-listing.js` | GET /api/v1/products | p95 < 200ms, errors < 0.5% |
| `ads-tracking.js` | POST /api/v1/tracking/events | p95 < 100ms, errors < 0.1% |
| `signalr-connections.js` | WS /hubs/pos + negotiate | p95 < 300ms, errors < 1% |

## Running Tests

```bash
# EN: Run against local environment / VI: Chạy với môi trường local
k6 run tests/load/k6/order-creation.js

# EN: Run against staging / VI: Chạy với staging
k6 run \
  --env BASE_URL=http://api.staging.goodgo.vn \
  --env JWT_TOKEN=<your-token> \
  --env SHOP_ID=<your-shop-id> \
  tests/load/k6/order-creation.js

# EN: Run with output to InfluxDB for Grafana / VI: Xuất kết quả ra InfluxDB cho Grafana
k6 run \
  --out influxdb=http://localhost:8086/k6 \
  tests/load/k6/order-creation.js

# EN: Run with HTML report / VI: Chạy với báo cáo HTML
k6 run \
  --out json=results/order-creation.json \
  tests/load/k6/order-creation.js
```

## Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `BASE_URL` | `http://localhost:5010` | API base URL |
| `WS_URL` | `ws://localhost:5010` | WebSocket base URL (SignalR) |
| `JWT_TOKEN` | `test-bearer-token` | Bearer token for auth |
| `SHOP_ID` | `00000000-...01` | Shop UUID for tenant isolation |
| `PRODUCT_ID` | `00000000-...02` | Sample product UUID |
| `TRACKING_API_KEY` | `test-tracking-key` | Ads tracking API key |

## CI Integration

Add to `.github/workflows/ci-performance.yml`:

```yaml
- name: Run k6 load tests (smoke only in CI)
  run: |
    k6 run \
      --vus 5 --duration 30s \
      --env BASE_URL=${{ secrets.STAGING_API_URL }} \
      --env JWT_TOKEN=${{ secrets.STAGING_JWT_TOKEN }} \
      tests/load/k6/order-creation.js
```
