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 = array();
39 /** @var TransactionProfiler */
40 protected $trxProfiler;
43 * @var array Mapping of output type to class name
45 private static $outputTypes = array(
46 'db' => 'ProfilerOutputDb',
47 'text' => 'ProfilerOutputText',
48 'udp' => 'ProfilerOutputUdp',
51 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
52 /** @var Profiler Do not call this outside Profiler and ProfileSection */
53 public static $__instance = null;
54 // @codingStandardsIgnoreEnd
57 * @param array $params
59 public function __construct( array $params ) {
60 if ( isset( $params['profileID'] ) ) {
61 $this->profileID
= $params['profileID'];
63 $this->params
= $params;
64 $this->trxProfiler
= new TransactionProfiler();
71 final public static function instance() {
72 if ( self
::$__instance === null ) {
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';
80 self
::$__instance = new $class( $wgProfiler );
82 self
::$__instance = new ProfilerStub( array() );
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;
104 * Return whether this a stub profiler
108 abstract public function isStub();
113 public function setProfileID( $id ) {
114 $this->profileID
= $id;
120 public function getProfileID() {
121 if ( $this->profileID
=== false ) {
124 return $this->profileID
;
129 * Called by wfProfieIn()
131 * @param string $functionname
133 abstract public function profileIn( $functionname );
136 * Called by wfProfieOut()
138 * @param string $functionname
140 abstract public function profileOut( $functionname );
143 * Mark the start of a custom profiling frame (e.g. DB queries).
144 * The frame ends when the result of this method falls out of scope.
146 * @param string $section
147 * @return ScopedCallback|null
150 abstract public function scopedProfileIn( $section );
153 * @param ScopedCallback $section
155 public function scopedProfileOut( ScopedCallback
&$section ) {
160 * @return TransactionProfiler
163 public function getTransactionProfiler() {
164 return $this->trxProfiler
;
168 * Close opened profiling sections
170 abstract public function close();
173 * Log the data to some store or even the page output
175 * @throws MWException
178 public function logData() {
179 $output = isset( $this->params
['output'] ) ?
$this->params
['output'] : null;
181 if ( !$output ||
$this->isStub() ) {
182 // return early when no output classes defined or we're a stub
186 if ( !is_array( $output ) ) {
187 $output = array( $output );
190 foreach ( $output as $outType ) {
191 if ( !isset( self
::$outputTypes[$outType] ) ) {
192 throw new MWException( "'$outType' is an invalid output type" );
194 $class = self
::$outputTypes[$outType];
196 /** @var ProfilerOutput $profileOut */
197 $profileOut = new $class( $this, $this->params
);
198 if ( $profileOut->canUse() ) {
199 $profileOut->log( $this->getFunctionStats() );
205 * Get the content type sent out to the client.
206 * Used for profilers that output instead of store data.
210 public function getContentType() {
211 foreach ( headers_list() as $header ) {
212 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
220 * Mark this call as templated or not
224 public function setTemplated( $t ) {
225 $this->templated
= $t;
229 * Was this call as templated or not
233 public function getTemplated() {
234 return $this->templated
;
238 * Get the aggregated inclusive profiling data for each method
240 * The percent time for each time is based on the current "total" time
241 * used is based on all methods so far. This method can therefore be
242 * called several times in between several profiling calls without the
243 * delays in usage of the profiler skewing the results. A "-total" entry
244 * is always included in the results.
246 * When a call chain involves a method invoked within itself, any
247 * entries for the cyclic invocation should be be demarked with "@".
248 * This makes filtering them out easier and follows the xhprof style.
250 * @return array List of method entries arrays, each having:
251 * - name : method name
252 * - calls : the number of invoking calls
253 * - real : real time ellapsed (ms)
254 * - %real : percent real time
255 * - cpu : CPU time ellapsed (ms)
256 * - %cpu : percent CPU time
257 * - memory : memory used (bytes)
258 * - %memory : percent memory used
261 abstract public function getFunctionStats();
264 * Returns a profiling output to be stored in debug file
268 abstract public function getOutput();