| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class ProfilerXhprof extends Profiler { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected $xhprofData; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
protected $sprofiler; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function __construct( array $params = [] ) { |
| 68 |
parent::__construct( $params ); |
| 69 |
|
| 70 |
|
| 71 |
if ( empty( $params['running'] ) ) { |
| 72 |
$flags = $params['flags'] ?? 0; |
| 73 |
if ( function_exists( 'xhprof_enable' ) ) { |
| 74 |
$options = isset( $params['exclude'] ) |
| 75 |
? [ 'ignored_functions' => $params['exclude'] ] |
| 76 |
: []; |
| 77 |
xhprof_enable( $flags, $options ); |
| 78 |
} elseif ( function_exists( 'tideways_xhprof_enable' ) ) { |
| 79 |
if ( isset( $params['exclude'] ) ) { |
| 80 |
throw new RuntimeException( 'The exclude option is not supported in tideways_xhprof' ); |
| 81 |
} |
| 82 |
tideways_xhprof_enable( $flags ); |
| 83 |
} else { |
| 84 |
throw new RuntimeException( 'Neither xhprof nor tideways_xhprof is installed' ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$this->sprofiler = new SectionProfiler(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function getXhprofData() { |
| 95 |
if ( !$this->xhprofData ) { |
| 96 |
if ( function_exists( 'xhprof_disable' ) ) { |
| 97 |
$data = xhprof_disable(); |
| 98 |
} elseif ( function_exists( 'tideways_xhprof_disable' ) ) { |
| 99 |
$data = tideways_xhprof_disable(); |
| 100 |
} else { |
| 101 |
throw new RuntimeException( 'Neither xhprof nor tideways_xhprof is installed' ); |
| 102 |
} |
| 103 |
$this->xhprofData = new XhprofData( $data, $this->params ); |
| 104 |
} |
| 105 |
return $this->xhprofData; |
| 106 |
} |
| 107 |
|
| 108 |
public function scopedProfileIn( $section ): ?SectionProfileCallback { |
| 109 |
$key = 'section.' . ltrim( $section, '.' ); |
| 110 |
return $this->sprofiler->scopedProfileIn( $key ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function close() { |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function shouldExclude( $name ) { |
| 126 |
if ( $name === '-total' ) { |
| 127 |
return true; |
| 128 |
} |
| 129 |
if ( !empty( $this->params['include'] ) ) { |
| 130 |
foreach ( $this->params['include'] as $pattern ) { |
| 131 |
if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
} |
| 135 |
return true; |
| 136 |
} |
| 137 |
if ( !empty( $this->params['exclude'] ) ) { |
| 138 |
foreach ( $this->params['exclude'] as $pattern ) { |
| 139 |
if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) { |
| 140 |
return true; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
public function getFunctionStats() { |
| 148 |
$metrics = $this->getXhprofData()->getCompleteMetrics(); |
| 149 |
$profile = []; |
| 150 |
|
| 151 |
$main = null; |
| 152 |
foreach ( $metrics as $fname => $stats ) { |
| 153 |
if ( $this->shouldExclude( $fname ) ) { |
| 154 |
continue; |
| 155 |
} |
| 156 |
e |
| 157 |
$entry = [ |
| 158 |
'name' => $fname, |
| 159 |
'calls' => $stats['ct'], |
| 160 |
'real' => $stats['wt']['total'] / 1000, |
| 161 |
'%real' => $stats['wt']['percent'], |
| 162 |
'cpu' => ( $stats['cpu']['total'] ?? 0 ) / 1000, |
| 163 |
'%cpu' => $stats['cpu']['percent'] ?? 0, |
| 164 |
'memory' => $stats['mu']['total'] ?? 0, |
| 165 |
'%memory' => $stats['mu']['percent'] ?? 0, |
| 166 |
'min_real' => $stats['wt']['min'] / 1000, |
| 167 |
'max_real' => $stats['wt']['max'] / 1000 |
| 168 |
]; |
| 169 |
$profile[] = $entry; |
| 170 |
if ( $fname === 'main()' ) { |
| 171 |
$main = $entry; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
foreach ( $this->sprofiler->getFunctionStats() as $stats ) { |
| 177 |
if ( $this->shouldExclude( $stats['name'] ) ) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
$stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0; |
| 183 |
$stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0; |
| 184 |
$stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0; |
| 185 |
$profile[] = $stats; |
| 186 |
} |
| 187 |
|
| 188 |
return $profile; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function getOutput() { |
| 197 |
return $this->getFunctionReport(); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
protected function getFunctionReport() { |
| 217 |
$data = $this->getFunctionStats(); |
| 218 |
usort( $data, static function ( $a, $b ) { |
| 219 |
return $b['real'] <=> $a['real']; |
| 220 |
} ); |
| 221 |
|
| 222 |
$width = 140; |
| 223 |
$nameWidth = $width - 65; |
| 224 |
$format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d"; |
| 225 |
$out = []; |
| 226 |
$out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s", |
| 227 |
'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem' |
| 228 |
); |
| 229 |
foreach ( $data as $stats ) { |
| 230 |
$out[] = sprintf( $format, |
| 231 |
$stats['name'], |
| 232 |
$stats['calls'], |
| 233 |
$stats['real'] * 1000, |
| 234 |
$stats['min_real'] * 1000, |
| 235 |
$stats['real'] / $stats['calls'] * 1000, |
| 236 |
$stats['max_real'] * 1000, |
| 237 |
$stats['%real'], |
| 238 |
$stats['memory'] |
| 239 |
); |
| 240 |
} |
| 241 |
return implode( "\n", $out ); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public function getRawData() { |
| 249 |
return $this->getXhprofData()->getRawData(); |
| 250 |
} |
| 251 |
} |