Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[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
15 var $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
16 var $errorEntry;
17
18 function __construct( $params ) {
19 global $wgRequestTime, $wgRUstart;
20 parent::__construct( $params );
21
22 $this->errorEntry = $this->zeroEntry;
23 $this->errorEntry['count'] = 1;
24
25 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
26 # Remove the -total entry from parent::__construct
27 $this->mWorkStack = array();
28
29 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
30
31 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
32 $elapsedreal = microtime(true) - $wgRequestTime;
33
34 $entry =& $this->mCollated["-setup"];
35 if (!is_array($entry)) {
36 $entry = $this->zeroEntry;
37 $this->mCollated["-setup"] =& $entry;
38 }
39 $entry['cpu'] += $elapsedcpu;
40 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
41 $entry['real'] += $elapsedreal;
42 $entry['real_sq'] += $elapsedreal*$elapsedreal;
43 $entry['count']++;
44 }
45 }
46
47 function setMinimum( $min ) {
48 $this->mMinimumTime = $min;
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 $this->mCollated[$message] = $this->errorEntry;
76 }
77 elseif ($ofname != $functionname) {
78 $message = "Profiling error: in({$ofname}), out($functionname)";
79 $this->debug( "$message\n" );
80 $this->mCollated[$message] = $this->errorEntry;
81 }
82 $entry =& $this->mCollated[$functionname];
83 $elapsedcpu = $this->getCpuTime() - $octime;
84 $elapsedreal = microtime(true) - $ortime;
85 if (!is_array($entry)) {
86 $entry = $this->zeroEntry;
87 $this->mCollated[$functionname] =& $entry;
88 }
89 $entry['cpu'] += $elapsedcpu;
90 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
91 $entry['real'] += $elapsedreal;
92 $entry['real_sq'] += $elapsedreal*$elapsedreal;
93 $entry['count']++;
94
95 }
96 }
97
98 public function getFunctionReport() {
99 /* Implement in output subclasses */
100 return '';
101 }
102
103 public function logData() {
104 /* Implement in subclasses */
105 }
106
107 function getCpuTime($ru=null) {
108 if ( function_exists( 'getrusage' ) ) {
109 if ( $ru == null ) {
110 $ru = getrusage();
111 }
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 }
124 list($a,$b)=explode(" ",$time);
125 return (float)($a+$b);
126 }
127 }