# K6 Load Testing - Quick Start Guide

## 5-Minute Setup

### Step 1: Install k6

```bash
# macOS
brew install k6

# Verify installation
k6 version
```

### Step 2: Start Your Backend

```bash
# In one terminal
cd /Users/pukpuk/Dev/website-enchun-mgr
pnpm dev
```

### Step 3: Run Your First Test

```bash
# In another terminal
cd apps/backend

# Run public browsing test (simplest - no auth needed)
k6 run tests/k6/public-browsing.js
```

That's it! You should see output showing 100 virtual users browsing your site.

## Next Steps

### Run All Tests

```bash
# Public browsing (100 users)
k6 run tests/k6/public-browsing.js

# API performance (50 users)
k6 run tests/k6/api-performance.js

# Admin operations (20 users) - requires admin credentials
k6 run --env ADMIN_EMAIL=your@email.com --env ADMIN_PASSWORD=yourpassword \
  tests/k6/admin-operations.js
```

### Test Against Staging

```bash
k6 run --env BASE_URL=https://staging.enchun.tw tests/k6/public-browsing.js
```

### Generate Report

```bash
# Generate JSON output
k6 run --out json=results.json tests/k6/public-browsing.js

# Convert to HTML (requires k6-reporter)
npm install -g k6-reporter
k6-reporter results.json --output results.html
open results.html
```

## Understanding Results

Look for these key metrics:

```
✓ http_req_duration..............: avg=185ms p(95)=420ms
✓ http_req_failed................: 0.00% ✓ 0      ✗ 12000
✓ checks.........................: 100.0% ✓ 12000 ✗ 0
```

**What to check:**
- `p(95)` should be < 500ms
- `http_req_failed` should be < 1%
- `checks` should be > 99%

## Common Issues

**"connect attempt failed"**
→ Make sure your backend is running (pnpm dev)

**"login failed" in admin tests**
→ Set correct admin credentials via environment variables

**High error rate**
→ Reduce VUs: `k6 run --env STAGED_USERS=10 tests/k6/public-browsing.js`

## Need Help?

See the full README: `tests/k6/README.md`

---

**Happy Testing!** 🚀
