Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / profiler / SectionProfiler.php
index 275e986..63940bc 100644 (file)
@@ -46,6 +46,8 @@ class SectionProfiler {
        protected $collateOnly = true;
        /** @var array Cache of a standard broken collation entry */
        protected $errorEntry;
+       /** @var callable Cache of a profile out callback */
+       protected $profileOutCallback;
 
        /**
         * @param array $params
@@ -53,6 +55,9 @@ class SectionProfiler {
        public function __construct( array $params = array() ) {
                $this->errorEntry = $this->getErrorEntry();
                $this->collateOnly = empty( $params['trace'] );
+               $this->profileOutCallback = function ( $profiler, $section ) {
+                       $profiler->profileOutInternal( $section );
+               };
        }
 
        /**
@@ -62,10 +67,7 @@ class SectionProfiler {
        public function scopedProfileIn( $section ) {
                $this->profileInInternal( $section );
 
-               $that = $this;
-               return new ScopedCallback( function () use ( $that, $section ) {
-                       $that->profileOutInternal( $section );
-               } );
+               return new SectionProfileCallback( $this, $section );
        }
 
        /**
@@ -87,9 +89,9 @@ class SectionProfiler {
         * @return array List of method entries arrays, each having:
         *   - name    : method name
         *   - calls   : the number of invoking calls
-        *   - real    : real time ellapsed (ms)
+        *   - real    : real time elapsed (ms)
         *   - %real   : percent real time
-        *   - cpu     : real time ellapsed (ms)
+        *   - cpu     : real time elapsed (ms)
         *   - %cpu    : percent real time
         *   - memory  : memory used (bytes)
         *   - %memory : percent memory used
@@ -275,6 +277,7 @@ class SectionProfiler {
        /**
         * Returns a tree of function calls with their real times
         * @return string
+        * @throws Exception
         */
        public function getCallTreeReport() {
                if ( $this->collateOnly ) {
@@ -448,15 +451,14 @@ class SectionProfiler {
        }
 
        /**
-        * Get the initial time of the request, based either on $wgRequestTime or
-        * $wgRUstart. Will return null if not able to find data.
+        * Get the initial time of the request, based on getrusage()
         *
         * @param string|bool $metric Metric to use, with the following possibilities:
         *   - user: User CPU time (without system calls)
         *   - cpu: Total CPU time (user and system calls)
         *   - wall (or any other string): elapsed time
         *   - false (default): will fall back to default metric
-        * @return float|null
+        * @return float
         */
        protected function getTime( $metric = 'wall' ) {
                if ( $metric === 'cpu' || $metric === 'user' ) {
@@ -499,3 +501,29 @@ class SectionProfiler {
                }
        }
 }
+
+/**
+ * Subclass ScopedCallback to avoid call_user_func_array(), which is slow
+ *
+ * This class should not be used outside of SectionProfiler
+ */
+class SectionProfileCallback extends ScopedCallback {
+       /** @var SectionProfiler */
+       protected $profiler;
+       /** @var string */
+       protected $section;
+
+       /**
+        * @param SectionProfiler $profiler
+        * @param string $section
+        */
+       public function __construct( SectionProfiler $profiler, $section ) {
+               parent::__construct( null );
+               $this->profiler = $profiler;
+               $this->section = $section;
+       }
+
+       function __destruct() {
+               $this->profiler->profileOutInternal( $this->section );
+       }
+}