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
- Efficient Queries: Use proper indexing and avoid N+1 queries following WordPress database optimization guidelines
- Caching Strategies: Implement object caching with Redis or Memcached and transients
- Query Optimization: Minimize database calls and optimize WP_Query usage
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
- Object Caching: Use Redis or Memcached for object caching
- Page Caching: Implement full-page caching with WP Rocket or W3 Total Cache
- CDN Integration: Use content delivery networks like Cloudflare or AWS CloudFront for static assets
🔧 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
- Query Monitor: Monitor database queries and performance
- New Relic: Application performance monitoring
- GTmetrix: Page speed analysis
- Google PageSpeed Insights: Core Web Vitals monitoring
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
- Database queries optimized and indexed following MySQL optimization guidelines
- Assets minified and compressed using Webpack or Gulp
- Caching implemented and tested with Redis or Memcached
- Images optimized and properly sized using WebP format
- CDN configured for static assets with Cloudflare or AWS CloudFront
- Performance monitoring tools installed (Query Monitor)
- Load testing completed with Apache Bench or K6
- Core Web Vitals within acceptable ranges (Google PageSpeed Insights)
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.