46 $this->period =
$params[
'period'] ?? 0.01;
47 $maxDepth =
$params[
'maxDepth'] ?? 100;
49 if ( isset(
$params[
'cpuProfiler'] ) ) {
50 $this->cpuProf =
$params[
'cpuProfiler'];
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();
59 if ( isset(
$params[
'realProfiler'] ) ) {
60 $this->realProf =
$params[
'realProfiler'];
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();
80 $cpuStats = $this->cpuProf->getLog()->aggregateByFunction();
81 '@phan-var array $cpuStats';
82 $realStats = $this->realProf->getLog()->aggregateByFunction();
83 '@phan-var array $realStats';
84 $allNames = array_keys( $realStats + $cpuStats );
85 $cpuSamples = $this->cpuProf->getLog()->getEventCount();
86 $realSamples = $this->realProf->getLog()->getEventCount();
95 'cpu' => $cpuSamples * $this->period * 1000,
97 'real' => $realSamples * $this->period * 1000,
101 foreach ( $allNames as $funcName ) {
102 $cpuEntry = $cpuStats[$funcName] ??
false;
103 $realEntry = $realStats[$funcName] ??
false;
114 $resultEntry[
'cpu'] = $cpuEntry[
'inclusive'] * $this->period * 1000;
115 $resultEntry[
'%cpu'] = $cpuEntry[
'inclusive'] / $cpuSamples * 100;
117 $resultEntry[
'cpu'] = 0;
118 $resultEntry[
'%cpu'] = 0;
121 $resultEntry[
'real'] = $realEntry[
'inclusive'] * $this->period * 1000;
122 $resultEntry[
'%real'] = $realEntry[
'inclusive'] / $realSamples * 100;
124 $resultEntry[
'real'] = 0;
125 $resultEntry[
'%real'] = 0;
128 $resultStats[] = $resultEntry;
135 $cpuLog = $this->cpuProf->getLog();
136 $realLog = $this->realProf->getLog();
137 $cpuStats = $cpuLog->aggregateByFunction();
138 '@phan-var array $cpuStats';
139 $realStats = $realLog->aggregateByFunction();
140 '@phan-var array $realStats';
141 $allNames = array_keys( $cpuStats + $realStats );
142 $cpuSamples = $cpuLog->getEventCount();
143 $realSamples = $realLog->getEventCount();
147 $titleFormat =
"%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n";
148 $statsFormat =
"%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n";
149 $result .= sprintf( $titleFormat,
151 'CPU incl',
'CPU incl%',
'CPU self',
'CPU self%',
152 'Real incl',
'Real incl%',
'Real self',
'Real self%'
155 foreach ( $allNames as $funcName ) {
156 $realEntry = $realStats[$funcName] ??
false;
157 $cpuEntry = $cpuStats[$funcName] ??
false;
158 $realIncl = $realEntry ? $realEntry[
'inclusive'] : 0;
159 $realSelf = $realEntry ? $realEntry[
'self'] : 0;
160 $cpuIncl = $cpuEntry ? $cpuEntry[
'inclusive'] : 0;
161 $cpuSelf = $cpuEntry ? $cpuEntry[
'self'] : 0;
162 $result .= sprintf( $statsFormat,
164 $cpuIncl * $this->period * 1000,
165 $cpuIncl == 0 ? 0 : $cpuIncl / $cpuSamples * 100,
166 $cpuSelf * $this->period * 1000,
167 $cpuSelf == 0 ? 0 : $cpuSelf / $cpuSamples * 100,
168 $realIncl * $this->period * 1000,
169 $realIncl == 0 ? 0 : $realIncl / $realSamples * 100,
170 $realSelf * $this->period * 1000,
171 $realSelf == 0 ? 0 : $realSelf / $realSamples * 100