MediaWiki REL1_35
ProfilerExcimer.php
Go to the documentation of this file.
1<?php
27 private $cpuProf;
29 private $realProf;
30 private $period;
31
41 public function __construct( array $params = [] ) {
42 parent::__construct( $params );
43
44 $this->period = $params['period'] ?? 0.01;
45 $maxDepth = $params['maxDepth'] ?? 100;
46
47 if ( isset( $params['cpuProfiler'] ) ) {
48 $this->cpuProf = $params['cpuProfiler'];
49 } else {
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();
55 }
56
57 if ( isset( $params['realProfiler'] ) ) {
58 $this->realProf = $params['realProfiler'];
59 } else {
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();
65 }
66 }
67
68 public function scopedProfileIn( $section ) {
69 }
70
71 public function close() {
72 $this->cpuProf->stop();
73 $this->realProf->stop();
74 }
75
76 public function getFunctionStats() {
77 $this->close();
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();
85
86 $resultStats = [ [
87 'name' => '-total',
88 'calls' => 1,
89 'memory' => 0,
90 '%memory' => 0,
91 'min_real' => 0,
92 'max_real' => 0,
93 'cpu' => $cpuSamples * $this->period * 1000,
94 '%cpu' => 100,
95 'real' => $realSamples * $this->period * 1000,
96 '%real' => 100,
97 ] ];
98
99 foreach ( $allNames as $funcName ) {
100 $cpuEntry = $cpuStats[$funcName] ?? false;
101 $realEntry = $realStats[$funcName] ?? false;
102 $resultEntry = [
103 'name' => $funcName,
104 'calls' => 0,
105 'memory' => 0,
106 '%memory' => 0,
107 'min_real' => 0,
108 'max_real' => 0,
109 ];
110
111 if ( $cpuEntry ) {
112 $resultEntry['cpu'] = $cpuEntry['inclusive'] * $this->period * 1000;
113 $resultEntry['%cpu'] = $cpuEntry['inclusive'] / $cpuSamples * 100;
114 } else {
115 $resultEntry['cpu'] = 0;
116 $resultEntry['%cpu'] = 0;
117 }
118 if ( $realEntry ) {
119 $resultEntry['real'] = $realEntry['inclusive'] * $this->period * 1000;
120 $resultEntry['%real'] = $realEntry['inclusive'] / $realSamples * 100;
121 } else {
122 $resultEntry['real'] = 0;
123 $resultEntry['%real'] = 0;
124 }
125
126 $resultStats[] = $resultEntry;
127 }
128 return $resultStats;
129 }
130
131 public function getOutput() {
132 $this->close();
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();
142
143 $result = '';
144
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,
148 'Name',
149 'CPU incl', 'CPU incl%', 'CPU self', 'CPU self%',
150 'Real incl', 'Real incl%', 'Real self', 'Real self%'
151 );
152
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,
161 $funcName,
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
170 );
171 }
172
173 return $result;
174 }
175}
ExcimerProfiler $realProf
ExcimerProfiler $cpuProf
close()
Close opened profiling sections.
getOutput()
Returns a profiling output to be stored in debug file.
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
__construct(array $params=[])
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
Profiler base class that defines the interface and some shared functionality.
Definition Profiler.php:33
array $params
All of the params passed from $wgProfiler.
Definition Profiler.php:37