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" );
69 function getOutput( $scriptStart, $scriptElapsed )
71 if( !count( $this->mStack
) ) {
72 return "No profiling output\n";
75 $format = "%-49s %6d %6.3f %6.3f %6.3f%%\n";
76 $titleFormat = "%-49s %9s %9s %9s %9s\n";
77 $prof = "\nProfiling data\n";
78 $prof .= sprintf( $titleFormat, "Name", "Calls", "Total", "Each", "%" );
79 $this->mCollated
= array();
80 $this->mCalls
= array();
83 # Estimate profiling overhead
84 $profileCount = count( $this->mStack
);
85 for ($i=0; $i<$profileCount ; $i++
) {
86 wfProfileIn( "--profiling overhead--" );
87 wfProfileOut( "--profiling overhead--" );
91 foreach ( $this->mStack
as $entry ) {
93 $thislevel = $entry[1];
94 $start = explode( " ", $entry[2]);
95 $start = (float)$start[0] +
(float)$start[1];
96 $end = explode( " ", $entry[3]);
97 $end = (float)$end[0] +
(float)$end[1];
98 $elapsed = $end - $start;
99 $this->mCollated
[$fname] +
= $elapsed;
100 $this->mCalls
[$fname] ++
;
102 if ( $fname != "--profiling overhead--" ) {
107 $overhead = $this->mCollated
["--profiling overhead--"] / $this->mCalls
["--profiling overhead--"];
110 foreach ( $this->mCollated
as $fname => $elapsed ) {
111 $calls = $this->mCalls
[$fname];
112 # Adjust for overhead
113 if ( $fname != "--profiling overhead--" ) {
114 $elapsed -= $overhead * $calls;
117 $percent = $total ?
100. * $elapsed / $total : 0;
118 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
119 (float)($elapsed * 1000) / $calls, $percent );
121 $prof .= "\nTotal: $total\n\n";
127 $wgProfiler = new Profiler();