remove Cpu from here too
[lhc/web/wiklou.git] / includes / ProfilerSimple.php
1 <?php
2
3 require_once(dirname(__FILE__).'/Profiler.php');
4
5 /**
6 * Simple profiler base class.
7 * @todo document methods (?)
8 * @addtogroup Profiler
9 */
10 class ProfilerSimple extends Profiler {
11 var $mMinimumTime = 0;
12 var $mProfileID = false;
13
14 function __construct() {
15 global $wgRequestTime;
16 if (!empty($wgRequestTime)) {
17 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime);
18
19 $elapsedreal = microtime(true) - $wgRequestTime;
20
21 $entry =& $this->mCollated["-setup"];
22 if (!is_array($entry)) {
23 $entry = array('real' => 0.0, 'count' => 0);
24 $this->mCollated["-setup"] =& $entry;
25 }
26 $entry['real'] += $elapsedreal;
27 $entry['count']++;
28 }
29 }
30
31 function setMinimum( $min ) {
32 $this->mMinimumTime = $min;
33 }
34
35 function setProfileID( $id ) {
36 $this->mProfileID = $id;
37 }
38
39 function getProfileID() {
40 if ( $this->mProfileID === false ) {
41 return wfWikiID();
42 } else {
43 return $this->mProfileID;
44 }
45 }
46
47 function profileIn($functionname) {
48 global $wgDebugFunctionEntry;
49 if ($wgDebugFunctionEntry) {
50 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
51 }
52 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack));
53 }
54
55 function profileOut($functionname) {
56 global $wgDebugFunctionEntry;
57
58 if ($wgDebugFunctionEntry) {
59 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
60 }
61
62 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
63
64 if (!$ofname) {
65 $this->debug("Profiling error: $functionname\n");
66 } else {
67 if ($functionname == 'close') {
68 $message = "Profile section ended by close(): {$ofname}";
69 $functionname = $ofname;
70 $this->debug( "$message\n" );
71 }
72 elseif ($ofname != $functionname) {
73 $message = "Profiling error: in({$ofname}), out($functionname)";
74 $this->debug( "$message\n" );
75 }
76 $entry =& $this->mCollated[$functionname];
77 $elapsedreal = microtime(true) - $ortime;
78 if (!is_array($entry)) {
79 $entry = array('real' => 0.0, 'count' => 0);
80 $this->mCollated[$functionname] =& $entry;
81 }
82 $entry['real'] += $elapsedreal;
83 $entry['count']++;
84
85 }
86 }
87
88 function getFunctionReport() {
89 /* Implement in output subclasses */
90 }
91
92 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
93 function getTime($time=null) {
94 if ($time==null)
95 return microtime(true);
96 list($a,$b)=explode(" ",$time);
97 return (float)($a+$b);
98 }
99
100 function debug( $s ) {
101 if (function_exists( 'wfDebug' ) ) {
102 wfDebug( $s );
103 }
104 }
105 }
106