Display database query count, query time and memory usage in WordPress footer

For those who have used the WP Super Cache plugin, you may know that the WP Super Cache plugin displays some information at the bottom of the cached page to indicate the time used to generate the page and the time to cache the page for the developer's reference.

query

When we are ready to optimize WordPress performance by reducing database queries, this information is obviously not enough, we need to show the parameters database query count, query time and memory usage.

Code needed to display database query count, query time and memory usage

function performance( $visible = false ) {

 $stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
 get_num_queries(),
 memory_get_peak_usage(), memory_get_peak_usage(), memory_get_peak_usage()
 memory_get_peak_usage() / 1024 / 1024
 );

 echo $visible ? $stat : "" ;
}

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

This code is passed through the wp_footer This hook hangs at the bottom of the page, before using it, make sure that the footer.php file of the WordPress theme you are using contains this hook.

Related Posts

Leave a Reply

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