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