2 # This file is only included if profiling is enabled
3 function wfProfileIn( $functionname )
6 $wgProfiler->profileIn( $functionname );
9 function wfProfileOut( $functionname = "missing" )
12 $wgProfiler->profileOut( $functionname );
15 function wfGetProfilingOutput( $start, $elapsed ) {
17 return $wgProfiler->getOutput( $start, $elapsed );
20 function wfProfileClose()
28 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
29 var $mCalls = array(), $mTotals = array();
33 $this->mProfileStack = array();
34 $this->mWorkStack = array();
35 $this->mCollated = array();
39 function profileIn( $functionname )
41 global $wgDebugFunctionEntry;
42 if ( $wgDebugFunctionEntry && function_exists( "wfDebug" ) ) {
43 wfDebug( "Entering $functionname\n" );
45 array_push( $this->mWorkStack
, array($functionname, count( $this->mWorkStack
), microtime() ) );
48 function profileOut( $functionname)
50 global $wgDebugProfiling, $wgDebugFunctionEntry;
51 if ( $wgDebugFunctionEntry && function_exists( "wfDebug" ) ) {
52 wfDebug( "Exiting $functionname\n" );
55 $bit = array_pop( $this->mWorkStack
);
58 wfDebug( "Profiling error, !\$bit: $functionname\n" );
60 if ( $wgDebugProfiling ) {
61 if ( $functionname == "close" ) {
62 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
63 } elseif ( $bit[0] != $functionname ) {
64 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
67 array_push( $bit, microtime() );
68 array_push( $this->mStack
, $bit );
74 while ( count( $this->mWorkStack
) ) {
75 $this->profileOut( "close" );
79 function getOutput( $scriptStart, $scriptElapsed )
81 if( !count( $this->mStack
) ) {
82 return "No profiling output\n";
85 $format = "%-" . ($width - 28) . "s %6d %6.3f %6.3f %6.3f%%\n";
86 $titleFormat = "%-" . ($width - 28) . "s %9s %9s %9s %9s\n";
87 $prof = "\nProfiling data\n";
88 $prof .= sprintf( $titleFormat, "Name", "Calls", "Total", "Each", "%" );
89 $this->mCollated
= array();
90 $this->mCalls
= array();
92 # Estimate profiling overhead
93 $profileCount = count( $this->mStack
);
94 wfProfileIn( "-overhead-total" );
95 for ($i=0; $i<$profileCount ; $i++
) {
96 wfProfileIn( "-overhead-internal" );
97 wfProfileOut( "-overhead-internal" );
99 wfProfileOut( "-overhead-total" );
102 foreach ( $this->mStack
as $entry ) {
104 $thislevel = $entry[1];
105 $start = explode( " ", $entry[2]);
106 $start = (float)$start[0] +
(float)$start[1];
107 $end = explode( " ", $entry[3]);
108 $end = (float)$end[0] +
(float)$end[1];
109 $elapsed = $end - $start;
110 $this->mCollated
[$fname] +
= $elapsed;
111 $this->mCalls
[$fname] ++
;
114 $total = $this->mCollated
["-total"];
115 $overhead = $this->mCollated
["-overhead-internal"] / $profileCount;
116 $this->mCalls
["-overhead-total"] = $profileCount;
119 foreach ( $this->mCollated
as $fname => $elapsed ) {
120 $calls = $this->mCalls
[$fname];
121 # Adjust for overhead
122 if ( $fname[0] != "-" ) {
123 $elapsed -= $overhead * $calls;
126 $percent = $total ?
100. * $elapsed / $total : 0;
127 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
128 (float)($elapsed * 1000) / $calls, $percent );
130 $prof .= "\nTotal: $total\n\n";
136 $wgProfiler = new Profiler();
137 $wgProfiler->profileIn( "-total" );