jenkins-bot has submitted this change and it was merged.

Change subject: Expose search metrics to Javascript for performance tooling
......................................................................


Expose search metrics to Javascript for performance tooling

Provide both the time we spent on Elastic as well as in Cirrus.
NavigationTiming already provides the total page times

Bug: 62768
Change-Id: Ib25fd0e2e9ecb2e9bdb1eb564762c175659b9ff0
---
M includes/CirrusSearch.php
M includes/ElasticsearchIntermediary.php
M includes/Hooks.php
3 files changed, 49 insertions(+), 2 deletions(-)

Approvals:
  Manybubbles: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/CirrusSearch.php b/includes/CirrusSearch.php
index d05e247..ac50eb8 100644
--- a/includes/CirrusSearch.php
+++ b/includes/CirrusSearch.php
@@ -33,6 +33,11 @@
        private $lastNamespacePrefix;
 
        /**
+        * @var array metrics about the last thing we searched
+        */
+       private $lastSearchMetrics;
+
+       /**
         * Override supports to shut off updates to Cirrus via the SearchEngine 
infrastructure.  Page
         * updates and additions are chained on the end of the links update 
job.  Deletes are noticed
         * via the ArticleDeleteComplete hook.
@@ -91,6 +96,8 @@
                        $status = $searcher->searchText( $term, 
$this->showRedirects, $this->showSuggestion );
                }
 
+               $this->lastSearchMetrics = $searcher->getSearchMetrics();
+
                // For historical reasons all callers of searchText interpret 
any Status return as an error
                // so we must unwrap all OK statuses.  Note that $status can be 
"good" and still contain null
                // since that is interpreted as no results.
@@ -132,7 +139,19 @@
                return $parsed;
        }
 
+       /**
+        * Get the sort of sorts we allow
+        * @return array
+        */
        public function getValidSorts() {
                return array( 'relevance', 'title_asc', 'title_desc' );
        }
+
+       /**
+        * Get the metrics for the last search we performed. Null if we haven't 
done any.
+        * @return array
+        */
+       public function getLastSearchMetrics() {
+               return $this->lastSearchMetrics;
+       }
 }
diff --git a/includes/ElasticsearchIntermediary.php 
b/includes/ElasticsearchIntermediary.php
index a753d97..47dfcec 100644
--- a/includes/ElasticsearchIntermediary.php
+++ b/includes/ElasticsearchIntermediary.php
@@ -43,6 +43,11 @@
        private $slowMillis;
 
        /**
+        * @var array Metrics about a completed search
+        */
+       private $searchMetrics = array();
+
+       /**
         * Constructor.
         *
         * @param User|null $user user for which this search is being 
performed.  Attached to slow request logs.  Note that
@@ -113,6 +118,14 @@
        }
 
        /**
+        * Get the search metrics we have
+        * @return array
+        */
+       public function getSearchMetrics() {
+               return $this->searchMetrics;
+       }
+
+       /**
         * Does this status represent an Elasticsearch parse error?
         * @param $status Status to check
         * @return boolean is this a parse error?
@@ -138,8 +151,12 @@
                // No need to check description because it must be set by 
$this->start.
 
                // Build the log message
-               $took = round( ( microtime( true ) - $this->requestStart ) * 
1000 );
+               $endTime = microtime( true );
+               $took = round( ( $endTime - $this->requestStart ) * 1000 );
                $logMessage = "$this->description took $took millis";
+
+               $this->searchMetrics['wgCirrusStartTime'] = $this->requestStart;
+               $this->searchMetrics['wgCirrusEndTime'] = $endTime;
 
                // Extract the amount of time Elasticsearch reported the last 
request took if possible.
                $result = ElasticaConnection::getClient()->getLastResponse();
@@ -148,6 +165,7 @@
                        if ( isset( $data[ 'took' ] ) ) {
                                $elasticTook = $data[ 'took' ];
                                $logMessage .= " and $elasticTook Elasticsearch 
millis";
+                               $this->searchMetrics['wgCirrusElasticTime'] = 
$elasticTook;
                        }
                }
 
diff --git a/includes/Hooks.php b/includes/Hooks.php
index 058b1a7..5d363dd 100644
--- a/includes/Hooks.php
+++ b/includes/Hooks.php
@@ -1,6 +1,7 @@
 <?php
 
 namespace CirrusSearch;
+use \CirrusSearch;
 use \BetaFeatures;
 use \JobQueueGroup;
 use \Title;
@@ -145,7 +146,7 @@
        }
 
        /**
-        * Called to prepend text before search results
+        * Called to prepend text before search results and inject metrics
         * @param SpecialSearch $specialSearch The SpecialPage object for 
Special:Search
         * @param OutputPage $out The output page object
         * @param string $term The term being searched for
@@ -153,11 +154,20 @@
         */
        public static function specialSearchResultsPrependHook( $specialSearch, 
$out, $term ) {
                global $wgCirrusSearchShowNowUsing;
+
+               // Prepend our message if needed
                if ( $wgCirrusSearchShowNowUsing ) {
                        $out->addHtml( Xml::openElement( 'div', array( 'class' 
=> 'cirrussearch-now-using' ) ) .
                                $specialSearch->msg( 'cirrussearch-now-using' 
)->parse() .
                                Xml::closeElement( 'div' ) );
                }
+
+               // Embed metrics if this was a Cirrus page
+               $engine = $specialSearch->getSearchEngine();
+               if ( $engine instanceof CirrusSearch ) {
+                       $out->addJsConfigVars( $engine->getLastSearchMetrics() 
);
+               }
+
                return true;
        }
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib25fd0e2e9ecb2e9bdb1eb564762c175659b9ff0
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Chad <[email protected]>
Gerrit-Reviewer: Manybubbles <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to