Merge "resourceloader: Introduce metric for backend response timing"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 19 Oct 2017 17:17:12 +0000 (17:17 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 19 Oct 2017 17:17:12 +0000 (17:17 +0000)
includes/resourceloader/ResourceLoader.php
tests/phpunit/includes/resourceloader/ResourceLoaderTest.php

index c58bb00..b2e1c49 100644 (file)
@@ -728,6 +728,8 @@ class ResourceLoader implements LoggerAwareInterface {
                // See https://bugs.php.net/bug.php?id=36514
                ob_start();
 
+               $this->measureResponseTime( RequestContext::getMain()->getTiming() );
+
                // Find out which modules are missing and instantiate the others
                $modules = [];
                $missing = [];
@@ -828,6 +830,16 @@ class ResourceLoader implements LoggerAwareInterface {
                echo $response;
        }
 
+       protected function measureResponseTime( Timing $timing ) {
+               DeferredUpdates::addCallableUpdate( function () use ( $timing ) {
+                       $measure = $timing->measure( 'responseTime', 'requestStart', 'requestShutdown' );
+                       if ( $measure !== false ) {
+                               $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
+                               $stats->timing( 'resourceloader.responseTime', $measure['duration'] * 1000 );
+                       }
+               } );
+       }
+
        /**
         * Send main response headers to the client.
         *
index e9d022f..f45f8ae 100644 (file)
@@ -869,4 +869,41 @@ mw.example();
                        'Extra headers'
                );
        }
+
+       /**
+        * @covers ResourceLoader::respond
+        */
+       public function testRespond() {
+               $rl = $this->getMockBuilder( EmptyResourceLoader::class )
+                       ->setMethods( [
+                               'tryRespondNotModified',
+                               'sendResponseHeaders',
+                               'measureResponseTime',
+                       ] )
+                       ->getMock();
+               $context = $this->getResourceLoaderContext( [ 'modules' => '' ], $rl );
+
+               $rl->expects( $this->once() )->method( 'measureResponseTime' );
+               $this->expectOutputRegex( '/no modules were requested/' );
+
+               $rl->respond( $context );
+       }
+
+       /**
+        * @covers ResourceLoader::measureResponseTime
+        */
+       public function testMeasureResponseTime() {
+               $stats = $this->getMockBuilder( NullStatsdDataFactory::class )
+                       ->setMethods( [ 'timing' ] )->getMock();
+               $this->setService( 'StatsdDataFactory', $stats );
+
+               $stats->expects( $this->once() )->method( 'timing' )
+                       ->with( 'resourceloader.responseTime', $this->anything() );
+
+               $timing = new Timing();
+               $timing->mark( 'requestShutdown' );
+               $rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader );
+               $rl->measureResponseTime( $timing );
+               DeferredUpdates::doUpdates();
+       }
 }