Nov. branch merge. Various features backported from stable, various bug fixes.
[lhc/web/wiklou.git] / includes / Profiling.php
index e82fd23..20e1939 100755 (executable)
@@ -1,7 +1,5 @@
 <?
 # This file is only included if profiling is enabled
-$wgDebugProfiling = true;
-
 function wfProfileIn( $functionname )
 {
        global $wgProfiler;
@@ -40,23 +38,35 @@ class Profiler
        
        function profileIn( $functionname )
        {
+               global $wgDebugFunctionEntry;
+               if ( $wgDebugFunctionEntry && function_exists( "wfDebug" ) ) {
+                       wfDebug( "Entering $functionname\n" );
+               }
                array_push( $this->mWorkStack, array($functionname, count( $this->mWorkStack ), microtime() ) );
        }
 
        function profileOut( $functionname) 
        {
-               global $wgDebugProfiling;
+               global $wgDebugProfiling, $wgDebugFunctionEntry;
+               if ( $wgDebugFunctionEntry && function_exists( "wfDebug" ) ) {
+                       wfDebug( "Exiting $functionname\n" );
+               }
+               
                $bit = array_pop( $this->mWorkStack );
                
-               if ( $wgDebugProfiling ) {
-                       if ( $functionname == "close" ) {
-                               wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
-                       } elseif ( $bit[0] != $functionname ) {
-                               wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
+               if ( !$bit ) {
+                       wfDebug( "Profiling error, !\$bit: $functionname\n" );
+               } else {
+                       if ( $wgDebugProfiling ) {
+                               if ( $functionname == "close" ) {
+                                       wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
+                               } elseif ( $bit[0] != $functionname ) {
+                                       wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
+                               }
                        }
+                       array_push( $bit, microtime() );
+                       array_push( $this->mStack, $bit );
                }
-               array_push( $bit, microtime() );
-               array_push( $this->mStack, $bit );
        }
        
        function close() 
@@ -71,21 +81,22 @@ class Profiler
                if( !count( $this->mStack ) ) {
                        return "No profiling output\n";
                }
-               
-               $format = "%-49s %6d %6.3f %6.3f %6.3f%%\n";
-               $titleFormat = "%-49s %9s %9s %9s %9s\n";
+               $width = 125;
+               $format = "%-" . ($width - 28) . "s %6d %6.3f %6.3f %6.3f%%\n";
+               $titleFormat = "%-" . ($width - 28) . "s %9s %9s %9s %9s\n";
                $prof = "\nProfiling data\n";
                $prof .= sprintf( $titleFormat, "Name", "Calls", "Total", "Each", "%" );
                $this->mCollated = array();
                $this->mCalls = array();
-               $total = 0;
                
                # Estimate profiling overhead
                $profileCount = count( $this->mStack );
+               wfProfileIn( "-overhead-total" );
                for ($i=0; $i<$profileCount ; $i++) {
-                       wfProfileIn( "--profiling overhead--" );
-                       wfProfileOut( "--profiling overhead--" );
+                       wfProfileIn( "-overhead-internal" );
+                       wfProfileOut( "-overhead-internal" );
                }
+               wfProfileOut( "-overhead-total" );
                
                # Collate
                foreach ( $this->mStack as $entry ) {
@@ -98,19 +109,17 @@ class Profiler
                        $elapsed = $end - $start;
                        $this->mCollated[$fname] += $elapsed;
                        $this->mCalls[$fname] ++;
-                       
-                       if ( $fname != "--profiling overhead--" ) {
-                               $total += $elapsed;
-                       }
                }
-               
-               $overhead = $this->mCollated["--profiling overhead--"] / $this->mCalls["--profiling overhead--"];
-               
+
+               $total = $this->mCollated["-total"];
+               $overhead = $this->mCollated["-overhead-internal"] / $profileCount;
+               $this->mCalls["-overhead-total"] = $profileCount;
+
                # Output
                foreach ( $this->mCollated as $fname => $elapsed ) {
                        $calls = $this->mCalls[$fname];
                        # Adjust for overhead
-                       if ( $fname != "--profiling overhead--" ) {
+                       if ( $fname[0] != "-" ) {
                                $elapsed -= $overhead * $calls;
                        }
                        
@@ -125,5 +134,6 @@ class Profiler
 }
 
 $wgProfiler = new Profiler();
+$wgProfiler->profileIn( "-total" );
 
 ?>