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 $realStats = $this->realProf->getLog()->aggregateByFunction();
81 $allNames = array_keys( $realStats + $cpuStats );
82 $cpuSamples = $this->cpuProf->getLog()->getEventCount();
83 $realSamples = $this->realProf->getLog()->getEventCount();
92 'cpu' => $cpuSamples * $this->period * 1000,
94 'real' => $realSamples * $this->period * 1000,
98 foreach ( $allNames as $funcName ) {
99 $cpuEntry = $cpuStats[$funcName] ??
false;
100 $realEntry = $realStats[$funcName] ??
false;
111 $resultEntry[
'cpu'] = $cpuEntry[
'inclusive'] * $this->period * 1000;
112 $resultEntry[
'%cpu'] = $cpuEntry[
'inclusive'] / $cpuSamples * 100;
114 $resultEntry[
'cpu'] = 0;
115 $resultEntry[
'%cpu'] = 0;
118 $resultEntry[
'real'] = $realEntry[
'inclusive'] * $this->period * 1000;
119 $resultEntry[
'%real'] = $realEntry[
'inclusive'] / $realSamples * 100;
121 $resultEntry[
'real'] = 0;
122 $resultEntry[
'%real'] = 0;
125 $resultStats[] = $resultEntry;
132 $cpuLog = $this->cpuProf->getLog();
133 $realLog = $this->realProf->getLog();
134 $cpuStats = $cpuLog->aggregateByFunction();
135 $realStats = $realLog->aggregateByFunction();
136 $allNames = array_keys( $cpuStats + $realStats );
137 $cpuSamples = $cpuLog->getEventCount();
138 $realSamples = $realLog->getEventCount();
142 $titleFormat =
"%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n";
143 $statsFormat =
"%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n";
144 $result .= sprintf( $titleFormat,
146 'CPU incl',
'CPU incl%',
'CPU self',
'CPU self%',
147 'Real incl',
'Real incl%',
'Real self',
'Real self%'
150 foreach ( $allNames as $funcName ) {
151 $realEntry = $realStats[$funcName] ??
false;
152 $cpuEntry = $cpuStats[$funcName] ??
false;
153 $realIncl = $realEntry ? $realEntry[
'inclusive'] : 0;
154 $realSelf = $realEntry ? $realEntry[
'self'] : 0;
155 $cpuIncl = $cpuEntry ? $cpuEntry[
'inclusive'] : 0;
156 $cpuSelf = $cpuEntry ? $cpuEntry[
'self'] : 0;
157 $result .= sprintf( $statsFormat,
159 $cpuIncl * $this->period * 1000,
160 $cpuIncl == 0 ? 0 : $cpuIncl / $cpuSamples * 100,
161 $cpuSelf * $this->period * 1000,
162 $cpuSelf == 0 ? 0 : $cpuSelf / $cpuSamples * 100,
163 $realIncl * $this->period * 1000,
164 $realIncl == 0 ? 0 : $realIncl / $realSamples * 100,
165 $realSelf * $this->period * 1000,
166 $realSelf == 0 ? 0 : $realSelf / $realSamples * 100