Refactor ProfilerSimple
authorOri Livneh <ori@wikimedia.org>
Tue, 31 Dec 2013 23:33:07 +0000 (15:33 -0800)
committerOri Livneh <ori@wikimedia.org>
Tue, 31 Dec 2013 23:55:03 +0000 (15:55 -0800)
This patch refactors ProfilerSimple, moving the code that creates a new
profiling entry and the code that updates an existing entry to discrete
methods. This allows subclasses to supply a different implementation. It will
allow me to introduce a profiler class that uses RunningStat (introduced in
Ifedda276d) without breaking existing APIs and without having to duplicate lots
of code.

Change-Id: Ida7d7d0c1e2a98618b51246861e6af8ec3eb6320

includes/profiler/ProfilerSimple.php

index 6f3b50c..ee92c17 100644 (file)
 class ProfilerSimple extends Profiler {
        var $mMinimumTime = 0;
 
-       var $zeroEntry = array( 'cpu' => 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0 );
        var $errorEntry;
 
+       public function getZeroEntry() {
+               return array(
+                       'cpu'     => 0.0,
+                       'cpu_sq'  => 0.0,
+                       'real'    => 0.0,
+                       'real_sq' => 0.0,
+                       'count'   => 0
+               );
+       }
+
+       public function getErrorEntry() {
+               $entry = $this->getZeroEntry();
+               $entry['count'] = 1;
+               return $entry;
+       }
+
+       public function updateEntry( $name, $elapsedCpu, $elapsedReal ) {
+               $entry =& $this->mCollated[$name];
+               if ( !is_array( $entry ) ) {
+                       $entry = $this->getZeroEntry();
+                       $this->mCollated[$name] =& $entry;
+               }
+               $entry['cpu'] += $elapsedCpu;
+               $entry['cpu_sq'] += $elapsedCpu * $elapsedCpu;
+               $entry['real'] += $elapsedReal;
+               $entry['real_sq'] += $elapsedReal * $elapsedReal;
+               $entry['count']++;
+       }
+
        public function isPersistent() {
                /* Implement in output subclasses */
                return false;
        }
 
        protected function addInitialStack() {
-               $this->errorEntry = $this->zeroEntry;
-               $this->errorEntry['count'] = 1;
+               $this->errorEntry = $this->getErrorEntry();
 
                $initialTime = $this->getInitialTime();
                $initialCpu = $this->getInitialTime( 'cpu' );
@@ -88,19 +115,9 @@ class ProfilerSimple extends Profiler {
                                $this->debug( "$message\n" );
                                $this->mCollated[$message] = $this->errorEntry;
                        }
-                       $entry =& $this->mCollated[$functionname];
                        $elapsedcpu = $this->getTime( 'cpu' ) - $octime;
                        $elapsedreal = $this->getTime() - $ortime;
-                       if ( !is_array( $entry ) ) {
-                               $entry = $this->zeroEntry;
-                               $this->mCollated[$functionname] =& $entry;
-                       }
-                       $entry['cpu'] += $elapsedcpu;
-                       $entry['cpu_sq'] += $elapsedcpu * $elapsedcpu;
-                       $entry['real'] += $elapsedreal;
-                       $entry['real_sq'] += $elapsedreal * $elapsedreal;
-                       $entry['count']++;
-
+                       $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal );
                        $this->updateTrxProfiling( $functionname, $elapsedreal );
                }
        }