Skip to content

Instantly share code, notes, and snippets.

@kinday
Last active Feb 12, 2016
Embed
What would you like to do?
WordPress performance stats
<?php
// Register function if WordPress is in debug mode
if ( true == 'WP_DEBUG' ) {
add_action( 'wp_footer', 'the_performance_stats' );
}
<?php
function get_performance_stats()
{
$stats = '<script>';
$stats .= 'console.info("Queries:", "' . get_num_queries() . '");';
$stats .= 'console.info("Execution time:", "' . timer_stop() . 's");';
$stats .= 'console.info("Memory usage:", "' . round( memory_get_usage() / 1024 / 1024, 2 ) . '", "MB");';
if ( current_user_can( 'administrator' ) ) {
global $wpdb;
$stats .= 'console.group("DB queries");';
foreach ( $wpdb->queries as $query ) {
$stats .= 'console.group();';
foreach ( $query as $query ) {
$stats .= 'console.log("' . $query . '");';
}
$stats .= 'console.groupEnd();';
}
$stats .= 'console.groupEnd();';
}
$stats .= '</script>';
return $stats;
}
function the_performance_stats()
{
echo $this->get_performance_stats();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment