Re-enable toc
[lhc/web/wiklou.git] / includes / Profiling.php
1 <?
2 # This file is only included if profiling is enabled
3 function wfProfileIn( $functionname )
4 {
5 global $wgProfiler;
6 $wgProfiler->profileIn( $functionname );
7 }
8
9 function wfProfileOut( $functionname = "missing" )
10 {
11 global $wgProfiler;
12 $wgProfiler->profileOut( $functionname );
13 }
14
15 function wfGetProfilingOutput( $start, $elapsed ) {
16 global $wgProfiler;
17 return $wgProfiler->getOutput( $start, $elapsed );
18 }
19
20 function wfProfileClose()
21 {
22 global $wgProfiler;
23 $wgProfiler->close();
24 }
25
26 class Profiler
27 {
28 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
29 var $mCalls = array(), $mTotals = array();
30 /*
31 function Profiler()
32 {
33 $this->mProfileStack = array();
34 $this->mWorkStack = array();
35 $this->mCollated = array();
36 }
37 */
38
39 function profileIn( $functionname )
40 {
41 global $wgDebugFunctionEntry;
42 if ( $wgDebugFunctionEntry && function_exists( "wfDebug" ) ) {
43 wfDebug( "Entering $functionname\n" );
44 }
45 array_push( $this->mWorkStack, array($functionname, count( $this->mWorkStack ), microtime() ) );
46 }
47
48 function profileOut( $functionname)
49 {
50 global $wgDebugProfiling, $wgDebugFunctionEntry, $wgProfileToDatabase;
51
52 if ( $wgDebugFunctionEntry && function_exists( "wfDebug" ) ) {
53 wfDebug( "Exiting $functionname\n" );
54 }
55
56 $bit = array_pop( $this->mWorkStack );
57
58 if ( !$bit ) {
59 wfDebug( "Profiling error, !\$bit: $functionname\n" );
60 } else {
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" );
66 }
67 }
68 array_push( $bit, microtime() );
69 array_push( $this->mStack, $bit );
70 }
71 }
72
73 function close()
74 {
75 while ( count( $this->mWorkStack ) ) {
76 $this->profileOut( "close" );
77 }
78 }
79
80 function getOutput( $scriptStart, $scriptElapsed )
81 {
82 if( !count( $this->mStack ) ) {
83 return "No profiling output\n";
84 }
85 $width = 125;
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();
92
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" );
99 }
100 wfProfileOut( "-overhead-total" );
101
102 # Collate
103 foreach ( $this->mStack as $entry ) {
104 $fname = $entry[0];
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;
111 $this->mCollated[$fname] += $elapsed;
112 $this->mCalls[$fname] ++;
113 }
114
115 $total = $this->mCollated["-total"];
116 $overhead = $this->mCollated["-overhead-internal"] / $profileCount;
117 $this->mCalls["-overhead-total"] = $profileCount;
118
119 # Output
120 foreach ( $this->mCollated as $fname => $elapsed ) {
121 $calls = $this->mCalls[$fname];
122 # Adjust for overhead
123 if ( $fname[0] != "-" ) {
124 $elapsed -= $overhead * $calls;
125 }
126
127 $percent = $total ? 100. * $elapsed / $total : 0;
128 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
129 (float)($elapsed * 1000) / $calls, $percent );
130
131 if( $wgProfileToDatabase ) {
132 Profiler::logToDB( $fname, (float)($elapsed * 1000), $calls );
133 }
134 }
135 $prof .= "\nTotal: $total\n\n";
136
137 return $prof;
138 }
139
140
141 /* static */ function logToDB($name, $timeSum, $eventCount)
142 {
143 $name = wfStrencode( $name );
144 $sql = "UPDATE profiling ".
145 "SET pf_count=pf_count+{$eventCount}, ".
146 "pf_time=pf_time + {$timeSum} ".
147 "WHERE pf_name='{$name}'";
148 wfQuery($sql , DB_WRITE);
149
150 $rc = wfAffectedRows();
151 if( $rc == 0) {
152 $sql = "INSERT IGNORE INTO profiling (pf_name,pf_count,pf_time) ".
153 "VALUES ('{$name}', {$eventCount}, {$timeSum}) ";
154 wfQuery($sql , DB_WRITE);
155 $rc = wfAffectedRows();
156 }
157 // When we upgrade to mysql 4.1, the insert+update
158 // can be merged into just a insert with this construct added:
159 // "ON DUPLICATE KEY UPDATE ".
160 // "pf_count=pf_count + VALUES(pf_count), ".
161 // "pf_time=pf_time + VALUES(pf_time)";
162 }
163
164 }
165
166
167 $wgProfiler = new Profiler();
168 $wgProfiler->profileIn( "-total" );
169
170 ?>