Changeset 42343 for trunk/tests/phpunit/includes/wp-profiler.php
- Timestamp:
- 11/30/2017 11:09:33 PM (9 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/includes/wp-profiler.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/wp-profiler.php
r33123 r42343 26 26 */ 27 27 function __construct() { 28 $this->stack = array();28 $this->stack = array(); 29 29 $this->profile = array(); 30 30 } 31 31 32 function start( $name) {32 function start( $name ) { 33 33 $time = $this->microtime(); 34 34 35 if ( !$this->stack) {35 if ( ! $this->stack ) { 36 36 // log all actions and filters 37 add_filter( 'all', array($this, 'log_filter'));37 add_filter( 'all', array( $this, 'log_filter' ) ); 38 38 } 39 39 40 40 // reset the wpdb queries log, storing it on the profile stack if necessary 41 41 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; 44 44 } 45 45 $wpdb->queries = array(); … … 48 48 49 49 $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(), 59 59 ); 60 60 … … 62 62 63 63 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'] ); 66 66 $name = $item['name']; 67 67 … … 70 70 global $wp_object_cache; 71 71 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'] ); 85 85 #$this->_query_summary($item['queries'], $this->profile[$name]['queries']); 86 86 87 } 88 else { 87 } else { 89 88 $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, 102 101 ); 103 102 } 104 103 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 ) { 117 116 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 } 122 122 } 123 123 return $arg; 124 124 } 125 125 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 } 129 130 } 130 131 131 132 function _current_action() { 132 133 global $wp_actions; 133 return $wp_actions[ count($wp_actions)-1];134 return $wp_actions[ count( $wp_actions ) - 1 ]; 134 135 } 135 136 … … 138 139 } 139 140 140 function _query_summary( $queries, &$out) {141 foreach ( $queries as $q) {141 function _query_summary( $queries, &$out ) { 142 foreach ( $queries as $q ) { 142 143 $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 ); 149 150 return; 150 151 } 151 152 152 function _query_count( $queries) {153 function _query_count( $queries ) { 153 154 // this requires the savequeries patch at https://core.trac.wordpress.org/ticket/5218 154 155 $out = array(); 155 foreach ( $queries as $q) {156 if ( empty($q[2]))156 foreach ( $queries as $q ) { 157 if ( empty( $q[2] ) ) { 157 158 @$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 ) { 165 167 $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 ) { 172 175 $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 ) { 182 187 $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 } 186 193 return $out; 187 194 } … … 190 197 $results = $this->results(); 191 198 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'] ) ); 195 202 } 196 203 } … … 200 207 $wppf = new WPProfiler(); 201 208 202 function wppf_start( $name) {203 $GLOBALS['wppf']->start( $name);209 function wppf_start( $name ) { 210 $GLOBALS['wppf']->start( $name ); 204 211 } 205 212 … … 216 223 } 217 224 218 ?> 225
Note: See TracChangeset
for help on using the changeset viewer.