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