* (bug 5761) Project talk namespace broken in Xal, Os, Udm and Cv
[mediawiki.git] / includes / ProfilerSimple.php
blob930c5451c3e0a0faf0d9b49f831c0b9f48ee809d
1 <?php
2 /**
3 * Simple profiler base class
4 * @package MediaWiki
5 */
7 /**
8 * @todo document
9 * @package MediaWiki
11 require_once(dirname(__FILE__).'/Profiling.php');
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));
19 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
20 $elapsedreal = $this->getTime() - $this->getTime($wgRequestTime);
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']++;
31 function profileIn($functionname) {
32 global $wgDebugFunctionEntry;
33 if ($wgDebugFunctionEntry) {
34 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
36 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getTime(), $this->getCpuTime());
39 function profileOut($functionname) {
40 $memory = memory_get_usage();
42 global $wgDebugFunctionEntry;
44 if ($wgDebugFunctionEntry) {
45 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
48 list($ofname,$ocount,$ortime,$octime) = array_pop($this->mWorkStack);
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" );
58 elseif ($ofname != $functionname) {
59 $message = "Profiling error: in({$ofname}), out($functionname)";
60 $this->debug( "$message\n" );
62 $entry =& $this->mCollated[$functionname];
64 $elapsedcpu = $this->getCpuTime() - $octime;
65 $elapsedreal = $this->getTime() - $ortime;
67 $entry['cpu'] += $elapsedcpu;
68 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
69 $entry['real'] += $elapsedreal;
70 $entry['real_sq'] += $elapsedreal*$elapsedreal;
71 $entry['count']++;
76 function getFunctionReport() {
77 /* Implement in output subclasses */
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);
86 function getTime($time=null) {
87 if ($time==null)
88 $time=microtime();
89 list($a,$b)=explode(" ",$time);
90 return (float)($a+$b);
93 function debug( $s ) {
94 if (function_exists( 'wfDebug' ) ) {
95 wfDebug( $s );