12e999b8da73f96f49cb4265964ff9bdea27d30d
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;
42 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
43 /** @var Profiler Do not call this outside Profiler and ProfileSection */
44 public static $__instance = null;
45 // @codingStandardsIgnoreEnd
48 * @param array $params
50 public function __construct( array $params ) {
51 if ( isset( $params['profileID'] ) ) {
52 $this->profileID
= $params['profileID'];
54 $this->params
= $params;
55 $this->trxProfiler
= new TransactionProfiler();
62 final public static function instance() {
63 if ( self
::$__instance === null ) {
65 if ( is_array( $wgProfiler ) ) {
66 if ( !isset( $wgProfiler['class'] ) ) {
67 $class = 'ProfilerStub';
69 $class = $wgProfiler['class'];
71 self
::$__instance = new $class( $wgProfiler );
73 self
::$__instance = new ProfilerStub( array() );
76 return self
::$__instance;
80 * Return whether this a stub profiler
84 abstract public function isStub();
89 public function setProfileID( $id ) {
90 $this->profileID
= $id;
96 public function getProfileID() {
97 if ( $this->profileID
=== false ) {
100 return $this->profileID
;
105 * Called by wfProfieIn()
107 * @param string $functionname
109 abstract public function profileIn( $functionname );
112 * Called by wfProfieOut()
114 * @param string $functionname
116 abstract public function profileOut( $functionname );
119 * @return TransactionProfiler
122 public function getTransactionProfiler() {
123 return $this->trxProfiler
;
127 * Close opened profiling sections
129 abstract public function close();
132 * Log the data to some store or even the page output
136 public function logData() {
137 $output = isset( $this->params
['output'] ) ?
138 $this->params
['output'] : null;
140 if ( !$output ||
$this->isStub() ) {
141 // return early when no output classes defined or we're a stub
145 if ( !is_array( $output ) ) {
146 $output = array( $output );
149 foreach ( $output as $outType ) {
150 $class = 'ProfilerOutput' . ucfirst( strtolower( $outType ) );
151 $profileOut = new $class( $this, $this->params
);
152 if ( $profileOut->canUse() ) {
159 * Get the content type sent out to the client.
160 * Used for profilers that output instead of store data.
164 public function getContentType() {
165 foreach ( headers_list() as $header ) {
166 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
174 * Mark this call as templated or not
178 public function setTemplated( $t ) {
179 $this->templated
= $t;
183 * Was this call as templated or not
187 public function getTemplated() {
188 return $this->templated
;
192 * Get the aggregated inclusive profiling data for each method
194 * The percent time for each time is based on the current "total" time
195 * used is based on all methods so far. This method can therefore be
196 * called several times in between several profiling calls without the
197 * delays in usage of the profiler skewing the results. A "-total" entry
198 * is always included in the results.
200 * When a call chain involves a method invoked within itself, any
201 * entries for the cyclic invocation should be be demarked with "@".
202 * This makes filtering them out easier and follows the xhprof style.
204 * @return array List of method entries arrays, each having:
205 * - name : method name
206 * - calls : the number of invoking calls
207 * - real : real time ellapsed (ms)
208 * - %real : percent real time
209 * - cpu : CPU time ellapsed (ms)
210 * - %cpu : percent CPU time
211 * - memory : memory used (bytes)
212 * - %memory : percent memory used
215 abstract public function getFunctionStats();
218 * Returns a profiling output to be stored in debug file
222 abstract public function getOutput();