# Load Testing Guide

**Status: Deferred to post-launch.** This guide documents the recommended commands and targets for load testing TheSol Chat once the application is live on the production host. Do not run these tests against a shared or production database without a read-only replica or a staging clone.

---

## Target Endpoints

| Priority | Endpoint | Method | Payload |
|---|---|---|---|
| P1 | Admin login | POST | `include/ajax.php` `function=login` |
| P1 | Widget boot (chat settings) | POST | `include/ajax.php` `function=get-front-settings` |
| P1 | Send message | POST | `include/ajax.php` `function=send-message` |
| P1 | WhatsApp webhook | POST | `apps/whatsapp/post.php` |
| P2 | AJAX batch request | POST | `include/ajax.php` `function=batch` |
| P2 | Queue worker (cron mode) | CLI | `php logs/queue_worker.php --batch=30` |
| P2 | OpenAI troubleshoot | POST | `include/ajax.php` `function=openai-troubleshoot` |

---

## Acceptable Baselines (to record after first run)

| Endpoint | p50 | p95 | Error rate |
|---|---|---|---|
| Widget boot | < 200 ms | < 500 ms | 0 % |
| Send message | < 300 ms | < 800 ms | 0 % |
| WhatsApp webhook | < 200 ms | < 400 ms | 0 % |
| Admin login | < 400 ms | < 1 s | 0 % |

---

## Tool Options

### Apache Bench (ab) — quick smoke test

Included with Apache. Good for single-endpoint burst testing.

```bash
# 100 requests, 10 concurrent — widget boot
ab -n 100 -c 10 -T 'application/x-www-form-urlencoded' \
   -p /tmp/ab_payload.txt \
   https://yourdomain.com/include/ajax.php

# /tmp/ab_payload.txt contents:
# function=get-front-settings&version=latest
```

### k6 — scripted scenarios (recommended)

[https://k6.io/docs/](https://k6.io/docs/) — install once on a test machine, write scenarios in JS.

```js
// k6/send-message.js
import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
    vus: 20,
    duration: '30s',
    thresholds: {
        http_req_duration: ['p(95)<800'],
        http_req_failed:   ['rate<0.01'],
    },
};

export default function () {
    const res = http.post('https://yourdomain.com/include/ajax.php', {
        function:        'send-message',
        message:         'Load test message',
        conversation_id: '1',
        token:           __ENV.SB_TOKEN, // set via k6 --env SB_TOKEN=xxx
    });
    check(res, { 'status 200': (r) => r.status === 200 });
    sleep(0.5);
}
```

Run:

```bash
k6 run --vus 20 --duration 30s --env SB_TOKEN=your_session_token k6/send-message.js
```

### wrk — HTTP benchmarking

[https://github.com/wg/wrk](https://github.com/wg/wrk) — suitable for simple GET endpoint stress testing (e.g., admin dashboard HTML).

```bash
wrk -t4 -c20 -d30s https://yourdomain.com/admin.php
```

---

## WhatsApp Webhook Replay Test

Use the procedure in [MSG91_WEBHOOK_SETUP.md](../MSG91_WEBHOOK_SETUP.md) to verify duplicate-message replay guard under concurrency:

```bash
# Fire the same webhook body 5 times in rapid succession
for i in 1 2 3 4 5; do
  curl -s -X POST "https://yourdomain.com/apps/whatsapp/post.php?sb-token=YOUR_TOKEN" \
       -H "Content-Type: application/json" \
       -d '{"requestId":"test-replay-001","from":"919999999999","type":"text","payload":{"text":"hello"}}' &
done
wait
```

Expected: only one new message inserted; all responses HTTP 200.

---

## Queue Worker Load Test

Run in batch mode and measure throughput:

```bash
time php logs/queue_worker.php --batch=100
php logs/queue_worker.php --stats
```

Expected: all 100 jobs claimed and processed without duplicate processing (check `claim_id` uniqueness in `sb_queue`).

---

## Post-Run Checklist

- [ ] Record p50/p95/error-rate for each target endpoint
- [ ] Confirm `sb_queue` failed count is 0 after the run
- [ ] Confirm no PHP fatal errors in `error_log`
- [ ] Confirm DB connection pool did not exhaust (check `SHOW STATUS LIKE 'Threads_connected'`)
- [ ] Document results in `FINAL_CHANGES.md` P5-OPS-06 row
