3 * This file is only included if profiling is enabled
7 * @param $functioname name of the function we will profile
9 function wfProfileIn( $functionname ) {
11 $wgProfiler->profileIn( $functionname );
15 * @param $functioname name of the function we have profiled
17 function wfProfileOut( $functionname = 'missing' ) {
19 $wgProfiler->profileOut( $functionname );
22 function wfGetProfilingOutput( $start, $elapsed ) {
24 return $wgProfiler->getOutput( $start, $elapsed );
27 function wfProfileClose() {
32 if( !function_exists( 'memory_get_usage' ) ) {
33 # Old PHP or --enable-memory-limit not compiled in
34 function memory_get_usage() {
44 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
45 var $mCalls = array(), $mTotals = array();
49 $this->mProfileStack = array();
50 $this->mWorkStack = array();
51 $this->mCollated = array();
55 function profileIn( $functionname ) {
56 global $wgDebugFunctionEntry;
57 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
58 wfDebug( str_repeat( ' ', count( $this->mWorkStack
) ) . 'Entering '.$functionname."\n" );
60 array_push( $this->mWorkStack
, array($functionname, count( $this->mWorkStack
), microtime(), memory_get_usage() ) );
63 function profileOut( $functionname ) {
64 $memory = memory_get_usage();
65 global $wgDebugProfiling, $wgDebugFunctionEntry;
67 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
68 wfDebug( str_repeat( ' ', count( $this->mWorkStack
) ) . 'Exiting '.$functionname."\n" );
71 $bit = array_pop( $this->mWorkStack
);
74 wfDebug( "Profiling error, !\$bit: $functionname\n" );
76 if ( $wgDebugProfiling ) {
77 if ( $functionname == 'close' ) {
78 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
79 } elseif ( $bit[0] != $functionname ) {
80 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
83 array_push( $bit, microtime() );
84 array_push( $bit, $memory );
85 array_push( $this->mStack
, $bit );
90 while ( count( $this->mWorkStack
) ) {
91 $this->profileOut( 'close' );
95 function getOutput() {
96 global $wgDebugFunctionEntry;
97 $wgDebugFunctionEntry = false;
99 if( !count( $this->mStack
) ) {
100 return "No profiling output\n";
104 $format = "%-" . ($width - 34) . "s %6d %6.3f %6.3f %6.3f%% %6d\n";
105 $titleFormat = "%-" . ($width - 34) . "s %9s %9s %9s %9s %6s\n";
106 $prof = "\nProfiling data\n";
107 $prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
108 $this->mCollated
= array();
109 $this->mCalls
= array();
110 $this->mMemory
= array();
112 # Estimate profiling overhead
113 $profileCount = count( $this->mStack
);
114 wfProfileIn( '-overhead-total' );
115 for ($i=0; $i<$profileCount ; $i++
) {
116 wfProfileIn( '-overhead-internal' );
117 wfProfileOut( '-overhead-internal' );
119 wfProfileOut( '-overhead-total' );
122 foreach ( $this->mStack
as $entry ) {
124 $thislevel = $entry[1];
125 $start = explode( ' ', $entry[2]);
126 $start = (float)$start[0] +
(float)$start[1];
127 $end = explode( ' ', $entry[4]);
128 $end = (float)$end[0] +
(float)$end[1];
129 $elapsed = $end - $start;
131 $memory = $entry[5] - $entry[3];
133 if ( !array_key_exists( $fname, $this->mCollated
) ) {
134 $this->mCollated
[$fname] = 0;
135 $this->mCalls
[$fname] = 0;
136 $this->mMemory
[$fname] = 0;
139 $this->mCollated
[$fname] +
= $elapsed;
140 $this->mCalls
[$fname] ++
;
141 $this->mMemory
[$fname] +
= $memory;
144 $total = @$this->mCollated
['-total'];
145 $overhead = $this->mCollated
['-overhead-internal'] / $profileCount;
146 $this->mCalls
['-overhead-total'] = $profileCount;
149 arsort( $this->mCollated
, SORT_NUMERIC
);
150 foreach ( $this->mCollated
as $fname => $elapsed ) {
151 $calls = $this->mCalls
[$fname];
152 # Adjust for overhead
153 if ( $fname[0] != '-' ) {
154 $elapsed -= $overhead * $calls;
157 $percent = $total ?
100. * $elapsed / $total : 0;
158 $memory = $this->mMemory
[$fname];
159 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
160 (float)($elapsed * 1000) / $calls, $percent, $memory );
162 global $wgProfileToDatabase;
163 if( $wgProfileToDatabase ) {
164 Profiler
::logToDB( $fname, (float)($elapsed * 1000), $calls );
167 $prof .= "\nTotal: $total\n\n";
176 function logToDB($name, $timeSum, $eventCount) {
177 $dbw =& wfGetDB( DB_MASTER
);
178 $profiling = $dbw->tableName( 'profiling' );
180 $name = substr($dbw->strencode( $name ),0,255);
181 $sql = "UPDATE $profiling ".
182 "SET pf_count=pf_count+{$eventCount}, ".
183 "pf_time=pf_time + {$timeSum} ".
184 "WHERE pf_name='{$name}'";
187 $rc = $dbw->affectedRows();
189 $dbw->insertArray($profiling,array(
191 'pf_count'=>$eventCount,
192 'pf_time'=>$timeSum),
193 $fname,array('IGNORE'));
195 // When we upgrade to mysql 4.1, the insert+update
196 // can be merged into just a insert with this construct added:
197 // "ON DUPLICATE KEY UPDATE ".
198 // "pf_count=pf_count + VALUES(pf_count), ".
199 // "pf_time=pf_time + VALUES(pf_time)";
205 $wgProfiler = new Profiler();
206 $wgProfiler->profileIn( '-total' );