* move $wgProfiler init to Setup.php
[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 profileIn($functionname) {
13 global $wgDebugFunctionEntry;
14 if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
15 wfDebug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
16 }
17 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getTime(), $this->getCpuTime());
18 }
19
20 function profileOut($functionname) {
21 $memory = memory_get_usage();
22
23 global $wgDebugFunctionEntry;
24
25 if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
26 wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
27 }
28
29 list($ofname,$ocount,$ortime,$octime) = array_pop($this->mWorkStack);
30
31 if (!$ofname) {
32 wfDebug("Profiling error: $functionname\n");
33 } else {
34 if ($functionname == 'close') {
35 $message = "Profile section ended by close(): {$ofname}";
36 wfDebug( "$message\n" );
37 }
38 elseif ($ofname != $functionname) {
39 $message = "Profiling error: in({$ofname}), out($functionname)";
40 wfDebug( "$message\n" );
41 }
42 $entry =& $this->mCollated[$functionname];
43
44 $elapsedcpu = $this->getCpuTime() - $octime;
45 $elapsedreal = $this->getTime() - $ortime;
46
47 $entry['cpu'] += $elapsedcpu;
48 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
49 $entry['real'] += $elapsedreal;
50 $entry['real_sq'] += $elapsedreal*$elapsedreal;
51 $entry['count']++;
52
53 }
54 }
55
56 function getFunctionReport() {
57 /* Implement in output subclasses */
58 }
59
60 function getCpuTime() {
61 $ru=getrusage();
62 return ($ru['ru_utime.tv_sec']+$ru['ru_stime.tv_sec']+($ru['ru_utime.tv_usec']+$ru['ru_stime.tv_usec'])*1e-6);
63 }
64
65 function getTime() {
66 list($a,$b)=explode(" ",microtime());
67 return (float)($a+$b);
68 }
69 }
70 ?>