WordPress. Better way to Get Number of Queries and Page Load Time

0

Information about database queries, memory consumption and page load time can greatly help any developer and even common user. Lets examine situation when your wordpress theme takes several seconds to load themselves, or while loading something cause a high loading of db. How to identify where is a problem? First of all – check all queries, check memory and then locate bottleneck.
There are dozen plugins which adds data like page loading time or queries count to WP admin panel or show them below footer. You can often find this in the footer of many sites and it may say something like: “db - 80 queries; loading time - 2.2 seconds”.
But in a case of performance and, as we also note it, better understanding of WP engine (how to deal with WP function and so on) we can retrieve all what we need by utilizing our own code.


The easiest way is to paste the following code at the end of footer.php (for your current theme), right before closing tag.
<?php 
   echo (get_num_queries() . ' queries in ' . timer_stop() .' seconds');
?>

Save changes and refresh the page. You will get at the bottom the number of queries and the execution time, something like this: 20 queries in 0.305 seconds

Now a little bit about these functions.
get_num_queries() - retrieve the number of database queries during the WordPress execution (located in wp-includes/functions.php.)

timer_stop() - returns the amount of time (in seconds) to generate the page. Function timer_stop has two parameters: display and precision.

<?php echo('Seconds: '.timer_stop( 0 ).'<br />'); ?>
<?php echo('Seconds: '.timer_stop( 0, 5 ).'<br />'); ?>
<?php echo('Seconds: '.timer_stop( 0, 10 ).'<br />'; ?>

Precision is an integer value which determines the accuracy of timer_stop result.

As for example above, we will get:

Seconds: 0.815         // precision of 3 digits
Seconds: 0.81551       // precision of 5 digits
Seconds: 0.8155429363  // precision of 10 digits


Should we finish with that? Probably not, ‘cuz we need more data.
What about memory usage?
<?php 
   echo 'MB Peak Memory Used: '.round(memory_get_peak_usage() / 1024 / 1024, 3).'<br />';
?>

memory_get_peak_usage - returns the peak of memory allocated by PHP

And a slightly reformatted code:

<?php
    echo 'queries: '.get_num_queries().'<br />';
    echo 'Page generation took: '.timer_stop().'<br />';	
    echo 'MB Peak Memory Used: '.round(memory_get_peak_usage() / 1024 / 1024, 3).'<br />';
?>


Now we will dig into database queries.

Note: following code should not be used on a live site, it have a performance impact.

Let’s define 'SAVEQUERIES' in wp-config.php.

define('SAVEQUERIES', true );

The SAVEQUERIES definition saves the database queries to a array and that array can be displayed to help analyze those queries. The information saves each query, what function called it, and how long that query took to execute.

Then in the footer of your theme put this:

<?php
    global $wpdb;
    echo "<pre wp-pre-tag-0></pre>";
?>

Now we have a full list of all queries:

[0] => Array
        (
            [0] => SELECT option_name, option_value FROM options WHERE autoload = 'yes'
            [1] => 0.00123000144958
            [2] => require, require_once, require_once, require_once, wp_not_installed, is_blog_installed, wp_load_alloptions
        )

But wait a minute! These data shouldn’t be shown to everyone. By wrapping code with condition

if (current_user_can('administrator'))
{ 
   . . .
}

We will protect critical data.

So the current variant of code is:

if (current_user_can('administrator'))
{ 
    global $wpdb;

    echo 'queries: '.get_num_queries().'<br />';
    echo 'Page generation took: '.timer_stop().'<br />';	
    echo 'MB Peak Memory Used: '.round(memory_get_peak_usage() / 1024 / 1024, 3).'<br />';

    echo "<pre wp-pre-tag-1></pre>";
}

Perfect, now we have a code and we can examine WP engine behavior.

Next time we will improve this code again and decorate it for better data presentation.

LEAVE A REPLY

Please enter your comment!
Please enter your name here