45 $this->period =
$params[
'period'] ?? 0.01;
46 $maxDepth =
$params[
'maxDepth'] ?? 100;
48 if ( isset(
$params[
'cpuProfiler'] ) ) {
49 $this->cpuProf =
$params[
'cpuProfiler'];
51 $this->cpuProf =
new ExcimerProfiler;
52 $this->cpuProf->setEventType( EXCIMER_CPU );
53 $this->cpuProf->setPeriod( $this->period );
54 $this->cpuProf->setMaxDepth( $maxDepth );
55 $this->cpuProf->start();
58 if ( isset(
$params[
'realProfiler'] ) ) {
59 $this->realProf =
$params[
'realProfiler'];
61 $this->realProf =
new ExcimerProfiler;
62 $this->realProf->setEventType( EXCIMER_REAL );
63 $this->realProf->setPeriod( $this->period );
64 $this->realProf->setMaxDepth( $maxDepth );
65 $this->realProf->start();
79 $cpuStats = $this->cpuProf->getLog()->aggregateByFunction();
80 '@phan-var array $cpuStats';
81 $realStats = $this->realProf->getLog()->aggregateByFunction();
82 '@phan-var array $realStats';
83 $allNames = array_keys( $realStats + $cpuStats );
84 $cpuSamples = $this->cpuProf->getLog()->getEventCount();
85 $realSamples = $this->realProf->getLog()->getEventCount();
94 'cpu' => $cpuSamples * $this->period * 1000,
96 'real' => $realSamples * $this->period * 1000,
100 foreach ( $allNames as $funcName ) {
101 $cpuEntry = $cpuStats[$funcName] ??
false;
102 $realEntry = $realStats[$funcName] ??
false;
113 $resultEntry[
'cpu'] = $cpuEntry[
'inclusive'] * $this->period * 1000;
114 $resultEntry[
'%cpu'] = $cpuEntry[
'inclusive'] / $cpuSamples * 100;
116 $resultEntry[
'cpu'] = 0;
117 $resultEntry[
'%cpu'] = 0;
120 $resultEntry[
'real'] = $realEntry[
'inclusive'] * $this->period * 1000;
121 $resultEntry[
'%real'] = $realEntry[
'inclusive'] / $realSamples * 100;
123 $resultEntry[
'real'] = 0;
124 $resultEntry[
'%real'] = 0;
127 $resultStats[] = $resultEntry;
134 $cpuLog = $this->cpuProf->getLog();
135 $realLog = $this->realProf->getLog();
136 $cpuStats = $cpuLog->aggregateByFunction();
137 '@phan-var array $cpuStats';
138 $realStats = $realLog->aggregateByFunction();
139 '@phan-var array $realStats';
140 $allNames = array_keys( $cpuStats + $realStats );
141 $cpuSamples = $cpuLog->getEventCount();
142 $realSamples = $realLog->getEventCount();
146 $titleFormat =
"%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n";
147 $statsFormat =
"%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n";
148 $result .= sprintf( $titleFormat,
150 'CPU incl',
'CPU incl%',
'CPU self',
'CPU self%',
151 'Real incl',
'Real incl%',
'Real self',
'Real self%'
154 foreach ( $allNames as $funcName ) {
155 $realEntry = $realStats[$funcName] ??
false;
156 $cpuEntry = $cpuStats[$funcName] ??
false;
157 $realIncl = $realEntry ? $realEntry[
'inclusive'] : 0;
158 $realSelf = $realEntry ? $realEntry[
'self'] : 0;
159 $cpuIncl = $cpuEntry ? $cpuEntry[
'inclusive'] : 0;
160 $cpuSelf = $cpuEntry ? $cpuEntry[
'self'] : 0;
161 $result .= sprintf( $statsFormat,
163 $cpuIncl * $this->period * 1000,
164 $cpuIncl == 0 ? 0 : $cpuIncl / $cpuSamples * 100,
165 $cpuSelf * $this->period * 1000,
166 $cpuSelf == 0 ? 0 : $cpuSelf / $cpuSamples * 100,
167 $realIncl * $this->period * 1000,
168 $realIncl == 0 ? 0 : $realIncl / $realSamples * 100,
169 $realSelf * $this->period * 1000,
170 $realSelf == 0 ? 0 : $realSelf / $realSamples * 100