# Task ID: 13
# Title: Implement Performance Analytics and Optimization
# Status: deferred
# Dependencies: 6, 7
# Priority: low
# Description: Add performance monitoring and analytics to identify bottlenecks and optimize extension performance.
# Details:
Create a PerformanceMonitor class to track and analyze extension performance:

```typescript
export interface PerformanceMetric {
  operation: string;
  duration: number;
  timestamp: number;
  metadata?: Record<string, any>;
}

export class PerformanceMonitor {
  private metrics: PerformanceMetric[] = [];
  private logger: Logger;
  private maxMetrics: number = 100;
  
  constructor(logger: Logger) {
    this.logger = logger;
  }
  
  public async trackOperation<T>(operation: string, fn: () => Promise<T>, metadata?: Record<string, any>): Promise<T> {
    const startTime = performance.now();
    try {
      return await fn();
    } finally {
      const endTime = performance.now();
      const duration = endTime - startTime;
      
      this.recordMetric({
        operation,
        duration,
        timestamp: Date.now(),
        metadata
      });
      
      // Log slow operations
      if (duration > 1000) { // More than 1 second
        this.logger.warn(`Slow operation: ${operation} took ${duration.toFixed(2)}ms`, metadata);
      }
    }
  }
  
  private recordMetric(metric: PerformanceMetric): void {
    this.metrics.push(metric);
    
    // Keep metrics array from growing too large
    if (this.metrics.length > this.maxMetrics) {
      this.metrics = this.metrics.slice(-this.maxMetrics);
    }
  }
  
  public getMetrics(): PerformanceMetric[] {
    return [...this.metrics];
  }
  
  public getAverageMetrics(): Record<string, number> {
    const operationTotals: Record<string, { total: number, count: number }> = {};
    
    for (const metric of this.metrics) {
      if (!operationTotals[metric.operation]) {
        operationTotals[metric.operation] = { total: 0, count: 0 };
      }
      
      operationTotals[metric.operation].total += metric.duration;
      operationTotals[metric.operation].count += 1;
    }
    
    const averages: Record<string, number> = {};
    for (const [operation, data] of Object.entries(operationTotals)) {
      averages[operation] = data.total / data.count;
    }
    
    return averages;
  }
  
  public clearMetrics(): void {
    this.metrics = [];
  }
}
```

Implement performance tracking for key operations including file reading, context generation, and output writing. Add memory usage monitoring to detect memory leaks. Create a performance dashboard command to view metrics. Implement automatic optimization suggestions based on performance data. Add telemetry for aggregate performance data (with user consent).

# Test Strategy:
1. Test performance tracking with various operations
2. Verify average metrics calculation is correct
3. Test memory usage monitoring
4. Benchmark performance with large workspaces
5. Test performance dashboard command
