* importing livehack for profiling errors output
[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),microtime(true));
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) = 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 $this->mCollated[$message] = array(
72 'real' => 0.0, 'count' => 1);
73 }
74 elseif ($ofname != $functionname) {
75 $message = "Profiling error: in({$ofname}), out($functionname)";
76 $this->debug( "$message\n" );
77 $this->mCollated[$message] = array(
78 'real' => 0.0, 'count' => 1);
79 }
80 $entry =& $this->mCollated[$functionname];
81 $elapsedreal = microtime(true) - $ortime;
82 if (!is_array($entry)) {
83 $entry = array('real' => 0.0, 'count' => 0);
84 $this->mCollated[$functionname] =& $entry;
85 }
86 $entry['real'] += $elapsedreal;
87 $entry['count']++;
88
89 }
90 }
91
92 function getFunctionReport() {
93 /* Implement in output subclasses */
94 }
95
96 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
97 function getTime($time=null) {
98 if ($time==null)
99 return microtime(true);
100 list($a,$b)=explode(" ",$time);
101 return (float)($a+$b);
102 }
103
104 function debug( $s ) {
105 if (function_exists( 'wfDebug' ) ) {
106 wfDebug( $s );
107 }
108 }
109 }
110