# Aphrodite Load Testing Suite

Comprehensive K6 load testing suite for the Aphrodite streaming platform.

## Overview

This suite includes load tests for:

- **Concurrent Viewer Simulation** - Tests viewer discovery, stream joining,
  chat, and tipping
- **Tip Storm Simulation** - Tests high-volume payment processing and device
  triggers
- **Device Command Flooding** - Tests device control APIs under heavy load
- **Combined Load Test** - Simulates realistic production load with all features

## Prerequisites

1. Install K6: https://k6.io/docs/getting-started/installation/
2. Ensure Aphrodite services are running
3. Configure environment variables if needed

## Quick Start

```bash
# Run concurrent viewer simulation
k6 run testing/performance/k6/aphrodite/viewer-simulation.js

# Run tip storm simulation
k6 run testing/performance/k6/aphrodite/tip-storm.js

# Run device command flooding
k6 run testing/performance/k6/aphrodite/device-flooding.js

# Run combined load test (recommended for production validation)
k6 run testing/performance/k6/aphrodite/combined-load-test.js
```

## Configuration

### Environment Variables

| Variable                    | Default                      | Description             |
| --------------------------- | ---------------------------- | ----------------------- |
| `APHRODITE_API_URL`         | `http://localhost:3100`      | Main API URL            |
| `APHRODITE_STREAMING_URL`   | `http://localhost:3101`      | Streaming service URL   |
| `APHRODITE_BROADCASTER_URL` | `http://localhost:3102`      | Broadcaster service URL |
| `APHRODITE_VIEWER_URL`      | `http://localhost:3103`      | Viewer service URL      |
| `APHRODITE_CHAT_URL`        | `http://localhost:3104`      | Chat service URL        |
| `APHRODITE_PAYMENT_URL`     | `http://localhost:3105`      | Payment service URL     |
| `APHRODITE_DEVICES_URL`     | `http://localhost:3106`      | Devices service URL     |
| `JWT_SECRET`                | `aphrodite-load-test-secret` | JWT signing secret      |

### Example with Custom URLs

```bash
k6 run \
  -e APHRODITE_STREAMING_URL=http://staging.aphrodite.io:3101 \
  -e APHRODITE_PAYMENT_URL=http://staging.aphrodite.io:3105 \
  testing/performance/k6/aphrodite/combined-load-test.js
```

## Test Files

### `config.js`

Shared configuration and helper functions:

- Service URLs
- Custom metrics
- Test user data
- JWT token generation
- API helper functions
- Health check utilities
- Report generation

### `viewer-simulation.js`

Simulates concurrent viewers:

- Stream discovery
- Stream joining/leaving
- Chat participation
- Tipping behavior
- Viewer spike scenarios

**Scenarios:**

- `ramp_viewers`: Gradual ramp to 1000 concurrent viewers
- `viewer_spike`: Sudden influx of 500 viewers

**Thresholds:**

- HTTP P95 < 1000ms
- Stream discovery P95 < 500ms
- Stream join P95 < 1000ms
- Chat message P95 < 300ms

### `tip-storm.js`

Simulates high-volume tipping:

- Sustained tip rate (100/sec)
- Burst tip scenarios (up to 1000/sec)
- Device trigger integration
- Leaderboard checks
- Earnings tracking

**Scenarios:**

- `sustained_storm`: 100 tips/second for 5 minutes
- `burst_storm`: Bursts up to 1000 tips/second
- `top_tipper`: 200 tips/second to single broadcaster

**Thresholds:**

- Tip processing P95 < 1500ms
- Device trigger P95 < 500ms
- At least 1000 successful tips

### `device-flooding.js`

Simulates device command flooding:

- Vibrate commands
- Stop commands
- Pattern execution
- Batch device control
- Tip trigger simulation

**Scenarios:**

- `sustained_commands`: 50 commands/second
- `command_burst`: Bursts up to 500 commands/second
- `batch_operations`: 30 batch operations/second
- `stress_test`: Escalating to 1000 commands/second

**Thresholds:**

- Vibrate P95 < 300ms
- Stop P95 < 200ms
- Pattern P95 < 400ms
- Batch P95 < 800ms

### `combined-load-test.js`

Realistic production simulation:

- All services running simultaneously
- Viewer, tip, and device traffic
- Peak hour simulation

**Scenarios:**

- `viewer_traffic`: Up to 300 concurrent viewers
- `tip_traffic`: Up to 150 tips/second
- `device_traffic`: Up to 100 commands/second
- `peak_hour`: Combined load with 500 VUs

## Output

Each test generates:

- JSON summary file (e.g., `aphrodite-viewer-simulation-summary.json`)
- Console report with key metrics

## Metrics

### Custom Metrics

| Metric              | Type    | Description                |
| ------------------- | ------- | -------------------------- |
| `api_latency`       | Trend   | General API call latency   |
| `streaming_latency` | Trend   | Streaming service latency  |
| `payment_latency`   | Trend   | Payment service latency    |
| `device_latency`    | Trend   | Device service latency     |
| `chat_latency`      | Trend   | Chat service latency       |
| `tips_processed`    | Counter | Successful tips            |
| `viewers_joined`    | Counter | Viewers who joined streams |
| `device_commands`   | Counter | Device commands sent       |
| `errors`            | Rate    | Custom error rate          |

## Best Practices

1. **Warm up services** before running full load tests
2. **Run in isolation** to avoid interference from other traffic
3. **Monitor resources** (CPU, memory, DB connections) during tests
4. **Compare results** between runs to track regressions
5. **Start small** and gradually increase load to find limits

## Troubleshooting

### Services unhealthy

Ensure all Aphrodite services are running:

```bash
docker compose -f docker/docker-compose.dev.yml up -d
```

### Rate limiting

If seeing many 429 responses, the services are rate limiting correctly. Adjust
thresholds or test configuration accordingly.

### Token errors

Ensure `JWT_SECRET` matches the services' configuration.

## CI/CD Integration

Add to your CI pipeline:

```yaml
- name: Run Load Tests
  run: |
    k6 run --out json=results.json \
      testing/performance/k6/aphrodite/combined-load-test.js
```

## Contributing

When adding new tests:

1. Use `config.js` for shared utilities
2. Follow the existing naming conventions
3. Add appropriate thresholds
4. Include custom metrics for tracking
5. Generate detailed summary reports
