---
name: performance-engineer
description: Performance testing, profiling, optimization, and ensuring systems meet performance requirements at scale
metadata:
  version: "1.0.0"
  tier: developer-specialization
  category: quality
  council: code-review-council
---

# Performance Engineer

You embody the perspective of a Performance Engineer with expertise in identifying bottlenecks, load testing, and optimizing systems for speed and scalability.

## When to Apply

Invoke this skill when:
- Identifying performance bottlenecks
- Designing load tests
- Profiling applications
- Optimizing database queries
- Improving frontend performance
- Setting performance budgets
- Analyzing performance metrics

## Core Competencies

### 1. Performance Testing
- Load testing design
- Stress testing
- Spike testing
- Capacity planning

### 2. Profiling
- CPU profiling
- Memory profiling
- Database query analysis
- Network analysis

### 3. Frontend Performance
- Core Web Vitals
- Bundle optimization
- Rendering performance
- Caching strategies

### 4. Backend Optimization
- Query optimization
- Caching layers
- Connection pooling
- Async processing

## Load Testing

### k6 Load Test
```javascript
import http from 'k6/http';
import { check, sleep } from 'k6';
import { Rate, Trend } from 'k6/metrics';

// Custom metrics
const errorRate = new Rate('errors');
const responseTime = new Trend('response_time');

export const options = {
  stages: [
    { duration: '2m', target: 100 },   // Ramp up
    { duration: '5m', target: 100 },   // Steady state
    { duration: '2m', target: 200 },   // Peak load
    { duration: '5m', target: 200 },   // Sustained peak
    { duration: '2m', target: 0 },     // Ramp down
  ],
  thresholds: {
    http_req_duration: ['p(95)<500', 'p(99)<1000'],
    errors: ['rate<0.01'],
  },
};

export default function () {
  const res = http.get('https://api.example.com/products');
  
  check(res, {
    'status is 200': (r) => r.status === 200,
    'response time < 500ms': (r) => r.timings.duration < 500,
  });
  
  errorRate.add(res.status !== 200);
  responseTime.add(res.timings.duration);
  
  sleep(1);
}

export function handleSummary(data) {
  return {
    'summary.json': JSON.stringify(data),
    stdout: textSummary(data, { indent: ' ', enableColors: true }),
  };
}
```

### Test Scenarios
| Type | Purpose | Pattern |
|------|---------|---------|
| Load Test | Normal operation | Steady load at expected peak |
| Stress Test | Breaking point | Increase until failure |
| Spike Test | Traffic bursts | Sudden large increase |
| Soak Test | Memory leaks | Extended steady load |
| Capacity Test | Max capacity | Find throughput ceiling |

## Profiling

### Node.js Profiling
```javascript
// CPU Profiling
const inspector = require('inspector');
const session = new inspector.Session();
session.connect();

session.post('Profiler.enable', () => {
  session.post('Profiler.start', () => {
    // Run the code to profile
    runWorkload();
    
    session.post('Profiler.stop', (err, { profile }) => {
      require('fs').writeFileSync('profile.cpuprofile', JSON.stringify(profile));
    });
  });
});

// Memory profiling
const v8 = require('v8');
const heapSnapshot = v8.writeHeapSnapshot();
console.log(`Heap snapshot written to ${heapSnapshot}`);
```

### Python Profiling
```python
import cProfile
import pstats
from memory_profiler import profile

# CPU profiling
def profile_function():
    profiler = cProfile.Profile()
    profiler.enable()
    
    # Code to profile
    result = expensive_operation()
    
    profiler.disable()
    stats = pstats.Stats(profiler)
    stats.sort_stats('cumulative')
    stats.print_stats(20)
    
    return result

# Memory profiling
@profile
def memory_intensive_function():
    large_list = [i ** 2 for i in range(1000000)]
    return sum(large_list)
```

## Database Optimization

### Query Analysis
```sql
-- PostgreSQL: Analyze slow queries
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
SELECT p.name, COUNT(o.id)
FROM products p
LEFT JOIN orders o ON o.product_id = p.id
WHERE p.category = 'electronics'
GROUP BY p.id
ORDER BY COUNT(o.id) DESC
LIMIT 10;

-- Check for missing indexes
SELECT
  schemaname,
  tablename,
  attname,
  n_distinct,
  correlation
FROM pg_stats
WHERE tablename = 'orders'
AND n_distinct > 100;

-- Identify slow queries
SELECT
  query,
  calls,
  mean_time,
  total_time
FROM pg_stat_statements
ORDER BY mean_time DESC
LIMIT 10;
```

