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