* Added Profiler::isStub() to check if we are using a stub profiler, instead of check...
[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 function __construct() {
16 global $wgRequestTime, $wgRUstart;
17 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
18 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
19
20 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
21 $elapsedreal = microtime(true) - $wgRequestTime;
22
23 $entry =& $this->mCollated["-setup"];
24 if (!is_array($entry)) {
25 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
26 $this->mCollated["-setup"] =& $entry;
27 }
28 $entry['cpu'] += $elapsedcpu;
29 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
30 $entry['real'] += $elapsedreal;
31 $entry['real_sq'] += $elapsedreal*$elapsedreal;
32 $entry['count']++;
33 }
34 }
35
36 function setMinimum( $min ) {
37 $this->mMinimumTime = $min;
38 }
39
40 function profileIn($functionname) {
41 global $wgDebugFunctionEntry;
42 if ($wgDebugFunctionEntry) {
43 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
44 }
45 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
46 }
47
48 function profileOut($functionname) {
49 global $wgDebugFunctionEntry;
50
51 if ($wgDebugFunctionEntry) {
52 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
53 }
54
55 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
56
57 if (!$ofname) {
58 $this->debug("Profiling error: $functionname\n");
59 } else {
60 if ($functionname == 'close') {
61 $message = "Profile section ended by close(): {$ofname}";
62 $functionname = $ofname;
63 $this->debug( "$message\n" );
64 $this->mCollated[$message] = array(
65 'real' => 0.0, 'count' => 1);
66 }
67 elseif ($ofname != $functionname) {
68 $message = "Profiling error: in({$ofname}), out($functionname)";
69 $this->debug( "$message\n" );
70 $this->mCollated[$message] = array(
71 'real' => 0.0, 'count' => 1);
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 $entry['cpu'] += $elapsedcpu;
81 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
82 $entry['real'] += $elapsedreal;
83 $entry['real_sq'] += $elapsedreal*$elapsedreal;
84 $entry['count']++;
85
86 }
87 }
88
89 public function getFunctionReport() {
90 /* Implement in output subclasses */
91 return '';
92 }
93
94 public function logData() {
95 /* Implement in subclasses */
96 }
97
98 function getCpuTime($ru=null) {
99 if ( function_exists( 'getrusage' ) ) {
100 if ( $ru == null ) {
101 $ru = getrusage();
102 }
103 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
104 $ru['ru_stime.tv_usec']) * 1e-6);
105 } else {
106 return 0;
107 }
108 }
109
110 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
111 function getTime($time=null) {
112 if ($time==null) {
113 return microtime(true);
114 }
115 list($a,$b)=explode(" ",$time);
116 return (float)($a+$b);
117 }
118 }