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;
52 if ( $wgDebugFunctionEntry && function_exists( "wfDebug" ) ) {
53 wfDebug( "Exiting $functionname\n" );
56 $bit = array_pop( $this->mWorkStack
);
59 wfDebug( "Profiling error, !\$bit: $functionname\n" );
61 if ( $wgDebugProfiling ) {
62 if ( $functionname == "close" ) {
63 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
64 } elseif ( $bit[0] != $functionname ) {
65 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
68 array_push( $bit, microtime() );
69 array_push( $this->mStack
, $bit );
75 while ( count( $this->mWorkStack
) ) {
76 $this->profileOut( "close" );
80 function getOutput( $scriptStart, $scriptElapsed )
82 if( !count( $this->mStack
) ) {
83 return "No profiling output\n";
86 $format = "%-" . ($width - 28) . "s %6d %6.3f %6.3f %6.3f%%\n";
87 $titleFormat = "%-" . ($width - 28) . "s %9s %9s %9s %9s\n";
88 $prof = "\nProfiling data\n";
89 $prof .= sprintf( $titleFormat, "Name", "Calls", "Total", "Each", "%" );
90 $this->mCollated
= array();
91 $this->mCalls
= array();
93 # Estimate profiling overhead
94 $profileCount = count( $this->mStack
);
95 wfProfileIn( "-overhead-total" );
96 for ($i=0; $i<$profileCount ; $i++
) {
97 wfProfileIn( "-overhead-internal" );
98 wfProfileOut( "-overhead-internal" );
100 wfProfileOut( "-overhead-total" );
103 foreach ( $this->mStack
as $entry ) {
105 $thislevel = $entry[1];
106 $start = explode( " ", $entry[2]);
107 $start = (float)$start[0] +
(float)$start[1];
108 $end = explode( " ", $entry[3]);
109 $end = (float)$end[0] +
(float)$end[1];
110 $elapsed = $end - $start;
112 if ( !array_key_exists( $fname, $this->mCollated
) ) {
113 $this->mCollated
[$fname] = 0;
114 $this->mCalls
[$fname] = 0;
117 $this->mCollated
[$fname] +
= $elapsed;
118 $this->mCalls
[$fname] ++
;
121 $total = $this->mCollated
["-total"];
122 $overhead = $this->mCollated
["-overhead-internal"] / $profileCount;
123 $this->mCalls
["-overhead-total"] = $profileCount;
126 foreach ( $this->mCollated
as $fname => $elapsed ) {
127 $calls = $this->mCalls
[$fname];
128 # Adjust for overhead
129 if ( $fname[0] != "-" ) {
130 $elapsed -= $overhead * $calls;
133 $percent = $total ?
100. * $elapsed / $total : 0;
134 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
135 (float)($elapsed * 1000) / $calls, $percent );
137 global $wgProfileToDatabase;
138 if( $wgProfileToDatabase ) {
139 Profiler
::logToDB( $fname, (float)($elapsed * 1000), $calls );
142 $prof .= "\nTotal: $total\n\n";
148 /* static */ function logToDB($name, $timeSum, $eventCount)
150 $name = wfStrencode( $name );
151 $sql = "UPDATE profiling ".
152 "SET pf_count=pf_count+{$eventCount}, ".
153 "pf_time=pf_time + {$timeSum} ".
154 "WHERE pf_name='{$name}'";
155 wfQuery($sql , DB_WRITE
);
157 $rc = wfAffectedRows();
159 $sql = "INSERT IGNORE INTO profiling (pf_name,pf_count,pf_time) ".
160 "VALUES ('{$name}', {$eventCount}, {$timeSum}) ";
161 wfQuery($sql , DB_WRITE
);
162 $rc = wfAffectedRows();
164 // When we upgrade to mysql 4.1, the insert+update
165 // can be merged into just a insert with this construct added:
166 // "ON DUPLICATE KEY UPDATE ".
167 // "pf_count=pf_count + VALUES(pf_count), ".
168 // "pf_time=pf_time + VALUES(pf_time)";
174 $wgProfiler = new Profiler();
175 $wgProfiler->profileIn( "-total" );