| 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 |
class ProfilerExcimer extends Profiler { |
| 27 |
|
| 28 |
private $cpuProf; |
| 29 |
|
| 30 |
private $realProf; |
| 31 |
|
| 32 |
private $period; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct( array $params = [] ) { |
| 44 |
parent::__construct( $params ); |
| 45 |
|
| 46 |
$this->period = $params['period'] ?? 0.01; |
| 47 |
$maxDepth = $params['maxDepth'] ?? 100; |
| 48 |
|
| 49 |
if ( isset( $params['cpuProfiler'] ) ) { |
| 50 |
$this->cpuProf = $params['cpuProfiler']; |
| 51 |
} else { |
| 52 |
$this->cpuProf = new ExcimerProfiler; |
| 53 |
$this->cpuProf->setEventType( EXCIMER_CPU ); |
| 54 |
$this->cpuProf->setPeriod( $this->period ); |
| 55 |
$this->cpuProf->setMaxDepth( $maxDepth ); |
| 56 |
$this->cpuProf->start(); |
| 57 |
} |
| 58 |
|
| 59 |
if ( isset( $params['realProfiler'] ) ) { |
| 60 |
$this->realProf = $params['realProfiler']; |
| 61 |
} else { |
| 62 |
$this->realProf = new ExcimerProfiler; |
| 63 |
$this->realProf->setEventType( EXCIMER_REAL ); |
| 64 |
$this->realProf->setPeriod( $this->period ); |
| 65 |
$this->realProf->setMaxDepth( $maxDepth ); |
| 66 |
$this->realProf->start(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function scopedProfileIn( $section ): ?SectionProfileCallback { |
| 71 |
return null; |
| 72 |
} |
| 73 |
|
| 74 |
public function close() { |
| 75 |
$this->cpuProf->stop(); |
| 76 |
$this->realProf->stop(); |
| 77 |
} |
| 78 |
|
| 79 |
public function getFunctionStats() { |
| 80 |
$this->close(); |
| 81 |
$cpuStats = $this->cpuProf->getLog()->aggregateByFunction(); |
| 82 |
'@phan-var array $cpuStats'; |
| 83 |
$realStats = $this->realProf->getLog()->aggregateByFunction(); |
| 84 |
'@phan-var array $realStats'; |
| 85 |
$allNames = array_keys( $realStats + $cpuStats ); |
| 86 |
$cpuSamples = $this->cpuProf->getLog()->getEventCount(); |
| 87 |
$realSamples = $this->realProf->getLog()->getEventCount(); |
| 88 |
|
| 89 |
$resultStats = [ [ |
| 90 |
'name' => '-total', |
| 91 |
'calls' => 1, |
| 92 |
'memory' => 0, |
| 93 |
'%memory' => 0, |
| 94 |
'min_real' => 0, |
| 95 |
'max_real' => 0, |
| 96 |
'cpu' => $cpuSamples * $this->period * 1000, |
| 97 |
'%cpu' => 100, |
| 98 |
'real' => $realSamples * $this->period * 1000, |
| 99 |
'%real' => 100, |
| 100 |
] ]; |
| 101 |
|
| 102 |
foreach ( $allNames as $funcName ) { |
| 103 |
$cpuEntry = $cpuStats[$funcName] ?? false; |
| 104 |
$realEntry = $realStats[$funcName] ?? false; |
| 105 |
$resultEntry = [ |
| 106 |
'name' => $funcName, |
| 107 |
'calls' => 0, |
| 108 |
'memory' => 0, |
| 109 |
'%memory' => 0, |
| 110 |
'min_real' => 0, |
| 111 |
'max_real' => 0, |
| 112 |
]; |
| 113 |
|
| 114 |
if ( $cpuEntry ) { |
| 115 |
$resultEntry['cpu'] = $cpuEntry['inclusive'] * $this->period * 1000; |
| 116 |
$resultEntry['%cpu'] = $cpuEntry['inclusive'] / $cpuSamples * 100; |
| 117 |
} else { |
| 118 |
$resultEntry['cpu'] = 0; |
| 119 |
$resultEntry['%cpu'] = 0; |
| 120 |
} |
| 121 |
if ( $realEntry ) { |
| 122 |
$resultEntry['real'] = $realEntry['inclusive'] * $this->period * 1000; |
| 123 |
$resultEntry['%real'] = $realEntry['inclusive'] / $realSamples * 100; |
| 124 |
} else { |
| 125 |
$resultEntry['real'] = 0; |
| 126 |
$resultEntry['%real'] = 0; |
| 127 |
} |
| 128 |
|
| 129 |
$resultStats[] = $resultEntry; |
| 130 |
} |
| 131 |
return $resultStats; |
| 132 |
} |
| 133 |
|
| 134 |
public function getOutput() { |
| 135 |
$this->close(); |
| 136 |
$cpuLog = $this->cpuProf->getLog(); |
| 137 |
$realLog = $this->realProf->getLog(); |
| 138 |
$cpuStats = $cpuLog->aggregateByFunction(); |
| 139 |
'@phan-var array $cpuStats'; |
| 140 |
$realStats = $realLog->aggregateByFunction(); |
| 141 |
'@phan-var array $realStats'; |
| 142 |
$allNames = array_keys( $cpuStats + $realStats ); |
| 143 |
$cpuSamples = $cpuLog->getEventCount(); |
| 144 |
$realSamples = $realLog->getEventCount(); |
| 145 |
|
| 146 |
$result = ''; |
| 147 |
|
| 148 |
$titleFormat = "%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n"; |
| 149 |
$statsFormat = "%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n"; |
| 150 |
$result .= sprintf( $titleFormat, |
| 151 |
'Name', |
| 152 |
'CPU incl', 'CPU incl%', 'CPU self', 'CPU self%', |
| 153 |
'Real incl', 'Real incl%', 'Real self', 'Real self%' |
| 154 |
); |
| 155 |
|
| 156 |
foreach ( $allNames as $funcName ) { |
| 157 |
$realEntry = $realStats[$funcName] ?? false; |
| 158 |
$cpuEntry = $cpuStats[$funcName] ?? false; |
| 159 |
$realIncl = $realEntry ? $realEntry['inclusive'] : 0; |
| 160 |
$realSelf = $realEntry ? $realEntry['self'] : 0; |
| 161 |
$cpuIncl = $cpuEntry ? $cpuEntry['inclusive'] : 0; |
| 162 |
$cpuSelf = $cpuEntry ? $cpuEntry['self'] : 0; |
| 163 |
$result .= sprintf( $statsFormat, |
| 164 |
$funcName, |
| 165 |
$cpuIncl * $this->period * 1000, |
| 166 |
$cpuIncl == 0 ? 0 : $cpuIncl / $cpuSamples * 100, |
| 167 |
$cpuSelf * $this->period * 1000, |
| 168 |
$cpuSelf == 0 ? 0 : $cpuSelf / $cpuSamples * 100, |
| 169 |
$realIncl * $this->period * 1000, |
| 170 |
$realIncl == 0 ? 0 : $realIncl / $realSamples * 100, |
| 171 |
$realSelf * $this->period * 1000, |
| 172 |
$realSelf == 0 ? 0 : $realSelf / $realSamples * 100 |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
return $result; |
| 177 |
} |
| 178 |
} |