Merge "mediawiki.inspect: Guard against Object.prototype keys as module names"
[lhc/web/wiklou.git] / includes / profiler / Profiler.php
1 <?php
2 /**
3 * Base class for profiling.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 * @defgroup Profiler Profiler
23 */
24
25 /**
26 * Profiler base class that defines the interface and some trivial
27 * functionality
28 *
29 * @ingroup Profiler
30 */
31 abstract class Profiler {
32 /** @var string|bool Profiler ID for bucketing data */
33 protected $profileID = false;
34 /** @var bool Whether MediaWiki is in a SkinTemplate output context */
35 protected $templated = false;
36 /** @var array All of the params passed from $wgProfiler */
37 protected $params = array();
38
39 /** @var TransactionProfiler */
40 protected $trxProfiler;
41
42 /**
43 * @var array Mapping of output type to class name
44 */
45 private static $outputTypes = array(
46 'db' => 'ProfilerOutputDb',
47 'text' => 'ProfilerOutputText',
48 'udp' => 'ProfilerOutputUdp',
49 );
50
51 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
52 /** @var Profiler Do not call this outside Profiler and ProfileSection */
53 public static $__instance = null;
54 // @codingStandardsIgnoreEnd
55
56 /**
57 * @param array $params
58 */
59 public function __construct( array $params ) {
60 if ( isset( $params['profileID'] ) ) {
61 $this->profileID = $params['profileID'];
62 }
63 $this->params = $params;
64 $this->trxProfiler = new TransactionProfiler();
65 }
66
67 /**
68 * Singleton
69 * @return Profiler
70 */
71 final public static function instance() {
72 if ( self::$__instance === null ) {
73 global $wgProfiler;
74 if ( is_array( $wgProfiler ) ) {
75 $class = isset( $wgProfiler['class'] ) ? $wgProfiler['class'] : 'ProfilerStub';
76 $factor = isset( $wgProfiler['sampling'] ) ? $wgProfiler['sampling'] : 1;
77 if ( PHP_SAPI === 'cli' || mt_rand( 0, $factor - 1 ) != 0 ) {
78 $class = 'ProfilerStub';
79 }
80 self::$__instance = new $class( $wgProfiler );
81 } else {
82 self::$__instance = new ProfilerStub( array() );
83 }
84 }
85 return self::$__instance;
86 }
87
88 /**
89 * Replace the current profiler with $profiler if no non-stub profiler is set
90 *
91 * @param Profiler $profiler
92 * @throws MWException
93 * @since 1.25
94 */
95 final public static function replaceStubInstance( Profiler $profiler ) {
96 if ( self::$__instance && !( self::$__instance instanceof ProfilerStub ) ) {
97 throw new MWException( 'Could not replace non-stub profiler instance.' );
98 } else {
99 self::$__instance = $profiler;
100 }
101 }
102
103 /**
104 * @param string $id
105 */
106 public function setProfileID( $id ) {
107 $this->profileID = $id;
108 }
109
110 /**
111 * @return string
112 */
113 public function getProfileID() {
114 if ( $this->profileID === false ) {
115 return wfWikiID();
116 } else {
117 return $this->profileID;
118 }
119 }
120
121 // Kept BC for now, remove when possible
122 public function profileIn( $functionname ) {}
123 public function profileOut( $functionname ) {}
124
125 /**
126 * Mark the start of a custom profiling frame (e.g. DB queries).
127 * The frame ends when the result of this method falls out of scope.
128 *
129 * @param string $section
130 * @return ScopedCallback|null
131 * @since 1.25
132 */
133 abstract public function scopedProfileIn( $section );
134
135 /**
136 * @param ScopedCallback $section
137 */
138 public function scopedProfileOut( ScopedCallback &$section ) {
139 $section = null;
140 }
141
142 /**
143 * @return TransactionProfiler
144 * @since 1.25
145 */
146 public function getTransactionProfiler() {
147 return $this->trxProfiler;
148 }
149
150 /**
151 * Close opened profiling sections
152 */
153 abstract public function close();
154
155 /**
156 * Log the data to some store or even the page output
157 *
158 * @throws MWException
159 * @since 1.25
160 */
161 public function logData() {
162 $output = isset( $this->params['output'] ) ? $this->params['output'] : null;
163
164 if ( !$output || $this instanceof ProfilerStub ) {
165 // return early when no output classes defined or we're a stub
166 return;
167 }
168
169 if ( !is_array( $output ) ) {
170 $output = array( $output );
171 }
172
173 foreach ( $output as $outType ) {
174 if ( !isset( self::$outputTypes[$outType] ) ) {
175 throw new MWException( "'$outType' is an invalid output type" );
176 }
177 $class = self::$outputTypes[$outType];
178
179 /** @var ProfilerOutput $profileOut */
180 $profileOut = new $class( $this, $this->params );
181 if ( $profileOut->canUse() ) {
182 $profileOut->log( $this->getFunctionStats() );
183 }
184 }
185 }
186
187 /**
188 * Get the content type sent out to the client.
189 * Used for profilers that output instead of store data.
190 * @return string
191 * @since 1.25
192 */
193 public function getContentType() {
194 foreach ( headers_list() as $header ) {
195 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
196 return $m[1];
197 }
198 }
199 return null;
200 }
201
202 /**
203 * Mark this call as templated or not
204 *
205 * @param bool $t
206 */
207 public function setTemplated( $t ) {
208 $this->templated = $t;
209 }
210
211 /**
212 * Was this call as templated or not
213 *
214 * @return bool
215 */
216 public function getTemplated() {
217 return $this->templated;
218 }
219
220 /**
221 * Get the aggregated inclusive profiling data for each method
222 *
223 * The percent time for each time is based on the current "total" time
224 * used is based on all methods so far. This method can therefore be
225 * called several times in between several profiling calls without the
226 * delays in usage of the profiler skewing the results. A "-total" entry
227 * is always included in the results.
228 *
229 * When a call chain involves a method invoked within itself, any
230 * entries for the cyclic invocation should be be demarked with "@".
231 * This makes filtering them out easier and follows the xhprof style.
232 *
233 * @return array List of method entries arrays, each having:
234 * - name : method name
235 * - calls : the number of invoking calls
236 * - real : real time ellapsed (ms)
237 * - %real : percent real time
238 * - cpu : CPU time ellapsed (ms)
239 * - %cpu : percent CPU time
240 * - memory : memory used (bytes)
241 * - %memory : percent memory used
242 * - min_real : min real time in a call (ms)
243 * - max_real : max real time in a call (ms)
244 * @since 1.25
245 */
246 abstract public function getFunctionStats();
247
248 /**
249 * Returns a profiling output to be stored in debug file
250 *
251 * @return string
252 */
253 abstract public function getOutput();
254 }