Aaron Schulz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/175140

Change subject: Adding basic profiler sampling support and restored the 
--profiler script option
......................................................................

Adding basic profiler sampling support and restored the --profiler script option

* Also enforce that the profiler is normally off in CLI mode

Change-Id: I35faedff818af2ad459b544c9ad50e77b54b378e
---
M StartProfiler.sample
M includes/profiler/Profiler.php
M maintenance/Maintenance.php
3 files changed, 48 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/40/175140/1

diff --git a/StartProfiler.sample b/StartProfiler.sample
index b72d5d5..51c24fb 100644
--- a/StartProfiler.sample
+++ b/StartProfiler.sample
@@ -22,8 +22,12 @@
  * maintenance/archives/patch-profiling.sql to your database.
  *
  * For a rudimentary sampling profiler:
- *   if ( !mt_rand( 0, 100 ) ) {
- *       $wgProfiler['class'] = 'ProfilerStandard';
- *       $wgProfiler['output'] = array( 'db' );
- *   }
+ *   $wgProfiler['class'] = 'ProfilerStandard';
+ *   $wgProfiler['output'] = array( 'db' );
+ *   $wgProfiler['sampling'] = 50; // one every 50 requests
+ * This will use ProfilerStub for non-sampled cases.
+ *
+ * For performance, the profiler is always disable for CLI scripts
+ * as they could be long running and the data would accumulate. Use
+ * the --profiler parameter of maintenance script to override this.
  */
diff --git a/includes/profiler/Profiler.php b/includes/profiler/Profiler.php
index f2bdc84..2b3b616 100644
--- a/includes/profiler/Profiler.php
+++ b/includes/profiler/Profiler.php
@@ -72,10 +72,10 @@
                if ( self::$__instance === null ) {
                        global $wgProfiler;
                        if ( is_array( $wgProfiler ) ) {
-                               if ( !isset( $wgProfiler['class'] ) ) {
+                               $class = isset( $wgProfiler['class'] ) ? 
$wgProfiler['class'] : 'ProfilerStub';
+                               $factor = isset( $wgProfiler['sampling'] ) ? 
$wgProfiler['sampling'] : 1;
+                               if ( PHP_SAPI === 'cli' || mt_rand( 0, $factor 
- 1 ) != 0 ) {
                                        $class = 'ProfilerStub';
-                               } else {
-                                       $class = $wgProfiler['class'];
                                }
                                self::$__instance = new $class( $wgProfiler );
                        } else {
@@ -83,6 +83,21 @@
                        }
                }
                return self::$__instance;
+       }
+
+       /**
+        * Replace the current profiler with $profiler if no non-stub profiler 
is set
+        *
+        * @param Profiler $profiler
+        * @throws MWException
+        * @since 1.25
+        */
+       final public static function replaceStubInstance( Profiler $profiler ) {
+               if ( self::$__instance && !( self::$__instance instanceof 
ProfilerStub ) ) {
+                       throw new MWException( 'Could not replace non-stub 
profiler instance.' );
+               } else {
+                       self::$__instance = $profiler;
+               }
        }
 
        /**
@@ -144,8 +159,7 @@
         * @since 1.25
         */
        public function logData() {
-               $output = isset( $this->params['output'] ) ?
-                       $this->params['output'] : null;
+               $output = isset( $this->params['output'] ) ? 
$this->params['output'] : null;
 
                if ( !$output || $this->isStub() ) {
                        // return early when no output classes defined or we're 
a stub
diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php
index 3f8f6e8..36214a2 100644
--- a/maintenance/Maintenance.php
+++ b/maintenance/Maintenance.php
@@ -446,6 +446,7 @@
                $this->addOption( 'server', "The protocol and server name to 
use in URLs, e.g. " .
                        "http://en.wikipedia.org. This is sometimes necessary 
because " .
                        "server name detection may fail in command line 
scripts.", false, true );
+               $this->addOption( 'profiler', 'Profiler output format (usually 
"text")', false, true );
 
                # Save generic options to display them separately in help
                $this->mGenericParameters = $this->mParams;
@@ -593,6 +594,23 @@
                }
                if ( $limit != 'default' ) {
                        ini_set( 'memory_limit', $limit );
+               }
+       }
+
+       /**
+        * Set the profiler for debugging (assuming $wgProfiler is set)
+        */
+       protected function setDebugProfiler() {
+               global $wgProfiler;
+
+               $output = $this->getOption( 'profiler' );
+               if ( $output && is_array( $wgProfiler ) ) {
+                       $class = $wgProfiler['class'];
+                       $profiler = new $class(
+                               array( 'sampling' => 1, 'output' => $output ) + 
$wgProfiler
+                       );
+                       $profiler->setTemplated( true );
+                       Profiler::replaceStubInstance( $profiler );
                }
        }
 
@@ -919,6 +937,9 @@
                        LBFactory::destroyInstance();
                }
 
+               // Per-script profiling; useful for debugging
+               $this->setDebugProfiler();
+
                $this->afterFinalSetup();
 
                $wgShowSQLErrors = true;

-- 
To view, visit https://gerrit.wikimedia.org/r/175140
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I35faedff818af2ad459b544c9ad50e77b54b378e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to