Skip to content

Performance Guide

This document covers performance optimization features and best practices for Azure Functions OpenAPI.

Performance Features

Caching System

The library includes a comprehensive caching system to improve performance:

In-Memory Cache

  • TTL-based expiration: Automatic cleanup of expired entries
  • LRU eviction: Least Recently Used eviction when cache is full
  • Thread-safe operations: Safe for concurrent access
  • Configurable TTL: Default 5 minutes, customizable per operation

Cached Operations

The following operations are automatically cached:

  • OpenAPI spec generation: 10-minute TTL
  • JSON serialization: 5-minute TTL
  • YAML serialization: 5-minute TTL

Cache Management

from azure_functions_openapi.cache import (
    get_cache_manager,
    invalidate_cache,
    clear_all_cache,
    get_cache_stats
)

# Get cache statistics
stats = get_cache_stats()
print(f"Active entries: {stats['active_entries']}")

# Invalidate specific cache entries
invalidated = invalidate_cache("openapi_spec")

# Clear all cache
clear_all_cache()

Performance Monitoring

Response Time Tracking

The library tracks response times for all operations:

from azure_functions_openapi.monitoring import get_performance_monitor

monitor = get_performance_monitor()
stats = monitor.get_response_time_stats()

print(f"Average response time: {stats['avg']:.3f}s")
print(f"95th percentile: {stats['p95']:.3f}s")

Throughput Metrics

Monitor requests per second, minute, and hour:

throughput = monitor.get_throughput_stats()
print(f"Requests per second: {throughput['requests_per_second']:.2f}")

Request Logging

Track individual requests with detailed metrics:

from azure_functions_openapi.monitoring import log_request

log_request(
    method="GET",
    path="/api/users",
    status_code=200,
    response_time=0.150,
    user_agent="Mozilla/5.0...",
    ip_address="192.168.1.1"
)

Performance Decorators

Use the @monitor_performance decorator to automatically track function performance:

from azure_functions_openapi.monitoring import monitor_performance

@monitor_performance
def my_api_function():
    # Your function code
    return {"result": "success"}

Performance Best Practices

OpenAPI Generation

  1. Use caching: The library automatically caches generated specs
  2. Minimize changes: Avoid frequent changes to function metadata
  3. Batch operations: Group related operations together
  4. Optimize models: Use efficient Pydantic models

Memory Management

  1. Monitor cache size: Use get_cache_stats() to monitor memory usage
  2. Clear cache periodically: Use clear_all_cache() in maintenance windows
  3. Limit cache entries: The cache automatically limits entries to prevent memory issues

Response Optimization

  1. Minimize data: Only include necessary information in responses
  2. Use compression: Enable gzip compression in Azure Functions
  3. Optimize serialization: Use efficient JSON serialization
  4. Cache responses: Cache frequently accessed data

Database and External Services

  1. Connection pooling: Use connection pooling for database connections
  2. Async operations: Use async/await for I/O operations
  3. Timeout configuration: Set appropriate timeouts
  4. Retry logic: Implement exponential backoff for retries

Performance Configuration

Cache Configuration

Configure cache behavior:

from azure_functions_openapi.cache import get_cache_manager

cache = get_cache_manager()
cache.default_ttl = 600  # 10 minutes

Monitoring Configuration

Configure monitoring settings:

from azure_functions_openapi.monitoring import get_performance_monitor

monitor = get_performance_monitor()
monitor._max_response_times = 2000  # Keep more response times

Logging Configuration

Configure logging for performance monitoring:

import logging

# Set up performance logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('azure_functions_openapi.monitoring')
logger.setLevel(logging.DEBUG)

Performance Metrics

Key Metrics to Monitor

  1. Response Time
  2. Average response time
  3. 95th percentile response time
  4. 99th percentile response time
  5. Maximum response time

  6. Throughput

  7. Requests per second
  8. Requests per minute
  9. Requests per hour
  10. Total requests

  11. Error Rate

  12. Error percentage
  13. Error count
  14. Error types

  15. Cache Performance

  16. Cache hit rate
  17. Cache miss rate
  18. Cache size
  19. Cache evictions

Performance Targets

Recommended performance targets:

  • Response Time: < 100ms for cached operations
  • Throughput: > 1000 requests/second
  • Error Rate: < 1%
  • Cache Hit Rate: > 90%

Performance Troubleshooting

Common Issues

  1. Slow Response Times
  2. Check cache hit rates
  3. Monitor memory usage
  4. Review function complexity
  5. Check external service latency

  6. High Memory Usage

  7. Monitor cache size
  8. Check for memory leaks
  9. Review data structures
  10. Clear cache if needed

  11. High Error Rates

  12. Check input validation
  13. Review error handling
  14. Monitor external services
  15. Check resource limits

Performance Profiling

Use Python profiling tools:

import cProfile
import pstats

# Profile your function
profiler = cProfile.Profile()
profiler.enable()

# Your code here
generate_openapi_spec()

profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats('cumulative')
stats.print_stats(10)

Memory Profiling

Use memory profiling tools:

from memory_profiler import profile

@profile
def my_function():
    # Your code here
    pass

Performance Testing

Load Testing

Use tools like Apache Bench or wrk for load testing:

# Test with 100 concurrent users for 30 seconds
ab -n 1000 -c 100 -t 30 http://localhost:7071/api/openapi.json

Stress Testing

Test system limits:

# Test with high concurrency
ab -n 10000 -c 500 http://localhost:7071/api/openapi.json

Monitoring During Tests

Monitor key metrics during testing:

from azure_functions_openapi.monitoring import get_performance_monitor

monitor = get_performance_monitor()

# Before test
initial_stats = monitor.get_response_time_stats()

# Run your test
# ...

# After test
final_stats = monitor.get_response_time_stats()
print(f"Performance change: {final_stats['avg'] - initial_stats['avg']:.3f}s")

Performance Optimization Checklist

  • [ ] Enable caching for all operations
  • [ ] Monitor response times and throughput
  • [ ] Optimize Pydantic models
  • [ ] Use efficient serialization
  • [ ] Implement proper error handling
  • [ ] Monitor memory usage
  • [ ] Configure appropriate timeouts
  • [ ] Use connection pooling
  • [ ] Implement retry logic
  • [ ] Regular performance testing
  • [ ] Monitor cache hit rates
  • [ ] Clear cache when needed
  • [ ] Use async operations where appropriate
  • [ ] Optimize database queries
  • [ ] Monitor external service latency