Skip to main content

Performance Optimization

This guide covers performance optimization techniques when using WP LLM generated code in WordPress applications. Learn how to ensure your AI-generated code performs optimally in production environments.

🎯 Performance Principles

Database Optimization

Asset Management

  • CSS/JS Optimization: Minify and combine assets using Webpack or Gulp
  • Image Optimization: Use appropriate formats like WebP and sizes
  • Lazy Loading: Implement lazy loading for images and content using Intersection Observer API

Caching Strategies

🔧 WP LLM Performance Best Practices

Generated Code Optimization

php
/**
 * Optimized custom post type query with performance considerations
 * 
 * This function demonstrates best practices for WP_Query optimization:
 * - Uses no_found_rows to skip pagination count
 * - Disables unnecessary cache updates
 * - Implements proper meta query optimization
 * - Follows WordPress coding standards
 * 
 * @param array $args Additional query arguments
 * @return WP_Query Optimized query object
 * 
 * @see https://developer.wordpress.org/reference/classes/wp_query/
 * @see https://developer.wordpress.org/advanced-administration/performance/optimization/
 */
function get_optimized_events($args = array()) {
    $defaults = array(
        'post_type' => 'event',
        'posts_per_page' => 10,
        'meta_query' => array(
            array(
                'key' => 'event_date',
                'value' => date('Y-m-d'),
                'compare' => '>=',
                'type' => 'DATE'
            )
        ),
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'no_found_rows' => true, // Skip pagination count for better performance
        'update_post_meta_cache' => false, // Skip meta cache if not needed
        'update_post_term_cache' => false, // Skip term cache if not needed
    );
    
    return wp_parse_args($args, $defaults);
}

REST API Performance

php
/**
 * Optimized REST API endpoint with performance considerations
 * 
 * This example shows how to create efficient REST API endpoints:
 * - Implements proper parameter validation
 * - Uses optimized queries
 * - Includes rate limiting considerations
 * - Follows WordPress REST API best practices
 * 
 * @see https://developer.wordpress.org/rest-api/
 * @see https://developer.wordpress.org/rest-api/extending-the-rest-api/
 */
function register_optimized_api_endpoint() {
    register_rest_route('wp-llm/v1', '/events', array(
        'methods' => 'GET',
        'callback' => 'get_events_api',
        'permission_callback' => '__return_true',
        'args' => array(
            'per_page' => array(
                'default' => 10,
                'sanitize_callback' => 'absint',
                'validate_callback' => function($param) {
                    return $param <= 100; // Limit to prevent abuse
                }
            )
        )
    ));
}

function get_events_api($request) {
    $per_page = $request->get_param('per_page');
    
    // Use optimized query with performance considerations
    $query = new WP_Query(array(
        'post_type' => 'event',
        'posts_per_page' => $per_page,
        'no_found_rows' => true,
        'update_post_meta_cache' => false,
        'update_post_term_cache' => false,
    ));
    
    return rest_ensure_response($query->posts);
}

📊 Performance Monitoring

Tools and Metrics

Key Metrics to Track

  • Time to First Byte (TTFB): Server response time - should be under 200ms
  • First Contentful Paint (FCP): First content display - should be under 1.8s
  • Largest Contentful Paint (LCP): Main content load time - should be under 2.5s
  • Cumulative Layout Shift (CLS): Visual stability - should be under 0.1
  • First Input Delay (FID): Interactivity responsiveness - should be under 100ms

🚀 Advanced Optimization Techniques

Database Indexing

sql
-- Example: Add indexes for custom post types to improve query performance
-- These indexes help optimize meta queries for custom post types
-- @see https://dev.mysql.com/doc/refman/8.0/en/create-index.html

CREATE INDEX idx_event_date ON wp_postmeta (meta_key, meta_value) 
WHERE meta_key = 'event_date';

CREATE INDEX idx_event_location ON wp_postmeta (meta_key, meta_value) 
WHERE meta_key = 'event_location';

-- Index for taxonomy queries
CREATE INDEX idx_event_category ON wp_term_relationships (object_id, term_taxonomy_id);

Caching Implementation

