Blog / WordPress/ WordPress Performance Monitoring: Track Query Count, Execution Time & Memory Usage

WordPress Performance Monitoring: Track Query Count, Execution Time & Memory Usage

WordPress 性能监控指南:实时查看查询次数、执行时间与内存占用

WordPress Performance Monitoring: Query Count, Execution Time & Memory Usage

When developing or optimizing WordPress, it's helpful to know the number of database queries, PHP execution time, and memory consumption for a page. This guide introduces a simple, practical function to display or hide these performance metrics.

Core Performance Monitoring Function

Add the following code to your current theme's functions.php file:

// Display page query count, load time, and memory usage
function performance( $visible = false ) {
    $stat = sprintf(
        '%d queries in %.3f seconds, using %.2fMB memory',
        get_num_queries(),
        timer_stop( 0, 3 ),
        memory_get_peak_usage() / 1024 / 1024
    );
    echo $visible ? $stat : "";
}

Function Explanation:

  • get_num_queries(): Gets the total number of database queries executed during the current page load.
  • timer_stop( 0, 3 ): Returns the PHP execution time (in seconds) from the start of page load until this function is called. The parameter 3 specifies three decimal places.
  • memory_get_peak_usage(): Returns the peak memory consumption (in bytes) during PHP script execution. Dividing by 1024 / 1024 converts it to MB.
  • Parameter $visible: Controls output. When true, stats are displayed directly on the page. When false, they are output as an HTML comment, visible only in the page source.

How to Call the Function

Insert the following call where you want the performance data to appear (e.g., in your theme's footer.php file):

<?php if (function_exists('performance')) performance(false); ?>

Change the parameter from false to true to display the statistics directly on the front end.

Recommended Method: Automatic Loading via Hook

A better approach is to hook the performance monitoring function to WordPress's wp_footer action, so it loads automatically in the page footer. Ensure your theme template (usually footer.php) includes the <?php wp_footer(); ?> call.

After defining the performance function in functions.php, add this code:

add_action( 'wp_footer', 'performance', 20 );

By default, this outputs the stats as an HTML comment. To display them on the front end, modify the performance function to have a default parameter of true, or use a filter for dynamic control.

Advanced Usage: Dynamic Display Control

For more flexible control, use this enhanced version:

// Enhanced version: visibility can be controlled dynamically via a filter
function performance_enhanced() {
    // Default: show for administrators on front end, hide for visitors (source only)
    $visible = apply_filters( 'show_performance_stats', current_user_can( 'manage_options' ) );
    $stat = sprintf(
        '%d queries in %.3f seconds, using %.2fMB memory',
        get_num_queries(),
        timer_stop( 0, 3 ),
        memory_get_peak_usage() / 1024 / 1024
    );
    echo $visible ? '<div class="performance-stats">' . esc_html($stat) . '</div>' : "";
}
add_action( 'wp_footer', 'performance_enhanced', 20 );

This version allows administrators to see performance data directly on the front end, while regular visitors cannot. You can also enable front-end display for all users by adding add_filter( 'show_performance_stats', '__return_true' );.

Note: This functionality is primarily for development and debugging. On production sites, it's recommended to disable front-end display or restrict it to administrators to avoid exposing unnecessary system information.

Post a Comment

Your email will not be published. Required fields are marked with *.