WordPress Code Snippet: Output Advanced Page Generation Statistics

Here’s a little bit of code to display the percent of a page’s generation time used by PHP and MySQL.

First, add this into your theme’s functions.php file wp-config.php file so that data about each MySQL query is stored, namely the time taken.

define( 'SAVEQUERIES', true );

Then in your footer.php, use this code to display it:

<?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 )";

?>

That will output code something like this: Page generated in 0.24691 seconds ( 95.50% PHP, 4.50% MySQL )

6 thoughts on “WordPress Code Snippet: Output Advanced Page Generation Statistics

  1. Great, i search it 1 hour on google ” page generated in x.xxx seconds wordpress ” and finaly i get to this post.
    Sorry for my bad english, i`m romanian :).

  2. Pingback: Display wordpress page generation/load time statistics

  3. Pingback: Display wordpress page generation/load time statistics | Useful Mix

  4. Pingback: ¿Cuánto tardan PHP y MySQL en generar tu contenido en WordPress? | Samuel Aguilera

  5. Pingback: ¿Cuánto tardan PHP y MySQL en generar tu contenido en WordPress? | Samuel Aguilera

Comments are closed.