44 $this->period =
$params[
'period'] ?? 0.01;
45 $maxDepth =
$params[
'maxDepth'] ?? 100;
47 if ( isset(
$params[
'cpuProfiler'] ) ) {
48 $this->cpuProf =
$params[
'cpuProfiler'];
50 $this->cpuProf =
new ExcimerProfiler;
51 $this->cpuProf->setEventType( EXCIMER_CPU );
52 $this->cpuProf->setPeriod( $this->period );
53 $this->cpuProf->setMaxDepth( $maxDepth );
54 $this->cpuProf->start();
57 if ( isset(
$params[
'realProfiler'] ) ) {
58 $this->realProf =
$params[
'realProfiler'];
60 $this->realProf =
new ExcimerProfiler;
61 $this->realProf->setEventType( EXCIMER_REAL );
62 $this->realProf->setPeriod( $this->period );
63 $this->realProf->setMaxDepth( $maxDepth );
64 $this->realProf->start();
78 $cpuStats = $this->cpuProf->getLog()->aggregateByFunction();
79 '@phan-var array $cpuStats';
80 $realStats = $this->realProf->getLog()->aggregateByFunction();
81 '@phan-var array $realStats';
82 $allNames = array_keys( $realStats + $cpuStats );
83 $cpuSamples = $this->cpuProf->getLog()->getEventCount();
84 $realSamples = $this->realProf->getLog()->getEventCount();
93 'cpu' => $cpuSamples * $this->period * 1000,
95 'real' => $realSamples * $this->period * 1000,
99 foreach ( $allNames as $funcName ) {
100 $cpuEntry = $cpuStats[$funcName] ??
false;
101 $realEntry = $realStats[$funcName] ??
false;
112 $resultEntry[
'cpu'] = $cpuEntry[
'inclusive'] * $this->period * 1000;
113 $resultEntry[
'%cpu'] = $cpuEntry[
'inclusive'] / $cpuSamples * 100;
115 $resultEntry[
'cpu'] = 0;
116 $resultEntry[
'%cpu'] = 0;
119 $resultEntry[
'real'] = $realEntry[
'inclusive'] * $this->period * 1000;
120 $resultEntry[
'%real'] = $realEntry[
'inclusive'] / $realSamples * 100;
122 $resultEntry[
'real'] = 0;
123 $resultEntry[
'%real'] = 0;
126 $resultStats[] = $resultEntry;
133 $cpuLog = $this->cpuProf->getLog();
134 $realLog = $this->realProf->getLog();
135 $cpuStats = $cpuLog->aggregateByFunction();
136 '@phan-var array $cpuStats';
137 $realStats = $realLog->aggregateByFunction();
138 '@phan-var array $realStats';
139 $allNames = array_keys( $cpuStats + $realStats );
140 $cpuSamples = $cpuLog->getEventCount();
141 $realSamples = $realLog->getEventCount();
145 $titleFormat =
"%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n";
146 $statsFormat =
"%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n";
147 $result .= sprintf( $titleFormat,
149 'CPU incl',
'CPU incl%',
'CPU self',
'CPU self%',
150 'Real incl',
'Real incl%',
'Real self',
'Real self%'
153 foreach ( $allNames as $funcName ) {
154 $realEntry = $realStats[$funcName] ??
false;
155 $cpuEntry = $cpuStats[$funcName] ??
false;
156 $realIncl = $realEntry ? $realEntry[
'inclusive'] : 0;
157 $realSelf = $realEntry ? $realEntry[
'self'] : 0;
158 $cpuIncl = $cpuEntry ? $cpuEntry[
'inclusive'] : 0;
159 $cpuSelf = $cpuEntry ? $cpuEntry[
'self'] : 0;
160 $result .= sprintf( $statsFormat,
162 $cpuIncl * $this->period * 1000,
163 $cpuIncl == 0 ? 0 : $cpuIncl / $cpuSamples * 100,
164 $cpuSelf * $this->period * 1000,
165 $cpuSelf == 0 ? 0 : $cpuSelf / $cpuSamples * 100,
166 $realIncl * $this->period * 1000,
167 $realIncl == 0 ? 0 : $realIncl / $realSamples * 100,
168 $realSelf * $this->period * 1000,
169 $realSelf == 0 ? 0 : $realSelf / $realSamples * 100