Skip to content

Instantly share code, notes, and snippets.

@hanicker
Forked from Viper007Bond/whatissoslow.php
Last active March 7, 2021 14:59
Show Gist options
  • Select an option

  • Save hanicker/f20192bee517171178b062a7f9e6b1f8 to your computer and use it in GitHub Desktop.

Select an option

Save hanicker/f20192bee517171178b062a7f9e6b1f8 to your computer and use it in GitHub Desktop.
WordPress: Times how long it takes each filter and action to run and displays results at the end of the page. Quick and dirty.
<?php
/**
* This little class records how long it takes each WordPress action or filter
* to execute which gives a good indicator of what hooks are being slow.
* You can then debug those hooks to see what hooked functions are causing problems.
*
* This class does NOT time the core WordPress code that is being run between hooks.
* You could use similar code to this that doesn't have an end processor to do that.
*/
// Here's a test to make sure it's working
add_action( 'wp_footer', function() { sleep( 2 ); } );
class WhatIsSoSlow {
public $data = array();
function __construct() {
add_action( 'all', array( $this, 'filter_start' ) );
add_action( 'shutdown', array( $this, 'results' ) );
}
// This runs first for all actions and filters.
// It starts a timer for this hook.
public function filter_start() {
$current_filter = current_filter();
$this->data[ $current_filter ][]['start'] = microtime( true );
add_filter( $current_filter, array( $this, 'filter_end' ), 99999 );
}
// This runs last (hopefully) for each hook and records the end time.
// This has problems if a hook fires inside of itself since it assumes
// the last entry in the data key for this hook is the matching pair.
public function filter_end( $filter_data ) {
$current_filter = current_filter();
remove_filter( $current_filter, array( $this, 'filter_end' ), 99999 );
end( $this->data[ $current_filter ] );
$last_key = key( $this->data[ $current_filter ] );
$this->data[ $current_filter ][ $last_key ]['stop'] = microtime( true );
return $filter_data;
}
// Processes the results and var_dump()'s them. TODO: Debug bar panel?
public function results() {
$results = array();
foreach ( $this->data as $filter => $calls ) {
foreach ( $calls as $call ) {
// Skip filters with no end point (i.e. the hook this function is hooked into)
if ( ! isset( $call['stop'] ) )
continue;
if ( ! isset( $results[ $filter ] ) )
$results[ $filter ] = 0;
$results[ $filter ] = $results[ $filter ] + ( $call['stop'] - $call['start'] );
}
}
asort( $results, SORT_NUMERIC );
$results = array_reverse( $results );
var_dump( $results );
if (defined('SAVEQUERIES') && SAVEQUERIES) {
global $wpdb;
if (is_array($wpdb->queries)) foreach ($wpdb->queries as $key => $q) {
list($query, $elapsed, $debug) = $q;
$time = number_format(($elapsed * 1000), 3);
$count = $key + 1;
$total_time += $elapsed;
echo "
<div style=\"position: relative; z-index: 9999 ; background: black; color: white; padding:10px\">
$count - Query: $query <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time: $time ms
</div>";
}
echo "
<div style=\"position: relative; z-index: 9999 ; background: black; color: white; padding:10px\">
Total Queries: " . count($wpdb->queries) . "<br>Total Time: " . number_format(($total_time * 1000), 3) . " ms
</div>";
}
}
}
$ip = null;
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
if($ip == ''){
new WhatIsSoSlow();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment