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