---
name: performance-baseline
description: Track performance metrics over time, establish baselines, and alert on anomalies. Monitors response times, resource usage, and service health trends.
---

# Performance Baseline Skill

Establish and monitor performance baselines to detect degradation early.

## Metrics Tracked

### Response Time
- API endpoint latency (p50, p95, p99)
- Page load time
- Database query time
- Cache hit/miss ratio

### Resource Usage
- CPU utilization (avg, peak)
- Memory usage (RSS, heap)
- Disk I/O
- Network throughput

### Service Health
- Uptime percentage
- Error rate
- Request rate
- Container restart count

## Baseline Collection

### Initial Baseline (7-day average)
```bash
# Collect metrics every 5 minutes for 7 days
*/5 * * * * /scripts/collect-metrics.sh >> /var/log/metrics/baseline.json
```

### Metrics Format
```json
{
  "timestamp": "2026-01-29T19:30:00Z",
  "service": "rima-backend",
  "metrics": {
    "response_time_ms": {
      "p50": 45,
      "p95": 120,
      "p99": 250
    },
    "cpu_percent": 15.2,
    "memory_mb": 256,
    "error_rate": 0.01,
    "requests_per_sec": 50
  }
}
```

## Anomaly Detection

### Threshold-based
```
Alert if:
- Response time > baseline + 50%
- Error rate > baseline + 200%
- CPU > 80% sustained 5 min
- Memory > 85% sustained 5 min
```

### Trend-based
```
Alert if:
- Response time increasing for 3 consecutive checks
- Memory growing without release (leak detection)
- Error rate trending up
```

## Performance Report Template

```
📈 PERFORMANCE REPORT: rima-backend
Period: Last 24 hours vs Baseline

┌─────────────────────────────────────────────┐
│ Response Time                               │
├─────────────────────────────────────────────┤
│ Current p95: 125ms                          │
│ Baseline p95: 120ms                         │
│ Status: ✅ Normal (+4%)                     │
│                                             │
│ Trend: ━━━━━━━━━━━━━━━━━━━━━━━ Stable      │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Resource Usage                              │
├─────────────────────────────────────────────┤
│ CPU: 18% (baseline: 15%) ✅                 │
│ Memory: 280MB (baseline: 256MB) ✅          │
│ Disk I/O: Normal ✅                         │
│                                             │
│ Memory Trend: ↗ Slight increase (monitor)  │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Service Health                              │
├─────────────────────────────────────────────┤
│ Uptime: 99.99%                              │
│ Error Rate: 0.02% (baseline: 0.01%)         │
│ Requests: 45k (baseline: 42k) ✅            │
│ Restarts: 0                                 │
└─────────────────────────────────────────────┘

Summary: All metrics within acceptable range
Next baseline update: 2026-02-05
```

## Quick Performance Check

```bash
# One-liner performance check
curl -w "@curl-format.txt" -o /dev/null -s https://rima-api.zesbe.my.id/health
```

curl-format.txt:
```
     time_namelookup:  %{time_namelookup}s\n
        time_connect:  %{time_connect}s\n
     time_appconnect:  %{time_appconnect}s\n
    time_pretransfer:  %{time_pretransfer}s\n
       time_redirect:  %{time_redirect}s\n
  time_starttransfer:  %{time_starttransfer}s\n
                     ----------\n
          time_total:  %{time_total}s\n
```

## Integration with Proactive Mode

On every health check:
1. Collect current metrics
2. Compare against baseline
3. Alert if anomaly detected
4. Log for trend analysis
5. Update rolling baseline weekly

## Load Testing Baseline

Before major releases:
```bash
# Establish performance under load
wrk -t12 -c400 -d30s https://rima-api.zesbe.my.id/health
```

Track:
- Max requests/sec before degradation
- Breaking point (error rate > 1%)
- Recovery time after load
