e82fd23fac06ecf49ff8dedb11d158d1edac2e68
[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 $total = 0;
82
83 # Estimate profiling overhead
84 $profileCount = count( $this->mStack );
85 for ($i=0; $i<$profileCount ; $i++) {
86 wfProfileIn( "--profiling overhead--" );
87 wfProfileOut( "--profiling overhead--" );
88 }
89
90 # Collate
91 foreach ( $this->mStack as $entry ) {
92 $fname = $entry[0];
93 $thislevel = $entry[1];
94 $start = explode( " ", $entry[2]);
95 $start = (float)$start[0] + (float)$start[1];
96 $end = explode( " ", $entry[3]);
97 $end = (float)$end[0] + (float)$end[1];
98 $elapsed = $end - $start;
99 $this->mCollated[$fname] += $elapsed;
100 $this->mCalls[$fname] ++;
101
102 if ( $fname != "--profiling overhead--" ) {
103 $total += $elapsed;
104 }
105 }
106
107 $overhead = $this->mCollated["--profiling overhead--"] / $this->mCalls["--profiling overhead--"];
108
109 # Output
110 foreach ( $this->mCollated as $fname => $elapsed ) {
111 $calls = $this->mCalls[$fname];
112 # Adjust for overhead
113 if ( $fname != "--profiling overhead--" ) {
114 $elapsed -= $overhead * $calls;
115 }
116
117 $percent = $total ? 100. * $elapsed / $total : 0;
118 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
119 (float)($elapsed * 1000) / $calls, $percent );
120 }
121 $prof .= "\nTotal: $total\n\n";
122
123 return $prof;
124 }
125 }
126
127 $wgProfiler = new Profiler();
128
129 ?>