3 * Base class for profiling.
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.
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.
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
22 * @defgroup Profiler Profiler
26 * Profiler base class that defines the interface and some trivial
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 = [];
38 /** @var IContextSource Current request context */
39 protected $context = null;
40 /** @var TransactionProfiler */
41 protected $trxProfiler;
43 private static $instance = null;
46 * @param array $params
48 public function __construct( array $params ) {
49 if ( isset( $params['profileID'] ) ) {
50 $this->profileID
= $params['profileID'];
52 $this->params
= $params;
53 $this->trxProfiler
= new TransactionProfiler();
60 final public static function instance() {
61 if ( self
::$instance === null ) {
62 global $wgProfiler, $wgProfileLimit;
65 'class' => 'ProfilerStub',
67 'threshold' => $wgProfileLimit,
70 if ( is_array( $wgProfiler ) ) {
71 $params = array_merge( $params, $wgProfiler );
74 $inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
75 if ( PHP_SAPI
=== 'cli' ||
!$inSample ) {
76 $params['class'] = 'ProfilerStub';
79 if ( !is_array( $params['output'] ) ) {
80 $params['output'] = [ $params['output'] ];
83 self
::$instance = new $params['class']( $params );
85 return self
::$instance;
89 * Replace the current profiler with $profiler if no non-stub profiler is set
91 * @param Profiler $profiler
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.' );
99 self
::$instance = $profiler;
106 public function setProfileID( $id ) {
107 $this->profileID
= $id;
113 public function getProfileID() {
114 if ( $this->profileID
=== false ) {
117 return $this->profileID
;
122 * Sets the context for this Profiler
124 * @param IContextSource $context
127 public function setContext( $context ) {
128 $this->context
= $context;
132 * Gets the context for this Profiler
134 * @return IContextSource
137 public function getContext() {
138 if ( $this->context
) {
139 return $this->context
;
141 wfDebug( __METHOD__
. " called and \$context is null. " .
142 "Return RequestContext::getMain(); for sanity\n" );
143 return RequestContext
::getMain();
147 // Kept BC for now, remove when possible
148 public function profileIn( $functionname ) {
151 public function profileOut( $functionname ) {
155 * Mark the start of a custom profiling frame (e.g. DB queries).
156 * The frame ends when the result of this method falls out of scope.
158 * @param string $section
159 * @return ScopedCallback|null
162 abstract public function scopedProfileIn( $section );
165 * @param ScopedCallback $section
167 public function scopedProfileOut( ScopedCallback
&$section = null ) {
172 * @return TransactionProfiler
175 public function getTransactionProfiler() {
176 return $this->trxProfiler
;
180 * Close opened profiling sections
182 abstract public function close();
185 * Get all usable outputs.
187 * @throws MWException
188 * @return array Array of ProfilerOutput instances.
191 private function getOutputs() {
193 foreach ( $this->params
['output'] as $outputType ) {
194 // The class may be specified as either the full class name (for
195 // example, 'ProfilerOutputStats') or (for backward compatibility)
196 // the trailing portion of the class name (for example, 'stats').
197 $outputClass = strpos( $outputType, 'ProfilerOutput' ) === false
198 ?
'ProfilerOutput' . ucfirst( $outputType )
200 if ( !class_exists( $outputClass ) ) {
201 throw new MWException( "'$outputType' is an invalid output type" );
203 $outputInstance = new $outputClass( $this, $this->params
);
204 if ( $outputInstance->canUse() ) {
205 $outputs[] = $outputInstance;
212 * Log the data to some store or even the page output
216 public function logData() {
217 $request = $this->getContext()->getRequest();
219 $timeElapsed = $request->getElapsedTime();
220 $timeElapsedThreshold = $this->params
['threshold'];
221 if ( $timeElapsed <= $timeElapsedThreshold ) {
225 $outputs = $this->getOutputs();
230 $stats = $this->getFunctionStats();
231 foreach ( $outputs as $output ) {
232 $output->log( $stats );
237 * Output current data to the page output if configured to do so
239 * @throws MWException
242 public function logDataPageOutputOnly() {
243 foreach ( $this->getOutputs() as $output ) {
244 if ( $output instanceof ProfilerOutputText
) {
245 $stats = $this->getFunctionStats();
246 $output->log( $stats );
252 * Get the content type sent out to the client.
253 * Used for profilers that output instead of store data.
257 public function getContentType() {
258 foreach ( headers_list() as $header ) {
259 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
267 * Mark this call as templated or not
271 public function setTemplated( $t ) {
272 $this->templated
= $t;
276 * Was this call as templated or not
280 public function getTemplated() {
281 return $this->templated
;
285 * Get the aggregated inclusive profiling data for each method
287 * The percent time for each time is based on the current "total" time
288 * used is based on all methods so far. This method can therefore be
289 * called several times in between several profiling calls without the
290 * delays in usage of the profiler skewing the results. A "-total" entry
291 * is always included in the results.
293 * When a call chain involves a method invoked within itself, any
294 * entries for the cyclic invocation should be be demarked with "@".
295 * This makes filtering them out easier and follows the xhprof style.
297 * @return array List of method entries arrays, each having:
298 * - name : method name
299 * - calls : the number of invoking calls
300 * - real : real time elapsed (ms)
301 * - %real : percent real time
302 * - cpu : CPU time elapsed (ms)
303 * - %cpu : percent CPU time
304 * - memory : memory used (bytes)
305 * - %memory : percent memory used
306 * - min_real : min real time in a call (ms)
307 * - max_real : max real time in a call (ms)
310 abstract public function getFunctionStats();
313 * Returns a profiling output to be stored in debug file
317 abstract public function getOutput();