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
);
52 wfDebug( "Profiling error, !\$bit: $functionname\n" );
54 if ( $wgDebugProfiling ) {
55 if ( $functionname == "close" ) {
56 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
57 } elseif ( $bit[0] != $functionname ) {
58 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
61 array_push( $bit, microtime() );
62 array_push( $this->mStack
, $bit );
68 while ( count( $this->mWorkStack
) ) {
69 $this->profileOut( "close" );
73 function getOutput( $scriptStart, $scriptElapsed )
75 if( !count( $this->mStack
) ) {
76 return "No profiling output\n";
79 $format = "%-49s %6d %6.3f %6.3f %6.3f%%\n";
80 $titleFormat = "%-49s %9s %9s %9s %9s\n";
81 $prof = "\nProfiling data\n";
82 $prof .= sprintf( $titleFormat, "Name", "Calls", "Total", "Each", "%" );
83 $this->mCollated
= array();
84 $this->mCalls
= array();
86 # Estimate profiling overhead
87 $profileCount = count( $this->mStack
);
88 wfProfileIn( "-overhead-total" );
89 for ($i=0; $i<$profileCount ; $i++
) {
90 wfProfileIn( "-overhead-internal" );
91 wfProfileOut( "-overhead-internal" );
93 wfProfileOut( "-overhead-total" );
96 foreach ( $this->mStack
as $entry ) {
98 $thislevel = $entry[1];
99 $start = explode( " ", $entry[2]);
100 $start = (float)$start[0] +
(float)$start[1];
101 $end = explode( " ", $entry[3]);
102 $end = (float)$end[0] +
(float)$end[1];
103 $elapsed = $end - $start;
104 $this->mCollated
[$fname] +
= $elapsed;
105 $this->mCalls
[$fname] ++
;
108 $total = $this->mCollated
["-total"];
109 $overhead = $this->mCollated
["-overhead-internal"] / $profileCount;
110 $this->mCalls
["-overhead-total"] = $profileCount;
113 foreach ( $this->mCollated
as $fname => $elapsed ) {
114 $calls = $this->mCalls
[$fname];
115 # Adjust for overhead
116 if ( $fname[0] != "-" ) {
117 $elapsed -= $overhead * $calls;
120 $percent = $total ?
100. * $elapsed / $total : 0;
121 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
122 (float)($elapsed * 1000) / $calls, $percent );
124 $prof .= "\nTotal: $total\n\n";
130 $wgProfiler = new Profiler();
131 $wgProfiler->profileIn( "-total" );