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