(part of bug 6100) Set the directionality based on user language instead of content...
[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 function __construct( $params ) {
16 global $wgRequestTime, $wgRUstart;
17 parent::__construct( $params );
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 profileIn($functionname) {
42 global $wgDebugFunctionEntry;
43 if ($wgDebugFunctionEntry) {
44 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
45 }
46 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
47 }
48
49 function profileOut($functionname) {
50 global $wgDebugFunctionEntry;
51
52 if ($wgDebugFunctionEntry) {
53 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
54 }
55
56 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
57
58 if (!$ofname) {
59 $this->debug("Profiling error: $functionname\n");
60 } else {
61 if ($functionname == 'close') {
62 $message = "Profile section ended by close(): {$ofname}";
63 $functionname = $ofname;
64 $this->debug( "$message\n" );
65 $this->mCollated[$message] = array(
66 'real' => 0.0, 'count' => 1);
67 }
68 elseif ($ofname != $functionname) {
69 $message = "Profiling error: in({$ofname}), out($functionname)";
70 $this->debug( "$message\n" );
71 $this->mCollated[$message] = array(
72 'real' => 0.0, 'count' => 1);
73 }
74 $entry =& $this->mCollated[$functionname];
75 $elapsedcpu = $this->getCpuTime() - $octime;
76 $elapsedreal = microtime(true) - $ortime;
77 if (!is_array($entry)) {
78 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
79 $this->mCollated[$functionname] =& $entry;
80 }
81 $entry['cpu'] += $elapsedcpu;
82 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
83 $entry['real'] += $elapsedreal;
84 $entry['real_sq'] += $elapsedreal*$elapsedreal;
85 $entry['count']++;
86
87 }
88 }
89
90 public function getFunctionReport() {
91 /* Implement in output subclasses */
92 return '';
93 }
94
95 public function logData() {
96 /* Implement in subclasses */
97 }
98
99 function getCpuTime($ru=null) {
100 if ( function_exists( 'getrusage' ) ) {
101 if ( $ru == null ) {
102 $ru = getrusage();
103 }
104 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
105 $ru['ru_stime.tv_usec']) * 1e-6);
106 } else {
107 return 0;
108 }
109 }
110
111 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
112 function getTime($time=null) {
113 if ($time==null) {
114 return microtime(true);
115 }
116 list($a,$b)=explode(" ",$time);
117 return (float)($a+$b);
118 }
119 }