3 class ProfilerExcimer
extends Profiler
{
8 public function __construct( array $params = [] ) {
9 parent
::__construct( $params );
11 $this->period
= $params['period'] ??
0.01;
12 $maxDepth = $params['maxDepth'] ??
100;
14 $this->cpuProf
= new ExcimerProfiler
;
15 $this->cpuProf
->setEventType( EXCIMER_CPU
);
16 $this->cpuProf
->setPeriod( $this->period
);
17 $this->cpuProf
->setMaxDepth( $maxDepth );
19 $this->realProf
= new ExcimerProfiler
;
20 $this->realProf
->setEventType( EXCIMER_REAL
);
21 $this->realProf
->setPeriod( $this->period
);
22 $this->realProf
->setMaxDepth( $maxDepth );
24 $this->cpuProf
->start();
25 $this->realProf
->start();
28 public function scopedProfileIn( $section ) {
31 public function close() {
32 $this->cpuProf
->stop();
33 $this->realProf
->stop();
36 public function getFunctionStats() {
38 $cpuStats = $this->cpuProf
->getLog()->aggregateByFunction();
39 $realStats = $this->realProf
->getLog()->aggregateByFunction();
40 $allNames = array_keys( $realStats +
$cpuStats );
41 $cpuSamples = $this->cpuProf
->getLog()->getEventCount();
42 $realSamples = $this->realProf
->getLog()->getEventCount();
51 'cpu' => $cpuSamples * $this->period
* 1000,
53 'real' => $realSamples * $this->period
* 1000,
57 foreach ( $allNames as $funcName ) {
58 $cpuEntry = $cpuStats[$funcName] ??
false;
59 $realEntry = $realStats[$funcName] ??
false;
70 $resultEntry['cpu'] = $cpuEntry['inclusive'] * $this->period
* 1000;
71 $resultEntry['%cpu'] = $cpuEntry['inclusive'] / $cpuSamples * 100;
73 $resultEntry['cpu'] = 0;
74 $resultEntry['%cpu'] = 0;
77 $resultEntry['real'] = $realEntry['inclusive'] * $this->period
* 1000;
78 $resultEntry['%real'] = $realEntry['inclusive'] / $realSamples * 100;
80 $resultEntry['real'] = 0;
81 $resultEntry['%real'] = 0;
84 $resultStats[] = $resultEntry;
89 public function getOutput() {
91 $cpuLog = $this->cpuProf
->getLog();
92 $realLog = $this->realProf
->getLog();
93 $cpuStats = $cpuLog->aggregateByFunction();
94 $realStats = $realLog->aggregateByFunction();
95 $allNames = array_keys( $cpuStats +
$realStats );
96 $cpuSamples = $cpuLog->getEventCount();
97 $realSamples = $realLog->getEventCount();
101 $titleFormat = "%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n";
102 $statsFormat = "%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n";
103 $result .= sprintf( $titleFormat,
105 'CPU incl', 'CPU incl%', 'CPU self', 'CPU self%',
106 'Real incl', 'Real incl%', 'Real self', 'Real self%'
109 foreach ( $allNames as $funcName ) {
110 $realEntry = $realStats[$funcName] ??
false;
111 $cpuEntry = $cpuStats[$funcName] ??
false;
112 $realIncl = $realEntry ?
$realEntry['inclusive'] : 0;
113 $realSelf = $realEntry ?
$realEntry['self'] : 0;
114 $cpuIncl = $cpuEntry ?
$cpuEntry['inclusive'] : 0;
115 $cpuSelf = $cpuEntry ?
$cpuEntry['self'] : 0;
116 $result .= sprintf( $statsFormat,
118 $cpuIncl * $this->period
* 1000,
119 $cpuIncl == 0 ?
0 : $cpuIncl / $cpuSamples * 100,
120 $cpuSelf * $this->period
* 1000,
121 $cpuSelf == 0 ?
0 : $cpuSelf / $cpuSamples * 100,
122 $realIncl * $this->period
* 1000,
123 $realIncl == 0 ?
0 : $realIncl / $realSamples * 100,
124 $realSelf * $this->period
* 1000,
125 $realSelf == 0 ?
0 : $realSelf / $realSamples * 100