8 * Simple profiler base class.
9 * @todo document methods (?)
12 class ProfilerSimple
extends Profiler
{
13 var $mMinimumTime = 0;
15 var $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
18 protected function addInitialStack() {
19 global $wgRequestTime, $wgRUstart;
21 $this->errorEntry
= $this->zeroEntry
;
22 $this->errorEntry
['count'] = 1;
24 if ( !empty( $wgRequestTime ) && !empty( $wgRUstart ) ) {
25 $initialCpu = $this->getCpuTime( $wgRUstart );
26 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime, $initialCpu );
27 $this->mWorkStack
[] = array( '-setup', 1, $wgRequestTime, $initialCpu );
29 $this->profileOut( '-setup' );
31 $this->profileIn( '-total' );
35 function setMinimum( $min ) {
36 $this->mMinimumTime
= $min;
39 function profileIn($functionname) {
40 global $wgDebugFunctionEntry;
41 if ($wgDebugFunctionEntry) {
42 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
44 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
47 function profileOut($functionname) {
48 global $wgDebugFunctionEntry;
50 if ($wgDebugFunctionEntry) {
51 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
54 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
57 $this->debug("Profiling error: $functionname\n");
59 if ($functionname == 'close') {
60 $message = "Profile section ended by close(): {$ofname}";
61 $functionname = $ofname;
62 $this->debug( "$message\n" );
63 $this->mCollated
[$message] = $this->errorEntry
;
65 elseif ($ofname != $functionname) {
66 $message = "Profiling error: in({$ofname}), out($functionname)";
67 $this->debug( "$message\n" );
68 $this->mCollated
[$message] = $this->errorEntry
;
70 $entry =& $this->mCollated
[$functionname];
71 $elapsedcpu = $this->getCpuTime() - $octime;
72 $elapsedreal = microtime(true) - $ortime;
73 if (!is_array($entry)) {
74 $entry = $this->zeroEntry
;
75 $this->mCollated
[$functionname] =& $entry;
77 $entry['cpu'] +
= $elapsedcpu;
78 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
79 $entry['real'] +
= $elapsedreal;
80 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
86 public function getFunctionReport() {
87 /* Implement in output subclasses */
91 public function logData() {
92 /* Implement in subclasses */
95 function getCpuTime($ru=null) {
96 if ( function_exists( 'getrusage' ) ) {
100 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
101 $ru['ru_stime.tv_usec']) * 1e-6);