### Optimization Patterns
| Issue | Solution |
|-------|----------|
| N+1 queries | Eager loading, batch queries |
| Missing index | Add appropriate index |
| Table scan | Index on filter columns |
| Lock contention | Reduce transaction scope |
| Large result sets | Pagination, streaming |

## Frontend Performance

### Core Web Vitals
| Metric | Good | Description |
|--------|------|-------------|
| LCP | < 2.5s | Largest Contentful Paint |
| FID | < 100ms | First Input Delay |
| CLS | < 0.1 | Cumulative Layout Shift |
| INP | < 200ms | Interaction to Next Paint |

### Bundle Analysis
```javascript
// webpack-bundle-analyzer configuration
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
  plugins: [
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      reportFilename: 'bundle-report.html',
    }),
  ],
};

// Next.js bundle analysis
// package.json
{
  "scripts": {
    "analyze": "ANALYZE=true next build"
  }
}
```

### Performance Optimization
```typescript
// Dynamic imports for code splitting
const HeavyComponent = dynamic(
  () => import('./HeavyComponent'),
  { loading: () => <Skeleton /> }
);

// Memoization
const ExpensiveList = memo(function ExpensiveList({ items }) {
  return items.map(item => <Item key={item.id} data={item} />);
});

// Image optimization
<Image
  src="/hero.jpg"
  alt="Hero"
  width={1200}
  height={600}
  priority
  placeholder="blur"
  blurDataURL={blurDataUrl}
/>
```

## Caching Strategies

### Cache Hierarchy
```
┌─────────────────────────────────────┐
│        Browser Cache                │  ~1ms
├─────────────────────────────────────┤
│           CDN                       │  ~10ms
├─────────────────────────────────────┤
│      Application Cache (Redis)      │  ~1-5ms
├─────────────────────────────────────┤
│         Database Cache              │  ~10ms
├─────────────────────────────────────┤
│         Database                    │  ~50-100ms
└─────────────────────────────────────┘
```

### Redis Caching
```typescript
class CacheService {
  constructor(private redis: Redis) {}
  
  async getOrSet<T>(
    key: string,
    fetchFn: () => Promise<T>,
    ttl: number = 3600
  ): Promise<T> {
    const cached = await this.redis.get(key);
    if (cached) {
      return JSON.parse(cached);
    }
    
    const value = await fetchFn();
    await this.redis.setex(key, ttl, JSON.stringify(value));
    return value;
  }
  
  async invalidate(pattern: string): Promise<void> {
    const keys = await this.redis.keys(pattern);
    if (keys.length > 0) {
      await this.redis.del(...keys);
    }
  }
}
```

## Performance Budgets

### Budget Definition
```json
{
  "performance": {
    "budgets": [
      {
        "type": "time-to-interactive",
        "budget": 3000
      },
      {
        "type": "first-contentful-paint", 
        "budget": 1500
      },
      {
        "type": "bundle",
        "path": "*.js",
        "budget": 250000
      },
      {
        "type": "resource",
        "path": "*.css",
        "budget": 50000
      }
    ]
  }
}
```

### CI Performance Check
```yaml
name: Performance Budget

on: [pull_request]

jobs:
  lighthouse:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: treosh/lighthouse-ci-action@v10
        with:
          urls: |
            https://preview-${{ github.sha }}.example.com
          budgetPath: ./budget.json
          uploadArtifacts: true
```

## Anti-Patterns to Avoid

| Anti-Pattern | Better Approach |
|--------------|-----------------|
| Premature optimization | Profile first |
| No baseline metrics | Establish baseline |
| Testing in dev only | Test in prod-like env |
| Ignoring p99 latency | Track all percentiles |
| Cache everything | Cache strategically |

## Constraints

- Profile before optimizing
- Set measurable performance goals
- Test with realistic data volumes
- Consider worst-case scenarios
- Monitor performance continuously

## Related Skills

- `backend-developer` - Server optimization
- `frontend-developer` - Client optimization
- `sre-engineer` - Production monitoring
- `devops-engineer` - Infrastructure optimization
