Visualize profile output in debug toolbar
[lhc/web/wiklou.git] / includes / profiler / Profiler.php
index a26ef68..f992d83 100644 (file)
@@ -96,7 +96,7 @@ class ProfileSection {
  */
 class Profiler {
        protected $mStack = array(), $mWorkStack = array(), $mCollated = array(),
-               $mCalls = array(), $mTotals = array();
+               $mCalls = array(), $mTotals = array(), $mPeriods = array();
        protected $mTimeMetric = 'wall';
        protected $mProfileID = false, $mCollateDone = false, $mTemplated = false;
 
@@ -558,6 +558,7 @@ class Profiler {
                                $this->mMin[$fname] = 1 << 24;
                                $this->mMax[$fname] = 0;
                                $this->mOverhead[$fname] = 0;
+                               $this->mPeriods[$fname] = array();
                        }
 
                        $this->mCollated[$fname] += $elapsed;
@@ -566,6 +567,7 @@ class Profiler {
                        $this->mMin[$fname] = min( $this->mMin[$fname], $elapsed );
                        $this->mMax[$fname] = max( $this->mMax[$fname], $elapsed );
                        $this->mOverhead[$fname] += $subcalls;
+                       $this->mPeriods[$fname][] = compact( 'start', 'end', 'memory', 'subcalls' );
                }
 
                $this->mCalls['-overhead-total'] = $profileCount;
@@ -610,6 +612,37 @@ class Profiler {
                return $prof;
        }
 
+       /**
+        * @return array
+        */
+       public function getRawData() {
+               $this->collateData();
+
+               $profile = array();
+               $total = isset( $this->mCollated['-total'] ) ? $this->mCollated['-total'] : 0;
+               foreach ( $this->mCollated as $fname => $elapsed ) {
+                       $periods = array();
+                       foreach ( $this->mPeriods[$fname] as $period ) {
+                               $period['start'] *= 1000;
+                               $period['end'] *= 1000;
+                               $periods[] = $period;
+                       }
+                       $profile[] = array(
+                               'name' => $fname,
+                               'calls' => $this->mCalls[$fname],
+                               'elapsed' => $elapsed * 1000,
+                               'percent' => $total ? 100. * $elapsed / $total : 0,
+                               'memory' => $this->mMemory[$fname],
+                               'min' => $this->mMin[$fname] * 1000,
+                               'max' => $this->mMax[$fname] * 1000,
+                               'overhead' => $this->mOverhead[$fname],
+                               'periods' => $periods,
+                       );
+               }
+
+               return $profile;
+       }
+
        /**
         * Dummy calls to wfProfileIn/wfProfileOut to calculate its overhead
         */