2 # This file is only included if profiling is enabled
3 $wgDebugProfiling = true;
5 function wfProfileIn( $functionname )
8 $wgProfiler->profileIn( $functionname );
11 function wfProfileOut( $functionname = "missing" )
14 $wgProfiler->profileOut( $functionname );
17 function wfGetProfilingOutput( $start, $elapsed ) {
19 return $wgProfiler->getOutput( $start, $elapsed );
22 function wfProfileClose()
30 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
31 var $mCalls = array(), $mTotals = array();
35 $this->mProfileStack = array();
36 $this->mWorkStack = array();
37 $this->mCollated = array();
41 function profileIn( $functionname )
43 array_push( $this->mWorkStack
, array($functionname, count( $this->mWorkStack
), microtime() ) );
46 function profileOut( $functionname)
48 global $wgDebugProfiling;
49 $bit = array_pop( $this->mWorkStack
);
51 if ( $wgDebugProfiling ) {
52 if ( $functionname == "close" ) {
53 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
54 } elseif ( $bit[0] != $functionname ) {
55 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
58 array_push( $bit, microtime() );
59 array_push( $this->mStack
, $bit );
64 while ( count( $this->mWorkStack
) ) {
65 $this->profileOut( "close" );
70 function getOutput( $scriptStart, $scriptElapsed )
74 $format = "%-39s %6.2f / %6.2f = %6.2f %6.2f%%\n";
76 if( !count( $this->mStack
) ) {
77 return "No profiling output\n";
79 $this->mCollated
= array();
81 $top = $this->doLevel( 0, true );
82 $this->merge( "WIKI.PHTML", $top, $scriptElapsed );
83 $this->mCollated
= array_reverse( $this->mCollated
, true );
86 foreach ( $this->mCollated as $f1 => $f1data ) {
88 foreach ( $f1data as $f2 => $t ) {
91 $this->mCollated[$f1][0] = $total;
95 foreach ( $this->mCollated
as $f1 => $f1data ) {
96 $prof .= "\n" . str_repeat( "-", $width ) . "\n";
97 $t = $this->mTotals
[$f1] * 1000;
98 $calls = $this->mCalls
[$f1];
103 $percent = $this->mTotals
[$f1] / $scriptElapsed * 100;
104 $prof .= sprintf( $format, "| $f1", $t, $calls, $each, $percent );
105 $prof .= str_repeat( "-", $width ) . "\n";
106 foreach ( $f1data as $f2 => $t ) {
107 $percent = $t / $this->mTotals
[$f1] * 100;
109 $calls = $this->mCalls
[$f1];
114 $percent = $this->mTotals
[$f1] / $scriptElapsed * 100;
115 $prof .= sprintf( $format, $f2, $t, $calls, $each, $percent );
118 $prof .= str_repeat( "-", $width ) . "\n";
123 function doLevel( $p, $fTop )
128 $tempArray = array();
130 $fname = $this->mStack
[$p][0];
131 $thislevel = $this->mStack
[$p][1];
132 $start = (float)$this->mStack
[$p][2] +
(float)$this->mStack
[$p][3];
133 $end = (float)$this->mStack
[$p][4] +
(float)$this->mStack
[$p][5];
134 $elapsed = $end - $start;
135 if ( $hotArray !== false ) {
136 # Just dropped down a level
137 # Therefore this entry is the parent of $hotArray
138 $this->merge( $fname, $hotArray, $elapsed );
142 if ( $level === false ) {
146 if ( $thislevel == $level ) {
147 $tempArray[$fname] +
= $elapsed;
148 #$this->mTotals[$fname] += elapsed;
149 $this->mCalls
[$fname] ++
;
150 } elseif ($thislevel > $level ) {
151 $hotArray = $this->doLevel( $p, false );
156 # Special case: top of hierarchy
157 # File starts with lvl 1 entry, then drops back to lvl 0
158 if ( $fTop && $getOut ) {
159 $hotArray = $tempArray;
164 } while ( !$getOut && $p < count( $this->mStack
) );
168 function merge( $f1, $a, $parentTime )
170 foreach ( $a as $f2 => $elapsed ) {
171 $this->mCollated
[$f1][$f2] +
= $elapsed;
173 $this->mTotals
[$f1] +
= $parentTime;
177 $wgProfiler = new Profiler();