Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/wp-profiler.php

    r33123 r42343  
    2626     */
    2727    function __construct() {
    28         $this->stack = array();
     28        $this->stack   = array();
    2929        $this->profile = array();
    3030    }
    3131
    32     function start($name) {
     32    function start( $name ) {
    3333        $time = $this->microtime();
    3434
    35         if (!$this->stack) {
     35        if ( ! $this->stack ) {
    3636            // log all actions and filters
    37             add_filter('all', array($this, 'log_filter'));
     37            add_filter( 'all', array( $this, 'log_filter' ) );
    3838        }
    3939
    4040        // reset the wpdb queries log, storing it on the profile stack if necessary
    4141        global $wpdb;
    42         if ($this->stack) {
    43             $this->stack[count($this->stack)-1]['queries'] = $wpdb->queries;
     42        if ( $this->stack ) {
     43            $this->stack[ count( $this->stack ) - 1 ]['queries'] = $wpdb->queries;
    4444        }
    4545        $wpdb->queries = array();
     
    4848
    4949        $this->stack[] = array(
    50             'start' => $time,
    51             'name' => $name,
    52             'cache_cold_hits' => $wp_object_cache->cold_cache_hits,
    53             'cache_warm_hits' => $wp_object_cache->warm_cache_hits,
    54             'cache_misses' => $wp_object_cache->cache_misses,
    55             'cache_dirty_objects' => $this->_dirty_objects_count($wp_object_cache->dirty_objects),
    56             'actions' => array(),
    57             'filters' => array(),
    58             'queries' => array(),
     50            'start'               => $time,
     51            'name'                => $name,
     52            'cache_cold_hits'     => $wp_object_cache->cold_cache_hits,
     53            'cache_warm_hits'     => $wp_object_cache->warm_cache_hits,
     54            'cache_misses'        => $wp_object_cache->cache_misses,
     55            'cache_dirty_objects' => $this->_dirty_objects_count( $wp_object_cache->dirty_objects ),
     56            'actions'             => array(),
     57            'filters'             => array(),
     58            'queries'             => array(),
    5959        );
    6060
     
    6262
    6363    function stop() {
    64         $item = array_pop($this->stack);
    65         $time = $this->microtime($item['start']);
     64        $item = array_pop( $this->stack );
     65        $time = $this->microtime( $item['start'] );
    6666        $name = $item['name'];
    6767
     
    7070        global $wp_object_cache;
    7171
    72         $cache_dirty_count = $this->_dirty_objects_count($wp_object_cache->dirty_objects);
    73         $cache_dirty_delta = $this->array_sub($cache_dirty_count, $item['cache_dirty_objects']);
    74 
    75         if (isset($this->profile[$name])) {
    76             $this->profile[$name]['time'] += $time;
    77             $this->profile[$name]['calls'] ++;
    78             $this->profile[$name]['cache_cold_hits'] += ($wp_object_cache->cold_cache_hits - $item['cache_cold_hits']);
    79             $this->profile[$name]['cache_warm_hits'] += ($wp_object_cache->warm_cache_hits - $item['cache_warm_hits']);
    80             $this->profile[$name]['cache_misses'] += ($wp_object_cache->cache_misses - $item['cache_misses']);
    81             $this->profile[$name]['cache_dirty_objects'] = array_add( $this->profile[$name]['cache_dirty_objects'], $cache_dirty_delta) ;
    82             $this->profile[$name]['actions'] = array_add( $this->profile[$name]['actions'], $item['actions'] );
    83             $this->profile[$name]['filters'] = array_add( $this->profile[$name]['filters'], $item['filters'] );
    84             $this->profile[$name]['queries'] = array_add( $this->profile[$name]['queries'], $item['queries'] );
     72        $cache_dirty_count = $this->_dirty_objects_count( $wp_object_cache->dirty_objects );
     73        $cache_dirty_delta = $this->array_sub( $cache_dirty_count, $item['cache_dirty_objects'] );
     74
     75        if ( isset( $this->profile[ $name ] ) ) {
     76            $this->profile[ $name ]['time'] += $time;
     77            $this->profile[ $name ]['calls'] ++;
     78            $this->profile[ $name ]['cache_cold_hits']    += ( $wp_object_cache->cold_cache_hits - $item['cache_cold_hits'] );
     79            $this->profile[ $name ]['cache_warm_hits']    += ( $wp_object_cache->warm_cache_hits - $item['cache_warm_hits'] );
     80            $this->profile[ $name ]['cache_misses']       += ( $wp_object_cache->cache_misses - $item['cache_misses'] );
     81            $this->profile[ $name ]['cache_dirty_objects'] = array_add( $this->profile[ $name ]['cache_dirty_objects'], $cache_dirty_delta );
     82            $this->profile[ $name ]['actions']             = array_add( $this->profile[ $name ]['actions'], $item['actions'] );
     83            $this->profile[ $name ]['filters']             = array_add( $this->profile[ $name ]['filters'], $item['filters'] );
     84            $this->profile[ $name ]['queries']             = array_add( $this->profile[ $name ]['queries'], $item['queries'] );
    8585            #$this->_query_summary($item['queries'], $this->profile[$name]['queries']);
    8686
    87         }
    88         else {
     87        } else {
    8988            $queries = array();
    90             $this->_query_summary($item['queries'], $queries);
    91             $this->profile[$name] = array(
    92                 'time' => $time,
    93                 'calls' => 1,
    94                 'cache_cold_hits' => ($wp_object_cache->cold_cache_hits - $item['cache_cold_hits']),
    95                 'cache_warm_hits' => ($wp_object_cache->warm_cache_hits - $item['cache_warm_hits']),
    96                 'cache_misses' => ($wp_object_cache->cache_misses - $item['cache_misses']),
    97                 'cache_dirty_objects' => $cache_dirty_delta,
    98                 'actions' => $item['actions'],
    99                 'filters' => $item['filters'],
    100 #               'queries' => $item['queries'],
    101                 'queries' => $queries,
     89            $this->_query_summary( $item['queries'], $queries );
     90            $this->profile[ $name ] = array(
     91                'time'                        => $time,
     92                'calls'                       => 1,
     93                'cache_cold_hits'             => ( $wp_object_cache->cold_cache_hits - $item['cache_cold_hits'] ),
     94                'cache_warm_hits'             => ( $wp_object_cache->warm_cache_hits - $item['cache_warm_hits'] ),
     95                'cache_misses'                => ( $wp_object_cache->cache_misses - $item['cache_misses'] ),
     96                'cache_dirty_objects'         => $cache_dirty_delta,
     97                'actions'                     => $item['actions'],
     98                'filters'                     => $item['filters'],
     99                #               'queries' => $item['queries'],
     100                                    'queries' => $queries,
    102101            );
    103102        }
    104103
    105         if (!$this->stack) {
    106             remove_filter('all', array($this, 'log_filter'));
    107         }
    108     }
    109 
    110     function microtime($since = 0.0) {
    111         list($usec, $sec) = explode(' ', microtime());
    112         return (float)$sec + (float)$usec - $since;
    113     }
    114 
    115     function log_filter($tag) {
    116         if ($this->stack) {
     104        if ( ! $this->stack ) {
     105            remove_filter( 'all', array( $this, 'log_filter' ) );
     106        }
     107    }
     108
     109    function microtime( $since = 0.0 ) {
     110        list($usec, $sec) = explode( ' ', microtime() );
     111        return (float) $sec + (float) $usec - $since;
     112    }
     113
     114    function log_filter( $tag ) {
     115        if ( $this->stack ) {
    117116            global $wp_actions;
    118             if ($tag == end($wp_actions))
    119                 @$this->stack[count($this->stack)-1]['actions'][$tag] ++;
    120             else
    121                 @$this->stack[count($this->stack)-1]['filters'][$tag] ++;
     117            if ( $tag == end( $wp_actions ) ) {
     118                @$this->stack[ count( $this->stack ) - 1 ]['actions'][ $tag ] ++;
     119            } else {
     120                @$this->stack[ count( $this->stack ) - 1 ]['filters'][ $tag ] ++;
     121            }
    122122        }
    123123        return $arg;
    124124    }
    125125
    126     function log_action($tag) {
    127         if ($this->stack)
    128             @$this->stack[count($this->stack)-1]['actions'][$tag] ++;
     126    function log_action( $tag ) {
     127        if ( $this->stack ) {
     128            @$this->stack[ count( $this->stack ) - 1 ]['actions'][ $tag ] ++;
     129        }
    129130    }
    130131
    131132    function _current_action() {
    132133        global $wp_actions;
    133         return $wp_actions[count($wp_actions)-1];
     134        return $wp_actions[ count( $wp_actions ) - 1 ];
    134135    }
    135136
     
    138139    }
    139140
    140     function _query_summary($queries, &$out) {
    141         foreach ($queries as $q) {
     141    function _query_summary( $queries, &$out ) {
     142        foreach ( $queries as $q ) {
    142143            $sql = $q[0];
    143             $sql = preg_replace('/(WHERE \w+ =) \d+/', '$1 x', $sql);
    144             $sql = preg_replace('/(WHERE \w+ =) \'\[-\w]+\'/', '$1 \'xxx\'', $sql);
    145 
    146             @$out[$sql] ++;
    147         }
    148         asort($out);
     144            $sql = preg_replace( '/(WHERE \w+ =) \d+/', '$1 x', $sql );
     145            $sql = preg_replace( '/(WHERE \w+ =) \'\[-\w]+\'/', '$1 \'xxx\'', $sql );
     146
     147            @$out[ $sql ] ++;
     148        }
     149        asort( $out );
    149150        return;
    150151    }
    151152
    152     function _query_count($queries) {
     153    function _query_count( $queries ) {
    153154        // this requires the savequeries patch at https://core.trac.wordpress.org/ticket/5218
    154155        $out = array();
    155         foreach ($queries as $q) {
    156             if (empty($q[2]))
     156        foreach ( $queries as $q ) {
     157            if ( empty( $q[2] ) ) {
    157158                @$out['unknown'] ++;
    158             else
    159                 @$out[$q[2]] ++;
    160         }
    161         return $out;
    162     }
    163 
    164     function _dirty_objects_count($dirty_objects) {
     159            } else {
     160                @$out[ $q[2] ] ++;
     161            }
     162        }
     163        return $out;
     164    }
     165
     166    function _dirty_objects_count( $dirty_objects ) {
    165167        $out = array();
    166         foreach (array_keys($dirty_objects) as $group)
    167             $out[$group] = count($dirty_objects[$group]);
    168         return $out;
    169     }
    170 
    171     function array_add($a, $b) {
     168        foreach ( array_keys( $dirty_objects ) as $group ) {
     169            $out[ $group ] = count( $dirty_objects[ $group ] );
     170        }
     171        return $out;
     172    }
     173
     174    function array_add( $a, $b ) {
    172175        $out = $a;
    173         foreach (array_keys($b) as $key)
    174             if (array_key_exists($key, $out))
    175                 $out[$key] += $b[$key];
    176             else
    177                 $out[$key] = $b[$key];
    178         return $out;
    179     }
    180 
    181     function array_sub($a, $b) {
     176        foreach ( array_keys( $b ) as $key ) {
     177            if ( array_key_exists( $key, $out ) ) {
     178                $out[ $key ] += $b[ $key ];
     179            } else {
     180                $out[ $key ] = $b[ $key ];
     181            }
     182        }
     183        return $out;
     184    }
     185
     186    function array_sub( $a, $b ) {
    182187        $out = $a;
    183         foreach (array_keys($b) as $key)
    184             if (array_key_exists($key, $b))
    185                 $out[$key] -= $b[$key];
     188        foreach ( array_keys( $b ) as $key ) {
     189            if ( array_key_exists( $key, $b ) ) {
     190                $out[ $key ] -= $b[ $key ];
     191            }
     192        }
    186193        return $out;
    187194    }
     
    190197        $results = $this->results();
    191198
    192         printf("\nname                      calls   time action filter   warm   cold misses  dirty\n");
    193         foreach ($results as $name=>$stats) {
    194             printf("%24.24s %6d %6.4f %6d %6d %6d %6d %6d %6d\n", $name, $stats['calls'], $stats['time'], array_sum($stats['actions']), array_sum($stats['filters']), $stats['cache_warm_hits'], $stats['cache_cold_hits'], $stats['cache_misses'], array_sum($stats['cache_dirty_objects']));
     199        printf( "\nname                      calls   time action filter   warm   cold misses  dirty\n" );
     200        foreach ( $results as $name => $stats ) {
     201            printf( "%24.24s %6d %6.4f %6d %6d %6d %6d %6d %6d\n", $name, $stats['calls'], $stats['time'], array_sum( $stats['actions'] ), array_sum( $stats['filters'] ), $stats['cache_warm_hits'], $stats['cache_cold_hits'], $stats['cache_misses'], array_sum( $stats['cache_dirty_objects'] ) );
    195202        }
    196203    }
     
    200207$wppf = new WPProfiler();
    201208
    202 function wppf_start($name) {
    203     $GLOBALS['wppf']->start($name);
     209function wppf_start( $name ) {
     210    $GLOBALS['wppf']->start( $name );
    204211}
    205212
     
    216223}
    217224
    218 ?>
     225
Note: See TracChangeset for help on using the changeset viewer.