Various fixes, see the version of [[m:Development status]] before this commit for...
[lhc/web/wiklou.git] / includes / Profiling.php
1 <?
2 # This file is only included if profiling is enabled
3 $wgDebugProfiling = true;
4
5 function wfProfileIn( $functionname )
6 {
7 global $wgProfiler;
8 $wgProfiler->profileIn( $functionname );
9 }
10
11 function wfProfileOut( $functionname = "missing" )
12 {
13 global $wgProfiler;
14 $wgProfiler->profileOut( $functionname );
15 }
16
17 function wfGetProfilingOutput( $start, $elapsed ) {
18 global $wgProfiler;
19 return $wgProfiler->getOutput( $start, $elapsed );
20 }
21
22 function wfProfileClose()
23 {
24 global $wgProfiler;
25 $wgProfiler->close();
26 }
27
28 class Profiler
29 {
30 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
31 var $mCalls = array(), $mTotals = array();
32 /*
33 function Profiler()
34 {
35 $this->mProfileStack = array();
36 $this->mWorkStack = array();
37 $this->mCollated = array();
38 }
39 */
40
41 function profileIn( $functionname )
42 {
43 array_push( $this->mWorkStack, array($functionname, count( $this->mWorkStack ), microtime() ) );
44 }
45
46 function profileOut( $functionname)
47 {
48 global $wgDebugProfiling;
49 $bit = array_pop( $this->mWorkStack );
50
51 if ( !$bit ) {
52 wfDebug( "Profiling error, !\$bit: $functionname\n" );
53 } else {
54 if ( $wgDebugProfiling ) {
55 if ( $functionname == "close" ) {
56 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
57 } elseif ( $bit[0] != $functionname ) {
58 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
59 }
60 }
61 array_push( $bit, microtime() );
62 array_push( $this->mStack, $bit );
63 }
64 }
65
66 function close()
67 {
68 while ( count( $this->mWorkStack ) ) {
69 $this->profileOut( "close" );
70 }
71 }
72
73 function getOutput( $scriptStart, $scriptElapsed )
74 {
75 if( !count( $this->mStack ) ) {
76 return "No profiling output\n";
77 }
78
79 $format = "%-49s %6d %6.3f %6.3f %6.3f%%\n";
80 $titleFormat = "%-49s %9s %9s %9s %9s\n";
81 $prof = "\nProfiling data\n";
82 $prof .= sprintf( $titleFormat, "Name", "Calls", "Total", "Each", "%" );
83 $this->mCollated = array();
84 $this->mCalls = array();
85
86 # Estimate profiling overhead
87 $profileCount = count( $this->mStack );
88 wfProfileIn( "-overhead-total" );
89 for ($i=0; $i<$profileCount ; $i++) {
90 wfProfileIn( "-overhead-internal" );
91 wfProfileOut( "-overhead-internal" );
92 }
93 wfProfileOut( "-overhead-total" );
94
95 # Collate
96 foreach ( $this->mStack as $entry ) {
97 $fname = $entry[0];
98 $thislevel = $entry[1];
99 $start = explode( " ", $entry[2]);
100 $start = (float)$start[0] + (float)$start[1];
101 $end = explode( " ", $entry[3]);
102 $end = (float)$end[0] + (float)$end[1];
103 $elapsed = $end - $start;
104 $this->mCollated[$fname] += $elapsed;
105 $this->mCalls[$fname] ++;
106 }
107
108 $total = $this->mCollated["-total"];
109 $overhead = $this->mCollated["-overhead-internal"] / $profileCount;
110 $this->mCalls["-overhead-total"] = $profileCount;
111
112 # Output
113 foreach ( $this->mCollated as $fname => $elapsed ) {
114 $calls = $this->mCalls[$fname];
115 # Adjust for overhead
116 if ( $fname[0] != "-" ) {
117 $elapsed -= $overhead * $calls;
118 }
119
120 $percent = $total ? 100. * $elapsed / $total : 0;
121 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
122 (float)($elapsed * 1000) / $calls, $percent );
123 }
124 $prof .= "\nTotal: $total\n\n";
125
126 return $prof;
127 }
128 }
129
130 $wgProfiler = new Profiler();
131 $wgProfiler->profileIn( "-total" );
132
133 ?>