Translation checker added (nl: fixed); more conditional inclusion; misc. tweaks
[mediawiki.git] / includes / Profiling.php
blobee82e4f12151556d0fb5650358f60ceea6957145
1 <?
2 # This file is only included if profiling is enabled
3 $wgDebugProfiling = true;
5 function wfProfileIn( $functionname )
7 global $wgProfiler;
8 $wgProfiler->profileIn( $functionname );
11 function wfProfileOut( $functionname = "missing" )
13 global $wgProfiler;
14 $wgProfiler->profileOut( $functionname );
17 function wfGetProfilingOutput( $start, $elapsed ) {
18 global $wgProfiler;
19 return $wgProfiler->getOutput( $start, $elapsed );
22 function wfProfileClose()
24 global $wgProfiler;
25 $wgProfiler->close();
28 class Profiler
30 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
31 var $mCalls = array(), $mTotals = array();
33 function Profiler()
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 );
62 function close()
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();
82 # Estimate profiling overhead
83 $profileCount = count( $this->mStack );
84 wfProfileIn( "-overhead-total" );
85 for ($i=0; $i<$profileCount ; $i++) {
86 wfProfileIn( "-overhead-internal" );
87 wfProfileOut( "-overhead-internal" );
89 wfProfileOut( "-overhead-total" );
91 # Collate
92 foreach ( $this->mStack as $entry ) {
93 $fname = $entry[0];
94 $thislevel = $entry[1];
95 $start = explode( " ", $entry[2]);
96 $start = (float)$start[0] + (float)$start[1];
97 $end = explode( " ", $entry[3]);
98 $end = (float)$end[0] + (float)$end[1];
99 $elapsed = $end - $start;
100 $this->mCollated[$fname] += $elapsed;
101 $this->mCalls[$fname] ++;
104 $total = $this->mCollated["-total"];
105 $overhead = $this->mCollated["-overhead-internal"] / $profileCount;
106 $this->mCalls["-overhead-total"] = $profileCount;
108 # Output
109 foreach ( $this->mCollated as $fname => $elapsed ) {
110 $calls = $this->mCalls[$fname];
111 # Adjust for overhead
112 if ( $fname[0] != "-" ) {
113 $elapsed -= $overhead * $calls;
116 $percent = $total ? 100. * $elapsed / $total : 0;
117 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
118 (float)($elapsed * 1000) / $calls, $percent );
120 $prof .= "\nTotal: $total\n\n";
122 return $prof;
126 $wgProfiler = new Profiler();
127 $wgProfiler->profileIn( "-total" );