From 38f267f402295b770d1ac14239fc576162e477ca Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Tue, 31 Dec 2013 15:33:07 -0800 Subject: [PATCH] Refactor ProfilerSimple 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 | 45 +++++++++++++++++++--------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/includes/profiler/ProfilerSimple.php b/includes/profiler/ProfilerSimple.php index 6f3b50c0f1..ee92c177ab 100644 --- a/includes/profiler/ProfilerSimple.php +++ b/includes/profiler/ProfilerSimple.php @@ -29,17 +29,44 @@ 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 ); } } -- 2.20.1