begin cleanup on magnus' url upload thingy
[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 function ProfilerSimple() {
15 global $wgRequestTime,$wgRUstart;
16 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
17 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
18
19 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
20 $elapsedreal = microtime(true) - $wgRequestTime;
21
22 $entry =& $this->mCollated["-setup"];
23 if (!is_array($entry)) {
24 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
25 $this->mCollated[$functionname] =& $entry;
26
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 profileIn($functionname) {
37 global $wgDebugFunctionEntry;
38 if ($wgDebugFunctionEntry) {
39 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
40 }
41 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
42 }
43
44 function profileOut($functionname) {
45 $memory = memory_get_usage();
46
47 global $wgDebugFunctionEntry;
48
49 if ($wgDebugFunctionEntry) {
50 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
51 }
52
53 list($ofname,$ocount,$ortime,$octime) = array_pop($this->mWorkStack);
54
55 if (!$ofname) {
56 $this->debug("Profiling error: $functionname\n");
57 } else {
58 if ($functionname == 'close') {
59 $message = "Profile section ended by close(): {$ofname}";
60 $functionname = $ofname;
61 $this->debug( "$message\n" );
62 }
63 elseif ($ofname != $functionname) {
64 $message = "Profiling error: in({$ofname}), out($functionname)";
65 $this->debug( "$message\n" );
66 }
67 $entry =& $this->mCollated[$functionname];
68 $elapsedcpu = $this->getCpuTime() - $octime;
69 $elapsedreal = microtime(true) - $ortime;
70 if (!is_array($entry)) {
71 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
72 $this->mCollated[$functionname] =& $entry;
73
74 }
75 $entry['cpu'] += $elapsedcpu;
76 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
77 $entry['real'] += $elapsedreal;
78 $entry['real_sq'] += $elapsedreal*$elapsedreal;
79 $entry['count']++;
80
81 }
82 }
83
84 function getFunctionReport() {
85 /* Implement in output subclasses */
86 }
87
88 function getCpuTime($ru=null) {
89 if ( function_exists( 'getrusage' ) ) {
90 if ( $ru == null )
91 $ru = getrusage();
92 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
93 $ru['ru_stime.tv_usec']) * 1e-6);
94 } else {
95 return 0;
96 }
97 }
98
99 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
100 function getTime($time=null) {
101 if ($time==null)
102 return microtime(true);
103 list($a,$b)=explode(" ",$time);
104 return (float)($a+$b);
105 }
106
107 function debug( $s ) {
108 if (function_exists( 'wfDebug' ) ) {
109 wfDebug( $s );
110 }
111 }
112 }
113 ?>