Add "redirect" and "querycachetwo" tables.
[lhc/web/wiklou.git] / includes / ProfilerSimple.php
1 <?php
2 /**
3 * Simple profiler base class
4 * @package MediaWiki
5 */
6
7 /**
8 * @todo document
9 * @package MediaWiki
10 */
11 require_once(dirname(__FILE__).'/Profiler.php');
12
13 class ProfilerSimple extends Profiler {
14 var $mMinimumTime = 0;
15
16 function ProfilerSimple() {
17 global $wgRequestTime,$wgRUstart;
18 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
19 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
20
21 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
22 $elapsedreal = microtime(true) - $wgRequestTime;
23
24 $entry =& $this->mCollated["-setup"];
25 if (!is_array($entry)) {
26 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
27 $this->mCollated[$functionname] =& $entry;
28
29 }
30 $entry['cpu'] += $elapsedcpu;
31 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
32 $entry['real'] += $elapsedreal;
33 $entry['real_sq'] += $elapsedreal*$elapsedreal;
34 $entry['count']++;
35 }
36 }
37
38 function setMinimum( $min ) {
39 $this->mMinimumTime = $min;
40 }
41
42 function profileIn($functionname) {
43 global $wgDebugFunctionEntry;
44 if ($wgDebugFunctionEntry) {
45 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
46 }
47 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
48 }
49
50 function profileOut($functionname) {
51 $memory = memory_get_usage();
52
53 global $wgDebugFunctionEntry;
54
55 if ($wgDebugFunctionEntry) {
56 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
57 }
58
59 list($ofname,$ocount,$ortime,$octime) = array_pop($this->mWorkStack);
60
61 if (!$ofname) {
62 $this->debug("Profiling error: $functionname\n");
63 } else {
64 if ($functionname == 'close') {
65 $message = "Profile section ended by close(): {$ofname}";
66 $functionname = $ofname;
67 $this->debug( "$message\n" );
68 }
69 elseif ($ofname != $functionname) {
70 $message = "Profiling error: in({$ofname}), out($functionname)";
71 $this->debug( "$message\n" );
72 }
73 $entry =& $this->mCollated[$functionname];
74 $elapsedcpu = $this->getCpuTime() - $octime;
75 $elapsedreal = microtime(true) - $ortime;
76 if (!is_array($entry)) {
77 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
78 $this->mCollated[$functionname] =& $entry;
79
80 }
81 $entry['cpu'] += $elapsedcpu;
82 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
83 $entry['real'] += $elapsedreal;
84 $entry['real_sq'] += $elapsedreal*$elapsedreal;
85 $entry['count']++;
86
87 }
88 }
89
90 function getFunctionReport() {
91 /* Implement in output subclasses */
92 }
93
94 function getCpuTime($ru=null) {
95 if ( function_exists( 'getrusage' ) ) {
96 if ( $ru == null )
97 $ru = getrusage();
98 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
99 $ru['ru_stime.tv_usec']) * 1e-6);
100 } else {
101 return 0;
102 }
103 }
104
105 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
106 function getTime($time=null) {
107 if ($time==null)
108 return microtime(true);
109 list($a,$b)=explode(" ",$time);
110 return (float)($a+$b);
111 }
112
113 function debug( $s ) {
114 if (function_exists( 'wfDebug' ) ) {
115 wfDebug( $s );
116 }
117 }
118 }
119 ?>