00001 <?php
00007 if ( !class_exists( 'Profiler' ) ) {
00008 require_once(dirname(__FILE__).'/Profiler.php');
00009 }
00010
00016 class ProfilerSimple extends Profiler {
00017 var $mMinimumTime = 0;
00018 var $mProfileID = false;
00019
00020 function __construct() {
00021 global $wgRequestTime, $wgRUstart;
00022 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
00023 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
00024
00025 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
00026 $elapsedreal = microtime(true) - $wgRequestTime;
00027
00028 $entry =& $this->mCollated["-setup"];
00029 if (!is_array($entry)) {
00030 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
00031 $this->mCollated["-setup"] =& $entry;
00032 }
00033 $entry['cpu'] += $elapsedcpu;
00034 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
00035 $entry['real'] += $elapsedreal;
00036 $entry['real_sq'] += $elapsedreal*$elapsedreal;
00037 $entry['count']++;
00038 }
00039 }
00040
00041 function setMinimum( $min ) {
00042 $this->mMinimumTime = $min;
00043 }
00044
00045 function setProfileID( $id ) {
00046 $this->mProfileID = $id;
00047 }
00048
00049 function getProfileID() {
00050 if ( $this->mProfileID === false ) {
00051 return wfWikiID();
00052 } else {
00053 return $this->mProfileID;
00054 }
00055 }
00056
00057 function profileIn($functionname) {
00058 global $wgDebugFunctionEntry;
00059 if ($wgDebugFunctionEntry) {
00060 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
00061 }
00062 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
00063 }
00064
00065 function profileOut($functionname) {
00066 global $wgDebugFunctionEntry;
00067
00068 if ($wgDebugFunctionEntry) {
00069 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
00070 }
00071
00072 list($ofname, ,$ortime,$octime) = array_pop($this->mWorkStack);
00073
00074 if (!$ofname) {
00075 $this->debug("Profiling error: $functionname\n");
00076 } else {
00077 if ($functionname == 'close') {
00078 $message = "Profile section ended by close(): {$ofname}";
00079 $functionname = $ofname;
00080 $this->debug( "$message\n" );
00081 $this->mCollated[$message] = array(
00082 'real' => 0.0, 'count' => 1);
00083 }
00084 elseif ($ofname != $functionname) {
00085 $message = "Profiling error: in({$ofname}), out($functionname)";
00086 $this->debug( "$message\n" );
00087 $this->mCollated[$message] = array(
00088 'real' => 0.0, 'count' => 1);
00089 }
00090 $entry =& $this->mCollated[$functionname];
00091 $elapsedcpu = $this->getCpuTime() - $octime;
00092 $elapsedreal = microtime(true) - $ortime;
00093 if (!is_array($entry)) {
00094 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
00095 $this->mCollated[$functionname] =& $entry;
00096 }
00097 $entry['cpu'] += $elapsedcpu;
00098 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
00099 $entry['real'] += $elapsedreal;
00100 $entry['real_sq'] += $elapsedreal*$elapsedreal;
00101 $entry['count']++;
00102
00103 }
00104 }
00105
00106 function getFunctionReport() {
00107
00108 }
00109
00110 function getCpuTime($ru=null) {
00111 if ( function_exists( 'getrusage' ) ) {
00112 if ( $ru == null )
00113 $ru = getrusage();
00114 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
00115 $ru['ru_stime.tv_usec']) * 1e-6);
00116 } else {
00117 return 0;
00118 }
00119 }
00120
00121
00122 function getTime($time=null) {
00123 if ($time==null)
00124 return microtime(true);
00125 list($a,$b)=explode(" ",$time);
00126 return (float)($a+$b);
00127 }
00128 }