patilswapnilv /
shortcodely
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | A simple manually-instrumented profiler for WordPress. |
||
| 5 | |||
| 6 | This records basic execution time, and a summary of the actions and SQL queries run within each block. |
||
| 7 | |||
| 8 | start() and stop() must be called in pairs, for example: |
||
| 9 | |||
| 10 | function something_to_profile() { |
||
| 11 | wppf_start(__FUNCTION__); |
||
| 12 | do_stuff(); |
||
| 13 | wppf_stop(); |
||
| 14 | } |
||
| 15 | |||
| 16 | Multiple profile blocks are permitted, and they may be nested. |
||
| 17 | |||
| 18 | */ |
||
| 19 | |||
| 20 | class WPProfiler |
||
| 21 | { |
||
| 22 | var $stack; |
||
| 23 | var $profile; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * PHP5 constructor. |
||
| 27 | */ |
||
| 28 | function __construct() |
||
| 29 | { |
||
| 30 | $this->stack = array(); |
||
| 31 | $this->profile = array(); |
||
| 32 | } |
||
| 33 | |||
| 34 | function start($name) |
||
| 35 | { |
||
| 36 | $time = $this->microtime(); |
||
| 37 | |||
| 38 | if (!$this->stack) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 39 | // log all actions and filters |
||
| 40 | add_filter('all', array($this, 'log_filter')); |
||
| 41 | } |
||
| 42 | |||
| 43 | // reset the wpdb queries log, storing it on the profile stack if necessary |
||
| 44 | global $wpdb; |
||
| 45 | if ($this->stack) { |
||
|
0 ignored issues
–
show
|
|||
| 46 | $this->stack[count($this->stack)-1]['queries'] = $wpdb->queries; |
||
|
0 ignored issues
–
show
|
|||
| 47 | } |
||
| 48 | $wpdb->queries = array(); |
||
| 49 | |||
| 50 | global $wp_object_cache; |
||
| 51 | |||
| 52 | $this->stack[] = array( |
||
| 53 | 'start' => $time, |
||
| 54 | 'name' => $name, |
||
| 55 | 'cache_cold_hits' => $wp_object_cache->cold_cache_hits, |
||
| 56 | 'cache_warm_hits' => $wp_object_cache->warm_cache_hits, |
||
| 57 | 'cache_misses' => $wp_object_cache->cache_misses, |
||
| 58 | 'cache_dirty_objects' => $this->_dirty_objects_count($wp_object_cache->dirty_objects), |
||
| 59 | 'actions' => array(), |
||
| 60 | 'filters' => array(), |
||
| 61 | 'queries' => array(), |
||
| 62 | ); |
||
| 63 | |||
| 64 | } |
||
| 65 | |||
| 66 | function stop() |
||
| 67 | { |
||
| 68 | $item = array_pop($this->stack); |
||
| 69 | $time = $this->microtime($item['start']); |
||
| 70 | $name = $item['name']; |
||
| 71 | |||
| 72 | global $wpdb; |
||
| 73 | $item['queries'] = $wpdb->queries; |
||
| 74 | global $wp_object_cache; |
||
| 75 | |||
| 76 | $cache_dirty_count = $this->_dirty_objects_count($wp_object_cache->dirty_objects); |
||
| 77 | $cache_dirty_delta = $this->array_sub($cache_dirty_count, $item['cache_dirty_objects']); |
||
| 78 | |||
| 79 | if (isset($this->profile[$name])) { |
||
|
0 ignored issues
–
show
|
|||
| 80 | $this->profile[$name]['time'] += $time; |
||
|
0 ignored issues
–
show
|
|||
| 81 | $this->profile[$name]['calls'] ++; |
||
|
0 ignored issues
–
show
|
|||
| 82 | $this->profile[$name]['cache_cold_hits'] += ($wp_object_cache->cold_cache_hits - $item['cache_cold_hits']); |
||
|
0 ignored issues
–
show
|
|||
| 83 | $this->profile[$name]['cache_warm_hits'] += ($wp_object_cache->warm_cache_hits - $item['cache_warm_hits']); |
||
|
0 ignored issues
–
show
|
|||
| 84 | $this->profile[$name]['cache_misses'] += ($wp_object_cache->cache_misses - $item['cache_misses']); |
||
|
0 ignored issues
–
show
|
|||
| 85 | $this->profile[$name]['cache_dirty_objects'] = array_add($this->profile[$name]['cache_dirty_objects'], $cache_dirty_delta); |
||
|
0 ignored issues
–
show
|
|||
| 86 | $this->profile[$name]['actions'] = array_add($this->profile[$name]['actions'], $item['actions']); |
||
|
0 ignored issues
–
show
|
|||
| 87 | $this->profile[$name]['filters'] = array_add($this->profile[$name]['filters'], $item['filters']); |
||
|
0 ignored issues
–
show
|
|||
| 88 | $this->profile[$name]['queries'] = array_add($this->profile[$name]['queries'], $item['queries']); |
||
|
0 ignored issues
–
show
|
|||
| 89 | // $this->_query_summary($item['queries'], $this->profile[$name]['queries']); |
||
| 90 | |||
| 91 | } else { |
||
| 92 | $queries = array(); |
||
| 93 | $this->_query_summary($item['queries'], $queries); |
||
| 94 | $this->profile[$name] = array( |
||
|
0 ignored issues
–
show
|
|||
| 95 | 'time' => $time, |
||
| 96 | 'calls' => 1, |
||
| 97 | 'cache_cold_hits' => ($wp_object_cache->cold_cache_hits - $item['cache_cold_hits']), |
||
| 98 | 'cache_warm_hits' => ($wp_object_cache->warm_cache_hits - $item['cache_warm_hits']), |
||
| 99 | 'cache_misses' => ($wp_object_cache->cache_misses - $item['cache_misses']), |
||
| 100 | 'cache_dirty_objects' => $cache_dirty_delta, |
||
| 101 | 'actions' => $item['actions'], |
||
| 102 | 'filters' => $item['filters'], |
||
| 103 | // 'queries' => $item['queries'], |
||
| 104 | 'queries' => $queries, |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!$this->stack) { |
||
|
0 ignored issues
–
show
|
|||
| 109 | remove_filter('all', array($this, 'log_filter')); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | function microtime($since = 0.0) |
||
| 114 | { |
||
| 115 | list($usec, $sec) = explode(' ', microtime()); |
||
| 116 | return (float)$sec + (float)$usec - $since; |
||
|
0 ignored issues
–
show
|
|||
| 117 | } |
||
| 118 | |||
| 119 | function log_filter($tag) |
||
| 120 | { |
||
| 121 | if ($this->stack) { |
||
|
0 ignored issues
–
show
|
|||
| 122 | global $wp_actions; |
||
| 123 | if ($tag == end($wp_actions)) { |
||
|
0 ignored issues
–
show
|
|||
| 124 | @$this->stack[count($this->stack)-1]['actions'][$tag] ++; |
||
|
0 ignored issues
–
show
|
|||
| 125 | } else { |
||
| 126 | @$this->stack[count($this->stack)-1]['filters'][$tag] ++; |
||
|
0 ignored issues
–
show
|
|||
| 127 | } |
||
| 128 | } |
||
| 129 | return $arg; |
||
| 130 | } |
||
| 131 | |||
| 132 | function log_action($tag) |
||
| 133 | { |
||
| 134 | if ($this->stack) { |
||
|
0 ignored issues
–
show
|
|||
| 135 | @$this->stack[count($this->stack)-1]['actions'][$tag] ++; |
||
|
0 ignored issues
–
show
|
|||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | function _current_action() |
||
| 140 | { |
||
| 141 | global $wp_actions; |
||
| 142 | return $wp_actions[count($wp_actions)-1]; |
||
|
0 ignored issues
–
show
|
|||
| 143 | } |
||
| 144 | |||
| 145 | function results() |
||
| 146 | { |
||
| 147 | return $this->profile; |
||
| 148 | } |
||
| 149 | |||
| 150 | function _query_summary($queries, &$out) |
||
| 151 | { |
||
| 152 | foreach ($queries as $q) { |
||
|
0 ignored issues
–
show
|
|||
| 153 | $sql = $q[0]; |
||
| 154 | $sql = preg_replace('/(WHERE \w+ =) \d+/', '$1 x', $sql); |
||
| 155 | $sql = preg_replace('/(WHERE \w+ =) \'\[-\w]+\'/', '$1 \'xxx\'', $sql); |
||
| 156 | |||
| 157 | @$out[$sql] ++; |
||
|
0 ignored issues
–
show
|
|||
| 158 | } |
||
| 159 | asort($out); |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | function _query_count($queries) |
||
| 164 | { |
||
| 165 | // this requires the savequeries patch at https://core.trac.wordpress.org/ticket/5218 |
||
| 166 | $out = array(); |
||
| 167 | foreach ($queries as $q) { |
||
|
0 ignored issues
–
show
|
|||
| 168 | if (empty($q[2])) { |
||
|
0 ignored issues
–
show
|
|||
| 169 | @$out['unknown'] ++; |
||
|
0 ignored issues
–
show
|
|||
| 170 | } else { |
||
| 171 | @$out[$q[2]] ++; |
||
|
0 ignored issues
–
show
|
|||
| 172 | } |
||
| 173 | } |
||
| 174 | return $out; |
||
| 175 | } |
||
| 176 | |||
| 177 | function _dirty_objects_count($dirty_objects) |
||
| 178 | { |
||
| 179 | $out = array(); |
||
| 180 | foreach (array_keys($dirty_objects) as $group) { |
||
|
0 ignored issues
–
show
|
|||
| 181 | $out[$group] = count($dirty_objects[$group]); |
||
|
0 ignored issues
–
show
|
|||
| 182 | } |
||
| 183 | return $out; |
||
| 184 | } |
||
| 185 | |||
| 186 | function array_add($a, $b) |
||
| 187 | { |
||
| 188 | $out = $a; |
||
| 189 | foreach (array_keys($b) as $key) { |
||
|
0 ignored issues
–
show
|
|||
| 190 | if (array_key_exists($key, $out)) { |
||
|
0 ignored issues
–
show
|
|||
| 191 | $out[$key] += $b[$key]; |
||
|
0 ignored issues
–
show
|
|||
| 192 | } |
||
| 193 | } |
||
| 194 | else { |
||
|
0 ignored issues
–
show
|
|||
| 195 | $out[$key] = $b[$key]; |
||
|
0 ignored issues
–
show
|
|||
| 196 | } |
||
| 197 | return $out; |
||
| 198 | } |
||
| 199 | |||
| 200 | function array_sub($a, $b) |
||
| 201 | { |
||
| 202 | $out = $a; |
||
| 203 | foreach (array_keys($b) as $key) { |
||
|
0 ignored issues
–
show
|
|||
| 204 | if (array_key_exists($key, $b)) { |
||
|
0 ignored issues
–
show
|
|||
| 205 | $out[$key] -= $b[$key]; |
||
|
0 ignored issues
–
show
|
|||
| 206 | } |
||
| 207 | } |
||
| 208 | return $out; |
||
| 209 | } |
||
| 210 | |||
| 211 | function print_summary() |
||
| 212 | { |
||
| 213 | $results = $this->results(); |
||
| 214 | |||
| 215 | printf("\nname calls time action filter warm cold misses dirty\n"); |
||
| 216 | foreach ($results as $name=>$stats) { |
||
|
0 ignored issues
–
show
|
|||
| 217 | 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'])); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | global $wppf; |
||
| 223 | $wppf = new WPProfiler(); |
||
| 224 | |||
| 225 | function wppf_start($name) |
||
| 226 | { |
||
| 227 | $GLOBALS['wppf']->start($name); |
||
| 228 | } |
||
| 229 | |||
| 230 | function wppf_stop() |
||
| 231 | { |
||
| 232 | $GLOBALS['wppf']->stop(); |
||
| 233 | } |
||
| 234 | |||
| 235 | function wppf_results() |
||
| 236 | { |
||
| 237 | return $GLOBALS['wppf']->results(); |
||
| 238 | } |
||
| 239 | |||
| 240 | function wppf_print_summary() |
||
| 241 | { |
||
| 242 | $GLOBALS['wppf']->print_summary(); |
||
| 243 | } |
||
| 244 | |||
| 245 | ?> |
||
|
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. Loading history...
|