Added feature for limiting profiling to every n requests
[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;
51 if ( $wgDebugFunctionEntry && function_exists( "wfDebug" ) ) {
52 wfDebug( "Exiting $functionname\n" );
53 }
54
55 $bit = array_pop( $this->mWorkStack );
56
57 if ( !$bit ) {
58 wfDebug( "Profiling error, !\$bit: $functionname\n" );
59 } else {
60 if ( $wgDebugProfiling ) {
61 if ( $functionname == "close" ) {
62 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
63 } elseif ( $bit[0] != $functionname ) {
64 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
65 }
66 }
67 array_push( $bit, microtime() );
68 array_push( $this->mStack, $bit );
69 }
70 }
71
72 function close()
73 {
74 while ( count( $this->mWorkStack ) ) {
75 $this->profileOut( "close" );
76 }
77 }
78
79 function getOutput( $scriptStart, $scriptElapsed )
80 {
81 if( !count( $this->mStack ) ) {
82 return "No profiling output\n";
83 }
84 $width = 125;
85 $format = "%-" . ($width - 28) . "s %6d %6.3f %6.3f %6.3f%%\n";
86 $titleFormat = "%-" . ($width - 28) . "s %9s %9s %9s %9s\n";
87 $prof = "\nProfiling data\n";
88 $prof .= sprintf( $titleFormat, "Name", "Calls", "Total", "Each", "%" );
89 $this->mCollated = array();
90 $this->mCalls = array();
91
92 # Estimate profiling overhead
93 $profileCount = count( $this->mStack );
94 wfProfileIn( "-overhead-total" );
95 for ($i=0; $i<$profileCount ; $i++) {
96 wfProfileIn( "-overhead-internal" );
97 wfProfileOut( "-overhead-internal" );
98 }
99 wfProfileOut( "-overhead-total" );
100
101 # Collate
102 foreach ( $this->mStack as $entry ) {
103 $fname = $entry[0];
104 $thislevel = $entry[1];
105 $start = explode( " ", $entry[2]);
106 $start = (float)$start[0] + (float)$start[1];
107 $end = explode( " ", $entry[3]);
108 $end = (float)$end[0] + (float)$end[1];
109 $elapsed = $end - $start;
110 $this->mCollated[$fname] += $elapsed;
111 $this->mCalls[$fname] ++;
112 }
113
114 $total = $this->mCollated["-total"];
115 $overhead = $this->mCollated["-overhead-internal"] / $profileCount;
116 $this->mCalls["-overhead-total"] = $profileCount;
117
118 # Output
119 foreach ( $this->mCollated as $fname => $elapsed ) {
120 $calls = $this->mCalls[$fname];
121 # Adjust for overhead
122 if ( $fname[0] != "-" ) {
123 $elapsed -= $overhead * $calls;
124 }
125
126 $percent = $total ? 100. * $elapsed / $total : 0;
127 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
128 (float)($elapsed * 1000) / $calls, $percent );
129 }
130 $prof .= "\nTotal: $total\n\n";
131
132 return $prof;
133 }
134 }
135
136 $wgProfiler = new Profiler();
137 $wgProfiler->profileIn( "-total" );
138
139 ?>