3 * This file is only included if profiling is enabled
8 * @param $functioname name of the function we will profile
10 function wfProfileIn( $functionname ) {
12 $wgProfiler->profileIn( $functionname );
16 * @param $functioname name of the function we have profiled
18 function wfProfileOut( $functionname = 'missing' ) {
20 $wgProfiler->profileOut( $functionname );
23 function wfGetProfilingOutput( $start, $elapsed ) {
25 return $wgProfiler->getOutput( $start, $elapsed );
28 function wfProfileClose() {
33 if( !function_exists( 'memory_get_usage' ) ) {
34 # Old PHP or --enable-memory-limit not compiled in
35 function memory_get_usage() {
46 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
47 var $mCalls = array(), $mTotals = array();
51 $this->mProfileStack = array();
52 $this->mWorkStack = array();
53 $this->mCollated = array();
57 function profileIn( $functionname ) {
58 global $wgDebugFunctionEntry;
59 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
60 wfDebug( str_repeat( ' ', count( $this->mWorkStack
) ) . 'Entering '.$functionname."\n" );
62 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(), memory_get_usage() );
65 function profileOut( $functionname ) {
67 $memory = memory_get_usage();
68 global $wgDebugProfiling, $wgDebugFunctionEntry;
70 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
71 wfDebug( str_repeat( ' ', count( $this->mWorkStack
) ) . 'Exiting '.$functionname."\n" );
74 $bit = array_pop( $this->mWorkStack
);
77 wfDebug( "Profiling error, !\$bit: $functionname\n" );
79 if ( $wgDebugProfiling ) {
80 if ( $functionname == 'close' ) {
81 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
82 } elseif ( $bit[0] != $functionname ) {
83 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
88 $this->mStack
[] = $bit;
93 while ( count( $this->mWorkStack
) ) {
94 $this->profileOut( 'close' );
98 function getOutput() {
99 global $wgDebugFunctionEntry;
100 $wgDebugFunctionEntry = false;
102 if( !count( $this->mStack
) ) {
103 return "No profiling output\n";
107 $format = "%-" . ($width - 34) . "s %6d %6.3f %6.3f %7.3f%% %6d (%6.3f-%6.3f)\n";
108 $titleFormat = "%-" . ($width - 34) . "s %9s %9s %9s %9s %6s\n";
109 $prof = "\nProfiling data\n";
110 $prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
111 $this->mCollated
= array();
112 $this->mCalls
= array();
113 $this->mMemory
= array();
115 # Estimate profiling overhead
116 $profileCount = count( $this->mStack
);
117 wfProfileIn( '-overhead-total' );
118 for ($i=0; $i<$profileCount ; $i++
) {
119 wfProfileIn( '-overhead-internal' );
120 wfProfileOut( '-overhead-internal' );
122 wfProfileOut( '-overhead-total' );
125 foreach ( $this->mStack
as $entry ) {
127 $thislevel = $entry[1];
128 $start = explode( ' ', $entry[2]);
129 $start = (float)$start[0] +
(float)$start[1];
130 $end = explode( ' ', $entry[4]);
131 $end = (float)$end[0] +
(float)$end[1];
132 $elapsed = $end - $start;
134 $memory = $entry[5] - $entry[3];
136 if ( !array_key_exists( $fname, $this->mCollated
) ) {
137 $this->mCollated
[$fname] = 0;
138 $this->mCalls
[$fname] = 0;
139 $this->mMemory
[$fname] = 0;
140 $this->mMin
[$fname] = 1 << 24;
141 $this->mMax
[$fname] = 0;
144 $this->mCollated
[$fname] +
= $elapsed;
145 $this->mCalls
[$fname] ++
;
146 $this->mMemory
[$fname] +
= $memory;
147 $this->mMin
[$fname] = min( $this->mMin
[$fname], $elapsed );
148 $this->mMax
[$fname] = max( $this->mMax
[$fname], $elapsed );
151 $total = @$this->mCollated
['-total'];
152 $overhead = $this->mCollated
['-overhead-internal'] / $profileCount;
153 $this->mCalls
['-overhead-total'] = $profileCount;
156 asort( $this->mCollated
, SORT_NUMERIC
);
157 foreach ( $this->mCollated
as $fname => $elapsed ) {
158 $calls = $this->mCalls
[$fname];
159 # Adjust for overhead
160 if ( $fname[0] != '-' ) {
161 $elapsed -= $overhead * $calls;
164 $percent = $total ?
100. * $elapsed / $total : 0;
165 $memory = $this->mMemory
[$fname];
166 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
167 (float)($elapsed * 1000) / $calls, $percent, $memory,
168 ($this->mMin
[$fname] * 1000.0),
169 ($this->mMax
[$fname] * 1000.0));
171 global $wgProfileToDatabase;
172 if( $wgProfileToDatabase ) {
173 Profiler
::logToDB( $fname, (float)($elapsed * 1000), $calls );
176 $prof .= "\nTotal: $total\n\n";
185 function logToDB($name, $timeSum, $eventCount) {
186 $dbw =& wfGetDB( DB_MASTER
);
187 $profiling = $dbw->tableName( 'profiling' );
189 $name = substr($name,0,255);
190 $encname = $dbw->strencode($name);
191 $sql = "UPDATE $profiling ".
192 "SET pf_count=pf_count+{$eventCount}, ".
193 "pf_time=pf_time + {$timeSum} ".
194 "WHERE pf_name='{$encname}'";
197 $rc = $dbw->affectedRows();
199 $dbw->insert('profiling',array(
201 'pf_count'=>$eventCount,
202 'pf_time'=>$timeSum),
203 $fname,array('IGNORE'));
205 // When we upgrade to mysql 4.1, the insert+update
206 // can be merged into just a insert with this construct added:
207 // "ON DUPLICATE KEY UPDATE ".
208 // "pf_count=pf_count + VALUES(pf_count), ".
209 // "pf_time=pf_time + VALUES(pf_time)";
215 $wgProfiler = new Profiler();
216 $wgProfiler->profileIn( '-total' );