Changing comments layout preparing for generated documentation with Phpdocumentor
[lhc/web/wiklou.git] / includes / Profiling.php
1 <?php
2 /**
3 * This file is only included if profiling is enabled
4 */
5
6 /**
7 * @param $functioname name of the function we will profile
8 */
9 function wfProfileIn( $functionname ) {
10 global $wgProfiler;
11 $wgProfiler->profileIn( $functionname );
12 }
13
14 /**
15 * @param $functioname name of the function we have profiled
16 */
17 function wfProfileOut( $functionname = 'missing' ) {
18 global $wgProfiler;
19 $wgProfiler->profileOut( $functionname );
20 }
21
22 function wfGetProfilingOutput( $start, $elapsed ) {
23 global $wgProfiler;
24 return $wgProfiler->getOutput( $start, $elapsed );
25 }
26
27 function wfProfileClose() {
28 global $wgProfiler;
29 $wgProfiler->close();
30 }
31
32 if( !function_exists( 'memory_get_usage' ) ) {
33 # Old PHP or --enable-memory-limit not compiled in
34 function memory_get_usage() {
35 return 0;
36 }
37 }
38
39 /**
40 * @todo document
41 */
42 class Profiler
43 {
44 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
45 var $mCalls = array(), $mTotals = array();
46 /*
47 function Profiler()
48 {
49 $this->mProfileStack = array();
50 $this->mWorkStack = array();
51 $this->mCollated = array();
52 }
53 */
54
55 function profileIn( $functionname ) {
56 global $wgDebugFunctionEntry;
57 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
58 wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering '.$functionname."\n" );
59 }
60 array_push( $this->mWorkStack, array($functionname, count( $this->mWorkStack ), microtime(), memory_get_usage() ) );
61 }
62
63 function profileOut( $functionname ) {
64 $memory = memory_get_usage();
65 global $wgDebugProfiling, $wgDebugFunctionEntry;
66
67 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
68 wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Exiting '.$functionname."\n" );
69 }
70
71 $bit = array_pop( $this->mWorkStack );
72
73 if ( !$bit ) {
74 wfDebug( "Profiling error, !\$bit: $functionname\n" );
75 } else {
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" );
81 }
82 }
83 array_push( $bit, microtime() );
84 array_push( $bit, $memory );
85 array_push( $this->mStack, $bit );
86 }
87 }
88
89 function close() {
90 while ( count( $this->mWorkStack ) ) {
91 $this->profileOut( 'close' );
92 }
93 }
94
95 function getOutput() {
96 global $wgDebugFunctionEntry;
97 $wgDebugFunctionEntry = false;
98
99 if( !count( $this->mStack ) ) {
100 return "No profiling output\n";
101 }
102 $this->close();
103 $width = 125;
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();
111
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' );
118 }
119 wfProfileOut( '-overhead-total' );
120
121 # Collate
122 foreach ( $this->mStack as $entry ) {
123 $fname = $entry[0];
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;
130
131 $memory = $entry[5] - $entry[3];
132
133 if ( !array_key_exists( $fname, $this->mCollated ) ) {
134 $this->mCollated[$fname] = 0;
135 $this->mCalls[$fname] = 0;
136 $this->mMemory[$fname] = 0;
137 }
138
139 $this->mCollated[$fname] += $elapsed;
140 $this->mCalls[$fname] ++;
141 $this->mMemory[$fname] += $memory;
142 }
143
144 $total = @$this->mCollated['-total'];
145 $overhead = $this->mCollated['-overhead-internal'] / $profileCount;
146 $this->mCalls['-overhead-total'] = $profileCount;
147
148 # Output
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;
155 }
156
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 );
161
162 global $wgProfileToDatabase;
163 if( $wgProfileToDatabase ) {
164 Profiler::logToDB( $fname, (float)($elapsed * 1000), $calls );
165 }
166 }
167 $prof .= "\nTotal: $total\n\n";
168
169 return $prof;
170 }
171
172
173 /**
174 * @static
175 */
176 function logToDB($name, $timeSum, $eventCount) {
177 $dbw =& wfGetDB( DB_MASTER );
178 $profiling = $dbw->tableName( 'profiling' );
179
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}'";
185 $dbw->query($sql);
186
187 $rc = $dbw->affectedRows();
188 if( $rc == 0) {
189 $dbw->insertArray($profiling,array(
190 'pf_name'=>$name,
191 'pf_count'=>$eventCount,
192 'pf_time'=>$timeSum),
193 $fname,array('IGNORE'));
194 }
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)";
200 }
201
202 }
203
204
205 $wgProfiler = new Profiler();
206 $wgProfiler->profileIn( '-total' );
207 ?>