Add the ability to change the profile ID for UDP profiling at runtime
[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 var $mProfileID = false;
16
17 function ProfilerSimple() {
18 global $wgRequestTime,$wgRUstart;
19 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
20 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
21
22 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
23 $elapsedreal = microtime(true) - $wgRequestTime;
24
25 $entry =& $this->mCollated["-setup"];
26 if (!is_array($entry)) {
27 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
28 $this->mCollated[$functionname] =& $entry;
29
30 }
31 $entry['cpu'] += $elapsedcpu;
32 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
33 $entry['real'] += $elapsedreal;
34 $entry['real_sq'] += $elapsedreal*$elapsedreal;
35 $entry['count']++;
36 }
37 }
38
39 function setMinimum( $min ) {
40 $this->mMinimumTime = $min;
41 }
42
43 function setProfileID( $id ) {
44 $this->mProfileID = $id;
45 }
46
47 function getProfileID() {
48 if ( $this->mProfileID === false ) {
49 return wfWikiID();
50 } else {
51 return $this->mProfileID;
52 }
53 }
54
55 function profileIn($functionname) {
56 global $wgDebugFunctionEntry;
57 if ($wgDebugFunctionEntry) {
58 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
59 }
60 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
61 }
62
63 function profileOut($functionname) {
64 $memory = memory_get_usage();
65
66 global $wgDebugFunctionEntry;
67
68 if ($wgDebugFunctionEntry) {
69 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
70 }
71
72 list($ofname,$ocount,$ortime,$octime) = array_pop($this->mWorkStack);
73
74 if (!$ofname) {
75 $this->debug("Profiling error: $functionname\n");
76 } else {
77 if ($functionname == 'close') {
78 $message = "Profile section ended by close(): {$ofname}";
79 $functionname = $ofname;
80 $this->debug( "$message\n" );
81 }
82 elseif ($ofname != $functionname) {
83 $message = "Profiling error: in({$ofname}), out($functionname)";
84 $this->debug( "$message\n" );
85 }
86 $entry =& $this->mCollated[$functionname];
87 $elapsedcpu = $this->getCpuTime() - $octime;
88 $elapsedreal = microtime(true) - $ortime;
89 if (!is_array($entry)) {
90 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
91 $this->mCollated[$functionname] =& $entry;
92
93 }
94 $entry['cpu'] += $elapsedcpu;
95 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
96 $entry['real'] += $elapsedreal;
97 $entry['real_sq'] += $elapsedreal*$elapsedreal;
98 $entry['count']++;
99
100 }
101 }
102
103 function getFunctionReport() {
104 /* Implement in output subclasses */
105 }
106
107 function getCpuTime($ru=null) {
108 if ( function_exists( 'getrusage' ) ) {
109 if ( $ru == null )
110 $ru = getrusage();
111 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
112 $ru['ru_stime.tv_usec']) * 1e-6);
113 } else {
114 return 0;
115 }
116 }
117
118 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
119 function getTime($time=null) {
120 if ($time==null)
121 return microtime(true);
122 list($a,$b)=explode(" ",$time);
123 return (float)($a+$b);
124 }
125
126 function debug( $s ) {
127 if (function_exists( 'wfDebug' ) ) {
128 wfDebug( $s );
129 }
130 }
131 }
132 ?>