Microtimers On Hooks To Measure WordPress Performance

What’s slowing down your WordPress page? Try adding this to your functions.php to see your pain points in red. Remember to change the IP address in one_of_us() to keep test results private:

// Performance testing
add_action('muplugins_loaded', 'pj_time');
add_action('registered_taxonomy', 'pj_time');
add_action('registered_post_type', 'pj_time');
add_action('plugins_loaded', 'pj_time');
add_action('sanitize_comment_cookies', 'pj_time');
add_action('setup_theme', 'pj_time');
add_action('load_textdomain', 'pj_time');
add_action('after_setup_theme', 'pj_time');
add_action('set_current_user', 'pj_time');
add_action('init', 'pj_time');
add_action('widgets_init', 'pj_time');
add_action('register_sidebar', 'pj_time');
add_action('wp_register_sidebar_widget', 'pj_time');
add_action('wp_default_scripts', 'pj_time');
add_action('wp_default_styles', 'pj_time');
add_action('admin_bar_init', 'pj_time');
add_action('add_admin_bar_menus', 'pj_time');
add_action('wp_loaded', 'pj_time');
add_action('parse_request', 'pj_time');
add_action('send_headers', 'pj_time');
add_action('parse_query', 'pj_time');
add_action('pre_get_posts', 'pj_time');
add_action('posts_selection', 'pj_time');
add_action('wp', 'pj_time');
add_action('template_redirect', 'pj_time');
add_action('wp_loaded', 'pj_time');
add_action('wp_head', 'pj_time');
add_action('wp_footer', 'pj_time');

// More hooks here http://codex.wordpress.org/Plugin_API/Action_Reference
// Check your theme's documentation, it may have more hooks to test.

  function pj_time () { if (one_of_us()) { $old_time = $GLOBALS["performance_timer"]; $GLOBALS["performance_timer"] = microtime(true); $new_time = $GLOBALS["performance_timer"]; $diff = round($new_time - $old_time, 4); // The < 100 seconds just hides the ugly starting time hook // Increase .01 to .1 seconds to see only the worst pain points. if (($diff > 0.01) && ($diff < 100)) { echo '<div style="color:red;">'; echo 'Test time of '. current_filter() .': '. $diff .'s. '; echo '</div>'; } } }
  function one_of_us() { if ($_SERVER['REMOTE_ADDR']=="x.x.x.x") return true; return false; }
If there’s a slow spot, the output will look something like this: Test time of parse_query: 0.123s. The time is measured from hook to hook. So adding more hooks helps you pinpoint what’s slow.