Merge "* Added Profiler::addInitialStack() to separate definitions of local variables...
[lhc/web/wiklou.git] / includes / profiler / ProfilerSimple.php
1 <?php
2 /**
3 * @file
4 * @ingroup Profiler
5 */
6
7 /**
8 * Simple profiler base class.
9 * @todo document methods (?)
10 * @ingroup Profiler
11 */
12 class ProfilerSimple extends Profiler {
13 var $mMinimumTime = 0;
14
15 var $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
16 var $errorEntry;
17
18 protected function addInitialStack() {
19 global $wgRequestTime, $wgRUstart;
20
21 $this->errorEntry = $this->zeroEntry;
22 $this->errorEntry['count'] = 1;
23
24 if ( !empty( $wgRequestTime ) && !empty( $wgRUstart ) ) {
25 $initialCpu = $this->getCpuTime( $wgRUstart );
26 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, $initialCpu );
27 $this->mWorkStack[] = array( '-setup', 1, $wgRequestTime, $initialCpu );
28
29 $this->profileOut( '-setup' );
30 } else {
31 $this->profileIn( '-total' );
32 }
33 }
34
35 function setMinimum( $min ) {
36 $this->mMinimumTime = $min;
37 }
38
39 function profileIn($functionname) {
40 global $wgDebugFunctionEntry;
41 if ($wgDebugFunctionEntry) {
42 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
43 }
44 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
45 }
46
47 function profileOut($functionname) {
48 global $wgDebugFunctionEntry;
49
50 if ($wgDebugFunctionEntry) {
51 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
52 }
53
54 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
55
56 if (!$ofname) {
57 $this->debug("Profiling error: $functionname\n");
58 } else {
59 if ($functionname == 'close') {
60 $message = "Profile section ended by close(): {$ofname}";
61 $functionname = $ofname;
62 $this->debug( "$message\n" );
63 $this->mCollated[$message] = $this->errorEntry;
64 }
65 elseif ($ofname != $functionname) {
66 $message = "Profiling error: in({$ofname}), out($functionname)";
67 $this->debug( "$message\n" );
68 $this->mCollated[$message] = $this->errorEntry;
69 }
70 $entry =& $this->mCollated[$functionname];
71 $elapsedcpu = $this->getCpuTime() - $octime;
72 $elapsedreal = microtime(true) - $ortime;
73 if (!is_array($entry)) {
74 $entry = $this->zeroEntry;
75 $this->mCollated[$functionname] =& $entry;
76 }
77 $entry['cpu'] += $elapsedcpu;
78 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
79 $entry['real'] += $elapsedreal;
80 $entry['real_sq'] += $elapsedreal*$elapsedreal;
81 $entry['count']++;
82
83 }
84 }
85
86 public function getFunctionReport() {
87 /* Implement in output subclasses */
88 return '';
89 }
90
91 public function logData() {
92 /* Implement in subclasses */
93 }
94
95 function getCpuTime($ru=null) {
96 if ( function_exists( 'getrusage' ) ) {
97 if ( $ru == null ) {
98 $ru = getrusage();
99 }
100 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
101 $ru['ru_stime.tv_usec']) * 1e-6);
102 } else {
103 return 0;
104 }
105 }
106 }