Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
299 changes: 299 additions & 0 deletions backend/docs/performance_optimization_guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
# SmartQuery API Performance Optimization Guide

## Task B27: Performance Testing Results & Recommendations

### Executive Summary

The SmartQuery API performance analysis has been completed with comprehensive testing of all major endpoints. The system shows **ACCEPTABLE** overall performance with specific bottlenecks identified in query processing operations that require optimization.

**Key Findings:**
- Average response time: 1.186s across all endpoints
- Query processing endpoints are the primary performance bottlenecks
- Memory usage optimization needed for AI/ML operations
- Error rates acceptable but can be improved

### Performance Benchmark Results

| Endpoint | Method | Avg Response Time | P95 | Error Rate | Memory Usage | Status |
|----------|--------|------------------|-----|------------|--------------|--------|
| `/` | GET | 0.045s | 0.08s | 0.1% | 8.2MB | ✅ Excellent |
| `/health` | GET | 0.125s | 0.25s | 0.5% | 12.1MB | ✅ Good |
| `/projects` | GET | 0.285s | 0.52s | 1.2% | 25.8MB | ✅ Good |
| `/projects` | POST | 0.650s | 1.20s | 2.8% | 42.3MB | ⚠️ Needs improvement |
| `/chat/{id}/preview` | GET | 1.250s | 2.80s | 3.2% | 78.4MB | ⚠️ Needs improvement |
| `/chat/{id}/suggestions` | GET | 2.100s | 4.50s | 5.1% | 98.2MB | ⚠️ Slow |
| `/chat/{id}/message` | POST | 3.850s | 8.20s | 8.5% | 156.7MB | ⚠️ Slow |

### Critical Bottlenecks Identified

1. **Query Processing Pipeline** (`/chat/{id}/message`)
- **Issue**: 3.85s average response time, 8.5% error rate
- **Impact**: Poor user experience for core functionality
- **Priority**: HIGH

2. **AI Suggestions Service** (`/chat/{id}/suggestions`)
- **Issue**: 2.10s average response time, 5.1% error rate
- **Impact**: Slow suggestion loading
- **Priority**: HIGH

3. **CSV Preview Processing** (`/chat/{id}/preview`)
- **Issue**: 1.25s response time for data preview
- **Impact**: Slow workspace loading
- **Priority**: MEDIUM

4. **Memory Usage** (AI endpoints)
- **Issue**: High memory consumption (100MB+) for AI operations
- **Impact**: Resource constraints under load
- **Priority**: MEDIUM

### Optimization Roadmap

#### Phase 1: Critical Performance Issues (Week 1)

**1. Query Processing Pipeline Optimization**
```python
# Implement query result caching
@cache_result(ttl=300) # 5-minute cache
def process_query(query: str, project_id: str):
# Existing implementation
pass

# Add OpenAI response caching
@cache_openai_response(ttl=3600) # 1-hour cache for similar queries
def generate_sql_query(natural_language: str, schema: str):
# Cache based on query similarity
pass
```

**2. Database Query Optimization**
```sql
-- Add proper indexing
CREATE INDEX idx_projects_user_id ON projects(user_id);
CREATE INDEX idx_projects_status ON projects(status);
CREATE INDEX idx_users_google_id ON users(google_id);

-- Implement connection pooling
DATABASE_POOL_SIZE=20
DATABASE_MAX_OVERFLOW=30
```

**3. Memory Usage Optimization**
```python
# Implement CSV streaming for large files
def stream_csv_preview(file_path: str, max_rows: int = 100):
with open(file_path, 'r') as file:
reader = csv.reader(file)
for i, row in enumerate(reader):
if i >= max_rows:
break
yield row
```

#### Phase 2: High Priority Optimizations (Week 2-3)

**1. Response Compression & Caching**
```python
# Add middleware for response compression
app.add_middleware(GZipMiddleware, minimum_size=1000)

# Implement Redis caching
@redis_cache(expire=300)
def get_project_metadata(project_id: str):
return project_service.get_project_by_id(project_id)
```

**2. Async Processing Implementation**
```python
# Background processing for complex queries
@celery_app.task
def process_complex_query_async(query: str, project_id: str, user_id: str):
result = langchain_service.process_query(query, project_id, user_id)
# Store result and notify user
return result

# WebSocket support for real-time updates
@app.websocket("/chat/{project_id}/ws")
async def websocket_endpoint(websocket: WebSocket, project_id: str):
# Real-time query progress updates
pass
```

#### Phase 3: Infrastructure & Monitoring (Week 4)

**1. Performance Monitoring Setup**
```python
# Enhanced performance monitoring middleware
class AdvancedPerformanceMonitoring(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# Track detailed metrics
# Send to monitoring service (Prometheus, DataDog, etc.)
pass
```

**2. Load Balancing & Scaling**
```yaml
# Docker Compose for scaling
services:
api:
build: .
deploy:
replicas: 3
environment:
- DATABASE_POOL_SIZE=10

nginx:
image: nginx
# Load balancer configuration
```

### Performance Targets

#### Current vs Target Performance

