Merge "Don't discourage usage of Html::element()"
[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 /**
122 * Mark the start of a custom profiling frame (e.g. DB queries).
123 * The frame ends when the result of this method falls out of scope.
124 *
125 * @param string $section
126 * @return ScopedCallback|null
127 * @since 1.25
128 */
129 abstract public function scopedProfileIn( $section );
130
131 /**
132 * @param ScopedCallback $section
133 */
134 public function scopedProfileOut( ScopedCallback &$section ) {
135 $section = null;
136 }
137
138 /**
139 * @return TransactionProfiler
140 * @since 1.25
141 */
142 public function getTransactionProfiler() {
143 return $this->trxProfiler;
144 }
145
146 /**
147 * Close opened profiling sections
148 */
149 abstract public function close();
150
151 /**
152 * Log the data to some store or even the page output
153 *
154 * @throws MWException
155 * @since 1.25
156 */
157 public function logData() {
158 $output = isset( $this->params['output'] ) ? $this->params['output'] : null;
159
160 if ( !$output || $this instanceof ProfilerStub ) {
161 // return early when no output classes defined or we're a stub
162 return;
163 }
164
165 if ( !is_array( $output ) ) {
166 $output = array( $output );
167 }
168
169 foreach ( $output as $outType ) {
170 if ( !isset( self::$outputTypes[$outType] ) ) {
171 throw new MWException( "'$outType' is an invalid output type" );
172 }
173 $class = self::$outputTypes[$outType];
174
175 /** @var ProfilerOutput $profileOut */
176 $profileOut = new $class( $this, $this->params );
177 if ( $profileOut->canUse() ) {
178 $profileOut->log( $this->getFunctionStats() );
179 }
180 }
181 }
182
183 /**
184 * Get the content type sent out to the client.
185 * Used for profilers that output instead of store data.
186 * @return string
187 * @since 1.25
188 */
189 public function getContentType() {
190 foreach ( headers_list() as $header ) {
191 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
192 return $m[1];
193 }
194 }
195 return null;
196 }
197
198 /**
199 * Mark this call as templated or not
200 *
201 * @param bool $t
202 */
203 public function setTemplated( $t ) {
204 $this->templated = $t;
205 }
206
207 /**
208 * Was this call as templated or not
209 *
210 * @return bool
211 */
212 public function getTemplated() {
213 return $this->templated;
214 }
215
216 /**
217 * Get the aggregated inclusive profiling data for each method
218 *
219 * The percent time for each time is based on the current "total" time
220 * used is based on all methods so far. This method can therefore be
221 * called several times in between several profiling calls without the
222 * delays in usage of the profiler skewing the results. A "-total" entry
223 * is always included in the results.
224 *
225 * When a call chain involves a method invoked within itself, any
226 * entries for the cyclic invocation should be be demarked with "@".
227 * This makes filtering them out easier and follows the xhprof style.
228 *
229 * @return array List of method entries arrays, each having:
230 * - name : method name
231 * - calls : the number of invoking calls
232 * - real : real time ellapsed (ms)
233 * - %real : percent real time
234 * - cpu : CPU time ellapsed (ms)
235 * - %cpu : percent CPU time
236 * - memory : memory used (bytes)
237 * - %memory : percent memory used
238 * - min_real : min real time in a call (ms)
239 * - max_real : max real time in a call (ms)
240 * @since 1.25
241 */
242 abstract public function getFunctionStats();
243
244 /**
245 * Returns a profiling output to be stored in debug file
246 *
247 * @return string
248 */
249 abstract public function getOutput();
250 }