MediaWiki  REL1_19
ProfilerSimple.php
Go to the documentation of this file.
00001 <?php
00012 class ProfilerSimple extends Profiler {
00013         var $mMinimumTime = 0;
00014 
00015         var $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
00016         var $errorEntry;
00017 
00018         function __construct( $params ) {
00019                 global $wgRequestTime, $wgRUstart;
00020                 parent::__construct( $params );
00021 
00022                 $this->errorEntry = $this->zeroEntry;
00023                 $this->errorEntry['count'] = 1;
00024 
00025                 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
00026                         # Remove the -total entry from parent::__construct
00027                         $this->mWorkStack = array();
00028 
00029                         $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
00030 
00031                         $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
00032                         $elapsedreal = microtime(true) - $wgRequestTime;
00033 
00034                         $entry =& $this->mCollated["-setup"];
00035                         if (!is_array($entry)) {
00036                                 $entry = $this->zeroEntry;
00037                                 $this->mCollated["-setup"] =& $entry;
00038                         }
00039                         $entry['cpu'] += $elapsedcpu;
00040                         $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
00041                         $entry['real'] += $elapsedreal;
00042                         $entry['real_sq'] += $elapsedreal*$elapsedreal;
00043                         $entry['count']++;
00044                 }
00045         }
00046 
00047         function setMinimum( $min ) {
00048                 $this->mMinimumTime = $min;
00049         }
00050 
00051         function profileIn($functionname) {
00052                 global $wgDebugFunctionEntry;
00053                 if ($wgDebugFunctionEntry) {
00054                         $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
00055                 }
00056                 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
00057         }
00058 
00059         function profileOut($functionname) {
00060                 global $wgDebugFunctionEntry;
00061 
00062                 if ($wgDebugFunctionEntry) {
00063                         $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
00064                 }
00065 
00066                 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
00067 
00068                 if (!$ofname) {
00069                         $this->debug("Profiling error: $functionname\n");
00070                 } else {
00071                         if ($functionname == 'close') {
00072                                 $message = "Profile section ended by close(): {$ofname}";
00073                                 $functionname = $ofname;
00074                                 $this->debug( "$message\n" );
00075                                 $this->mCollated[$message] = $this->errorEntry;
00076                         }
00077                         elseif ($ofname != $functionname) {
00078                                 $message = "Profiling error: in({$ofname}), out($functionname)";
00079                                 $this->debug( "$message\n" );
00080                                 $this->mCollated[$message] = $this->errorEntry;
00081                         }
00082                         $entry =& $this->mCollated[$functionname];
00083                         $elapsedcpu = $this->getCpuTime() - $octime;
00084                         $elapsedreal = microtime(true) - $ortime;
00085                         if (!is_array($entry)) {
00086                                 $entry = $this->zeroEntry;
00087                                 $this->mCollated[$functionname] =& $entry;
00088                         }
00089                         $entry['cpu'] += $elapsedcpu;
00090                         $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
00091                         $entry['real'] += $elapsedreal;
00092                         $entry['real_sq'] += $elapsedreal*$elapsedreal;
00093                         $entry['count']++;
00094 
00095                 }
00096         }
00097 
00098         public function getFunctionReport() {
00099                 /* Implement in output subclasses */
00100                 return '';
00101         }
00102 
00103         public function logData() {
00104                 /* Implement in subclasses */
00105         }
00106 
00107         function getCpuTime($ru=null) {
00108                 if ( function_exists( 'getrusage' ) ) {
00109                         if ( $ru == null ) {
00110                                 $ru = getrusage();
00111                         }
00112                         return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
00113                                 $ru['ru_stime.tv_usec']) * 1e-6);
00114                 } else {
00115                         return 0;
00116                 }
00117         }
00118 }