php
/**
 * Custom caching implementation for generated content
 * 
 * This function demonstrates advanced caching strategies:
 * - Uses WordPress cache API for consistency
 * - Implements cache invalidation
 * - Handles cache misses gracefully
 * - Follows WordPress caching best practices
 * 
 * @param string $category Event category to filter by
 * @return array Cached or fresh event data
 * 
 * @see https://developer.wordpress.org/reference/functions/wp_cache_get/
 * @see https://developer.wordpress.org/reference/functions/wp_cache_set/
 */
function get_cached_events($category = '') {
    $cache_key = 'wp_llm_events_' . sanitize_title($category);
    $cached_events = wp_cache_get($cache_key);
    
    if (false === $cached_events) {
        // Cache miss - generate fresh data
        $events = get_optimized_events(array(
            'meta_query' => array(
                array(
                    'key' => 'event_category',
                    'value' => $category,
                    'compare' => '='
                )
            )
        ));
        
        // Cache for 1 hour
        wp_cache_set($cache_key, $events, '', HOUR_IN_SECONDS);
        return $events;
    }
    
    return $cached_events;
}

/**
 * Cache invalidation function
 * Call this when events are updated to ensure fresh data
 */
function invalidate_events_cache($category = '') {
    $cache_key = 'wp_llm_events_' . sanitize_title($category);
    wp_cache_delete($cache_key);
}

Asset Optimization

php
/**
 * Optimized asset loading with performance considerations
 * 
 * This function implements modern asset optimization techniques:
 * - Defers non-critical CSS loading
 * - Preloads critical resources
 * - Implements resource hints
 * - Follows web performance best practices
 * 
 * @see https://developer.wordpress.org/reference/hooks/style_loader_tag/
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types
 */
function wp_llm_optimize_assets() {
    // Defer non-critical CSS for better performance
    add_filter('style_loader_tag', function($tag, $handle) {
        if ('wp-llm-non-critical' === $handle) {
            return str_replace(
                "rel='stylesheet'", 
                "rel='stylesheet' media='print' onload=\"this.media='all'\"", 
                $tag
            );
        }
        return $tag;
    }, 10, 2);
    
    // Preload critical resources
    add_action('wp_head', function() {
        echo '<link rel="preload" href="' . get_template_directory_uri() . '/assets/critical.css" as="style">';
        echo '<link rel="preload" href="' . get_template_directory_uri() . '/assets/fonts/main-font.woff2" as="font" type="font/woff2" crossorigin>';
    });
    
    // Add resource hints for external resources
    add_action('wp_head', function() {
        echo '<link rel="dns-prefetch" href="//cdn.example.com">';
        echo '<link rel="preconnect" href="//api.example.com">';
    });
}

🔍 Performance Testing

Load Testing

  • Apache Bench (ab): Basic load testing tool
  • Siege: Advanced load testing with realistic scenarios
  • JMeter: Comprehensive performance testing tool
  • K6: Modern load testing tool with JavaScript scripting

Profiling

php
/**
 * Simple performance profiling function
 * 
 * This function helps identify performance bottlenecks:
 * - Measures execution time
 * - Tracks memory usage
 * - Logs performance data
 * - Useful for debugging slow functions
 * 
 * @param string $function_name Name of the function being profiled
 * @param callable $callback Function to profile
 * @return mixed Result of the callback function
 * 
 * @see https://www.php.net/manual/en/function.microtime.php
 * @see https://www.php.net/manual/en/function.memory-get-usage.php
 */
function profile_function($function_name, $callback) {
    $start_time = microtime(true);
    $start_memory = memory_get_usage();
    
    $result = $callback();
    
    $end_time = microtime(true);
    $end_memory = memory_get_usage();
    
    // Log performance data
    error_log(sprintf(
        'Function %s took %.4f seconds and used %d bytes',
        $function_name,
        $end_time - $start_time,
        $end_memory - $start_memory
    ));
    
    return $result;
}

// Usage example
$result = profile_function('get_events', function() {
    return get_optimized_events();
});

📈 Performance Checklist

Before Deployment

Ongoing Monitoring

  • Regular performance audits using GTmetrix
  • Database query monitoring with Query Monitor
  • Cache hit rate tracking
  • User experience metrics from Google Analytics
  • Server resource monitoring
  • Error rate tracking

💡 Tip: Always test performance optimizations in a staging environment before deploying to production. Monitor performance metrics regularly to ensure optimizations remain effective as your application grows. For more information, visit the WordPress Performance Handbook and Web Performance Best Practices.