How to Get Website Performance Info

In developing websites, I have used a number of tools to get Website Performance info. Some of the tools can monitor website browser performance others can monitor database queries, memory consumption and page load time.

When a WordPress site takes several seconds to load or there is a high CPU load, how do you find out the cause? First of all – check all queries, check the memory impacts and then locate the bottleneck.

Website Performance Info About Number of Queries and Page Load Time

You can use plugins to get this information or paste the following code at the end of footer.php (for your current theme), right before closing tag.

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.

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.
‘); ?>
‘); ?>
‘; ?>
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

Website Performance Info About Memory Usage

echo ‘MB Peak Memory Used: ‘.round(memory_get_peak_usage() / 1024 / 1024, 3).’
‘;
?>
memory_get_peak_usage – returns the peak of memory allocated by PHP
And a slightly reformatted code:
echo ‘queries: ‘.get_num_queries().’
‘;
echo ‘Page generation took: ‘.timer_stop().’
‘;
echo ‘MB Peak Memory Used: ‘.round(memory_get_peak_usage() / 1024 / 1024, 3).’
‘;
?>

Website Performance Info About Database Queries

WARNING: As this code will have an impact on performance, test on a development site.

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.

Insert in footer.php:
global $wpdb;
echo ”

";
    print_r($wpdb->queries);
    echo "

“;
?>
//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
)
//Only show to administrator

if (current_user_can(‘administrator’))
{
global $wpdb;

echo ‘queries: ‘.get_num_queries().’
‘;
echo ‘Page generation took: ‘.timer_stop().’
‘;
echo ‘MB Peak Memory Used: ‘.round(memory_get_peak_usage() / 1024 / 1024, 3).’
‘;

echo ”

";
    print_r($wpdb->queries);
    echo "

“;
}

COMING SOON: Improve code for better data presentation.