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