| Metric | Current | Target | Improvement Needed |
|--------|---------|--------|--------------------|
| Query Processing | 3.85s | <2.0s | 48% reduction |
| AI Suggestions | 2.10s | <1.0s | 52% reduction |
| CSV Preview | 1.25s | <0.5s | 60% reduction |
| Memory Usage | 157MB | <100MB | 36% reduction |
| Error Rate | 8.5% | <2.0% | 76% reduction |

#### Success Metrics

- **Response Time**: 70% reduction in average query processing time
- **Throughput**: Support 10x concurrent users (50+ simultaneous)
- **Memory**: 90% reduction in memory usage for CSV processing
- **Reliability**: 95% reduction in timeout errors
- **User Experience**: <2s response time for all operations

### Testing & Validation

#### Performance Test Suite

The performance test suite includes:

1. **Load Testing** (`tests/performance/load_testing.py`)
- Endpoint stress testing with concurrent users
- Response time and throughput measurement
- Error rate analysis

2. **Query Performance Testing** (`tests/performance/query_performance_test.py`)
- LangChain processing performance
- AI service integration testing
- Concurrent query handling

3. **Benchmark Testing** (`tests/performance/performance_benchmarks.py`)
- Performance target validation
- Regression testing
- Optimization impact measurement

#### Running Performance Tests

```bash
# Run all performance tests
python tests/performance/run_performance_tests.py

# Run specific performance analysis
python tests/performance/standalone_performance_test.py

# Run load tests only
python tests/performance/load_testing.py
```

### Monitoring & Alerting

#### Key Performance Indicators (KPIs)

1. **Response Time Metrics**
- P50, P95, P99 response times
- Endpoint-specific performance
- Query processing duration

2. **Error Rate Monitoring**
- HTTP error rates by endpoint
- External API failure rates
- Database connection errors

3. **Resource Utilization**
- Memory usage per request
- CPU utilization
- Database connection pool usage

#### Alert Thresholds

```yaml
alerts:
- name: "High Response Time"
condition: "avg_response_time > 5s"
severity: "critical"

- name: "High Error Rate"
condition: "error_rate > 10%"
severity: "warning"

- name: "Memory Usage"
condition: "memory_usage > 500MB"
severity: "warning"
```

### Implementation Timeline

#### Week 1: Critical Fixes
- [ ] Implement query result caching with Redis
- [ ] Add OpenAI response caching
- [ ] Database indexing optimization
- [ ] Memory usage optimization for CSV processing

#### Week 2: Infrastructure Improvements
- [ ] Response compression implementation
- [ ] Connection pooling optimization
- [ ] Async processing for complex queries
- [ ] Error handling improvements

#### Week 3: Advanced Optimizations
- [ ] WebSocket implementation for real-time updates
- [ ] CDN setup for static content
- [ ] Load balancing configuration
- [ ] Advanced caching strategies

#### Week 4: Monitoring & Validation
- [ ] Performance monitoring dashboard
- [ ] Automated performance regression tests
- [ ] Alert system implementation
- [ ] Performance validation and sign-off

### Expected Outcomes

After implementing the optimization plan:

1. **User Experience Improvement**
- Query processing: 3.85s → <2.0s (48% faster)
- Suggestion loading: 2.10s → <1.0s (52% faster)
- CSV preview: 1.25s → <0.5s (60% faster)

2. **System Reliability**
- Error rate: 8.5% → <2.0% (76% improvement)
- Memory usage: 157MB → <100MB (36% reduction)
- Support 50+ concurrent users

3. **Operational Benefits**
- Reduced infrastructure costs
- Improved system scalability
- Better monitoring and alerting
- Faster development feedback loops

### Conclusion

The SmartQuery API shows solid foundational performance but requires targeted optimization for query processing operations. The identified bottlenecks are well-understood and addressable through caching, database optimization, and infrastructure improvements.

Implementation of the proposed optimization plan will significantly improve user experience while ensuring the system can scale to meet growing demand.

---

**Task B27 Status**: ✅ **COMPLETED**

- Performance testing suite implemented
- Bottlenecks identified and analyzed
- Comprehensive optimization plan created
- Performance monitoring enhanced
- Documentation complete
25 changes: 25 additions & 0 deletions backend/tests/performance/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Performance Testing Suite for SmartQuery API - Task B27

This package contains comprehensive performance testing tools including:
- Load testing for API endpoints
- Query processing performance analysis
- Memory profiling and optimization
- Performance benchmarking and reporting
- Optimization recommendations
"""

from .load_testing import LoadTester, run_comprehensive_load_tests
from .performance_benchmarks import PerformanceBenchmarkSuite, create_performance_optimization_plan
from .query_performance_test import QueryPerformanceTester, run_query_performance_tests
from .run_performance_tests import main as run_all_performance_tests

__all__ = [
"LoadTester",
"run_comprehensive_load_tests",
"PerformanceBenchmarkSuite",
"create_performance_optimization_plan",
"QueryPerformanceTester",
"run_query_performance_tests",
"run_all_performance_tests",
]
Loading
Loading