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