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