Display performance stats in WordPress
Here is how to display the page-generation time based on PHP and MySQL.
All you need is this in your site’s wp-config.php
file:
define( 'SAVEQUERIES', true );
..and then this in your theme’s footer.php
file (or wherever):
<?php
global $wpdb;
// Get the total page generation time
$totaltime = timer_stop( false, 22 );
// Calculate the time spent on MySQL queries by adding up the time spent on each query
$mysqltime = 0;
foreach ( $wpdb->queries as $query )
$mysqltime = $mysqltime + $query[1];
// The time spent on PHP is the remainder
$phptime = $totaltime - $mysqltime;
// Create the percentages
$mysqlper = number_format_i18n( $mysqltime / $totaltime * 100, 2 );
$phpper = number_format_i18n( $phptime / $totaltime * 100, 2 );
// Output the stats
echo 'Page generated in ' . number_format_i18n( $totaltime, 5 ) . " seconds ( {$phpper}% PHP, {$mysqlper}% MySQL )";
?>
This will display the results like so:
Page generated in 0.24691 seconds ( 95.50% PHP, 4.50% MySQL )