- Home
- About
- Archives
- My WordPress Plugins
- Bulk Change Attachment Parent
- Clean Archives Reloaded
- Disable Trackbacks
- jQuery Lightbox For Native Galleries
- Local Time
- Regenerate Thumbnails
- Registered Users Only
- SyntaxHighlighter Evolved
- Viper’s Video Quicktags
- WordPress Admin Bar
- WordPress Download Counter
- WordPress.org One-Click Install
- YOURLS: Short URL Widget
- Other Plugins
- Discontinued Plugins
- Donate
- Contact
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 so that data about each MySQL query is stored, namely the time taken.
<?php define( 'SAVEQUERIES', true ); ?>
Then in your footer.php, use this code to display it: (click the
icon to view the unformatted code)
<?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 )
Tweet| Print article | This entry was posted by Alex (Viper007Bond) on May 4th, 2009 at 1:48 AM, and is filed under WordPress Code & Hacks. Follow any responses to this post through RSS 2.0. You can leave a response or pingback from your own site. |
