# Performance Issues Guide

Diagnose and resolve infrastructure performance problems.

## Slow Deployments

Diagnose why deployments are taking longer than expected:

```bash
#!/usr/bin/env nu

def diagnose-slow-deployment [] {
  # Check orchestrator performance
  let orchestrator_latency = (provisioning health orchestrator-latency)
  print $"Orchestrator latency: $orchestrator_latency ms"

  # Check task service performance
  let task_latencies = (provisioning task-services health | get latency)
  print $task_latencies

  # Check network
  let network_throughput = (provisioning metrics get network_bytes_per_second)
  print $"Network throughput: $network_throughput bytes/sec"

  # Identify bottleneck
  if $orchestrator_latency > 1000 {
    print "Bottleneck: Orchestrator latency high"
  } else if ($task_latencies | values | any { | | v $v > 5000 }) {
    print "Bottleneck: Task service performance"
  } else {
    print "Bottleneck: Network or external API"
  }
}
```

## High CPU Usage

Address excessive CPU consumption:

```bash
#!/usr/bin/env nu

def diagnose-high-cpu [] {
  # Get CPU-intensive processes
  let high_cpu_procs = (
    provisioning metrics get "process_cpu_seconds_total" |
    where value > 50 |
    sort-by value --reverse
  )

  print "High CPU processes:"
  $high_cpu_procs | each { | proc |
    print $"  ($proc.process): ($proc.value)%"
  }

  # Get CPU per service
  let service_cpu = (provisioning metrics get "cpu_usage_by_service")
  print $service_cpu
}
```

## Memory Leaks

Detect and prevent memory leaks:

```bash
#!/usr/bin/env nu

def detect-memory-leaks [] {
  let memory_trend = (provisioning metrics get memory_usage --period 1h --resolution 5m)

  # Check if memory is growing steadily
  let is_leaking = (
    $memory_trend |
    window 2 |
    all? { | pair |
      $pair.1 > $pair.0 * 1.01  # Growing by >1% each period
    }
  )

  if $is_leaking {
    print "Memory leak detected!"
    print "Restart services to resolve"
  } else {
    print "Memory usage normal"
  }
}
```

## Network Latency

Diagnose network performance:

```bash
#!/usr/bin/env nu

def diagnose-network-latency [] {
  # Check inter-service latency
  let latencies = (provisioning network latency-between-services)
  print "Service-to-service latency:"
  $latencies | each { | pair |
    print $"  ($pair.from) → ($pair.to): ($pair.latency)ms"
  }

  # Check external API latency
  let external_latency = (provisioning metrics get external_api_latency)
  print $"External API latency: $external_latency ms"

  # Recommend fixes
  if $external_latency > 100 {
    print "Recommendation: Enable caching, add CDN"
  }
}
```

## Database Slowness

Optimize database performance:

```sql
-- Identify slow queries
SELECT query, execution_time_ms, count
FROM pg_stat_statements
ORDER BY execution_time_ms DESC
LIMIT 10;

-- Check index usage
SELECT schemaname, tablename, indexname
FROM pg_indexes
WHERE idx_scan = 0;  -- Unused indexes

-- Check table bloat
SELECT schemaname, tablename, round(100.0 * dead_tuple_count / n_live_tup) AS dead_percent
FROM pgstattuple_approx(...)
WHERE dead_percent > 10;
```

## Related Documentation

- [Monitoring](./monitoring.md)
- [Troubleshooting Guide](./common-issues.md)
- [Logs Analysis](./logs-analysis.md)
