Debian Codegraph @

includes/profiler/ProfilerExcimer.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * @ingroup Profiler
23 * @since 1.33
24 * @see $wgProfiler
25 */
26 class ProfilerExcimer extends Profiler {
27 /** @var ExcimerProfiler */
28 private $cpuProf;
29 /** @var ExcimerProfiler */
30 private $realProf;
31 /** @var float */
32 private $period;
33
34 /**
35 * @param array $params Associative array of parameters:
36 * - period: The sampling period
37 * - maxDepth: The maximum stack depth collected
38 * - cpuProfiler: A pre-started ExcimerProfiler instance for CPU
39 * profiling of the entire request including configuration.
40 * - realProfiler: A pre-started ExcimerProfiler instance for wall
41 * clock profiling of the entire request.
42 */
43 public function __construct( array $params = [] ) {
44 parent::__construct( $params );
45
46 $this->period = $params['period'] ?? 0.01;
47 $maxDepth = $params['maxDepth'] ?? 100;
48
49 if ( isset( $params['cpuProfiler'] ) ) {
50 $this->cpuProf = $params['cpuProfiler'];
51 } else {
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();
57 }
58
59 if ( isset( $params['realProfiler'] ) ) {
60 $this->realProf = $params['realProfiler'];
61 } else {
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();
67 }
68 }
69
70 public function scopedProfileIn( $section ): ?SectionProfileCallback {
71 return null;
72 }
73
74 public function close() {
75 $this->cpuProf->stop();
76 $this->realProf->stop();
77 }
78
79 public function getFunctionStats() {
80 $this->close();
81 $cpuStats = $this->cpuProf->getLog()->aggregateByFunction();
82 '@phan-var array $cpuStats';
83 $realStats = $this->realProf->getLog()->aggregateByFunction();
84 '@phan-var array $realStats';
85 $allNames = array_keys( $realStats + $cpuStats );
86 $cpuSamples = $this->cpuProf->getLog()->getEventCount();
87 $realSamples = $this->realProf->getLog()->getEventCount();
88
89 $resultStats = [ [
90 'name' => '-total',
91 'calls' => 1,
92 'memory' => 0,
93 '%memory' => 0,
94 'min_real' => 0,
95 'max_real' => 0,
96 'cpu' => $cpuSamples * $this->period * 1000,
97 '%cpu' => 100,
98 'real' => $realSamples * $this->period * 1000,
99 '%real' => 100,
100 ] ];
101
102 foreach ( $allNames as $funcName ) {
103 $cpuEntry = $cpuStats[$funcName] ?? false;
104 $realEntry = $realStats[$funcName] ?? false;
105 $resultEntry = [
106 'name' => $funcName,
107 'calls' => 0,
108 'memory' => 0,
109 '%memory' => 0,
110 'min_real' => 0,
111 'max_real' => 0,
112 ];
113
114 if ( $cpuEntry ) {
115 $resultEntry['cpu'] = $cpuEntry['inclusive'] * $this->period * 1000;
116 $resultEntry['%cpu'] = $cpuEntry['inclusive'] / $cpuSamples * 100;
117 } else {
118 $resultEntry['cpu'] = 0;
119 $resultEntry['%cpu'] = 0;
120 }
121 if ( $realEntry ) {
122 $resultEntry['real'] = $realEntry['inclusive'] * $this->period * 1000;
123 $resultEntry['%real'] = $realEntry['inclusive'] / $realSamples * 100;
124 } else {
125 $resultEntry['real'] = 0;
126 $resultEntry['%real'] = 0;
127 }
128
129 $resultStats[] = $resultEntry;
130 }
131 return $resultStats;
132 }
133
134 public function getOutput() {
135 $this->close();
136 $cpuLog = $this->cpuProf->getLog();
137 $realLog = $this->realProf->getLog();
138 $cpuStats = $cpuLog->aggregateByFunction();
139 '@phan-var array $cpuStats';
140 $realStats = $realLog->aggregateByFunction();
141 '@phan-var array $realStats';
142 $allNames = array_keys( $cpuStats + $realStats );
143 $cpuSamples = $cpuLog->getEventCount();
144 $realSamples = $realLog->getEventCount();
145
146 $result = '';
147
148 $titleFormat = "%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n";
149 $statsFormat = "%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n";
150 $result .= sprintf( $titleFormat,
151 'Name',
152 'CPU incl', 'CPU incl%', 'CPU self', 'CPU self%',
153 'Real incl', 'Real incl%', 'Real self', 'Real self%'
154 );
155
156 foreach ( $allNames as $funcName ) {
157 $realEntry = $realStats[$funcName] ?? false;
158 $cpuEntry = $cpuStats[$funcName] ?? false;
159 $realIncl = $realEntry ? $realEntry['inclusive'] : 0;
160 $realSelf = $realEntry ? $realEntry['self'] : 0;
161 $cpuIncl = $cpuEntry ? $cpuEntry['inclusive'] : 0;
162 $cpuSelf = $cpuEntry ? $cpuEntry['self'] : 0;
163 $result .= sprintf( $statsFormat,
164 $funcName,
165 $cpuIncl * $this->period * 1000,
166 $cpuIncl == 0 ? 0 : $cpuIncl / $cpuSamples * 100,
167 $cpuSelf * $this->period * 1000,
168 $cpuSelf == 0 ? 0 : $cpuSelf / $cpuSamples * 100,
169 $realIncl * $this->period * 1000,
170 $realIncl == 0 ? 0 : $realIncl / $realSamples * 100,
171 $realSelf * $this->period * 1000,
172 $realSelf == 0 ? 0 : $realSelf / $realSamples * 100
173 );
174 }
175
176 return $result;
177 }
178 }