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