* Fix ::close()
[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 class ProfilerSimple extends Profiler {
12 function ProfilerSimple() {
13 global $wgRequestTime,$wgRUstart;
14 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
15 $this->mWorkStack[] = array( '-total', 0, $this->getTime($wgRequestTime),$this->getCpuTime($wgRUstart));
16
17 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
18 $elapsedreal = $this->getTime() - $this->getTime($wgRequestTime);
19
20 $entry =& $this->mCollated["-setup"];
21 $entry['cpu'] += $elapsedcpu;
22 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
23 $entry['real'] += $elapsedreal;
24 $entry['real_sq'] += $elapsedreal*$elapsedreal;
25 $entry['count']++;
26 }
27 }
28
29 function profileIn($functionname) {
30 global $wgDebugFunctionEntry;
31 if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
32 wfDebug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
33 }
34 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getTime(), $this->getCpuTime());
35 }
36
37 function profileOut($functionname) {
38 $memory = memory_get_usage();
39
40 global $wgDebugFunctionEntry;
41
42 if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
43 wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
44 }
45
46 list($ofname,$ocount,$ortime,$octime) = array_pop($this->mWorkStack);
47
48 if (!$ofname) {
49 wfDebug("Profiling error: $functionname\n");
50 } else {
51 if ($functionname == 'close') {
52 $message = "Profile section ended by close(): {$ofname}";
53 $functionname = $ofname;
54 wfDebug( "$message\n" );
55 }
56 elseif ($ofname != $functionname) {
57 $message = "Profiling error: in({$ofname}), out($functionname)";
58 wfDebug( "$message\n" );
59 }
60 $entry =& $this->mCollated[$functionname];
61
62 $elapsedcpu = $this->getCpuTime() - $octime;
63 $elapsedreal = $this->getTime() - $ortime;
64
65 $entry['cpu'] += $elapsedcpu;
66 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
67 $entry['real'] += $elapsedreal;
68 $entry['real_sq'] += $elapsedreal*$elapsedreal;
69 $entry['count']++;
70
71 }
72 }
73
74 function getFunctionReport() {
75 /* Implement in output subclasses */
76 }
77
78 function getCpuTime($ru=null) {
79 if ($ru==null)
80 $ru=getrusage();
81 return ($ru['ru_utime.tv_sec']+$ru['ru_stime.tv_sec']+($ru['ru_utime.tv_usec']+$ru['ru_stime.tv_usec'])*1e-6);
82 }
83
84 function getTime($time=null) {
85 if ($time==null)
86 $time=microtime();
87 list($a,$b)=explode(" ",$time);
88 return (float)($a+$b);
89 }
90 }
91 ?>