Measure WordPress loading time and queries

How to measure WordPress’ loading time and executed database queries? During an HTTP request, WordPress executes a lot of queries on your MySQL database. Not just the database queries take time, also loading and executing PHP takes time. How do you measure this?

WordPress has built in functions to measure PHP‘s loading time and executed MySQL queries. These functions are: get_num_queries() for the number of database queries, and timer_stop() for PHP execution time. You can read all about these functions here and here.

You can simply add the following PHP code somewhere in your theme footer.php file to display these statistics:

<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.

After saving this change to your footer.php file, you need to refresh your page in the browser to see the measurements in effect. These numbers give you an idea of how long it took PHP to render your WordPress blog or website.

If you don’t want to mess with theme files, because changes are undone after an update, you can use the wp_footer() action of the Plugin API / Action Reference.

Add to your theme functions.php or your site-specific plugin:

function saotn_php_loadtime_num_queries_footer() {
  echo '<p><small>', get_num_queries(), __(' queries, ');
    timer_stop(1);
  echo __(' seconds.'), '</small></p>';
}
add_action('wp_footer', 'saotn_php_loadtime_num_queries_footer', 100);

Note: this is an older post, moved from another blog of mine.


Show your support


If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ($5 USD for example). And please share the love and help others make use of this website. Thank you very much!

This may interest you:   MySQL in WordPress 3.9

About the Author Jan Reilink

My name is Jan. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing my daily thing at Vevida in the Netherlands. With over 15 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization.

follow me on:

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of