* Redundant script that isn't used by anything
[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["-setup"] =& $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 global $wgDebugFunctionEntry;
65
66 if ($wgDebugFunctionEntry) {
67 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
68 }
69
70 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
71
72 if (!$ofname) {
73 $this->debug("Profiling error: $functionname\n");
74 } else {
75 if ($functionname == 'close') {
76 $message = "Profile section ended by close(): {$ofname}";
77 $functionname = $ofname;
78 $this->debug( "$message\n" );
79 }
80 elseif ($ofname != $functionname) {
81 $message = "Profiling error: in({$ofname}), out($functionname)";
82 $this->debug( "$message\n" );
83 }
84 $entry =& $this->mCollated[$functionname];
85 $elapsedcpu = $this->getCpuTime() - $octime;
86 $elapsedreal = microtime(true) - $ortime;
87 if (!is_array($entry)) {
88 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
89 $this->mCollated[$functionname] =& $entry;
90
91 }
92 $entry['cpu'] += $elapsedcpu;
93 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
94 $entry['real'] += $elapsedreal;
95 $entry['real_sq'] += $elapsedreal*$elapsedreal;
96 $entry['count']++;
97
98 }
99 }
100
101 function getFunctionReport() {
102 /* Implement in output subclasses */
103 }
104
105 function getCpuTime($ru=null) {
106 if ( function_exists( 'getrusage' ) ) {
107 if ( $ru == null )
108 $ru = getrusage();
109 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
110 $ru['ru_stime.tv_usec']) * 1e-6);
111 } else {
112 return 0;
113 }
114 }
115
116 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
117 function getTime($time=null) {
118 if ($time==null)
119 return microtime(true);
120 list($a,$b)=explode(" ",$time);
121 return (float)($a+$b);
122 }
123
124 function debug( $s ) {
125 if (function_exists( 'wfDebug' ) ) {
126 wfDebug( $s );
127 }
128 }
129 }
130 ?>