r81281 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81280‎ | r81281 | r81282 >
Date:23:15, 31 January 2011
Author:dale
Status:deferred
Tags:
Comment:
added a "poorMansSquidProxy" in front of resource loader request per second goes from 3-4 to 1000 or so for cached hits
Modified paths:
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/DefaultSettings.php (modified) (history)
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedAutoLoader.php (modified) (history)
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedMediaWikiGlobalFunctions.php (modified) (history)
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedMediaWikiStubs.php (modified) (history)
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedPoorManSquidProxy.php (added) (history)
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedResourceLoaderContext.php (modified) (history)
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/resourceloader/ResourceLoader.php (modified) (history)
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/load.php (modified) (history)
  • /branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/modules/EmbedPlayer/tests/Player_Sources.html (modified) (history)

Diff [purge]

Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/load.php
@@ -28,8 +28,24 @@
2929
3030 require ( dirname( __FILE__ ) . '/includes/MwEmbedWebStartSetup.php' );
3131
 32+// A script we put in front of mwEmbedResourceLoader to quickly serve files from cache,
 33+// in the absence of a reverse proxy in front of the resource loader.
 34+// if you ~do~ reverse proxy resource loader you can set
 35+// $mwUsePoorManSquidProxy = false;
 36+
 37+MwEmbedPoorManSquidProxy::checkCacheRespond();
 38+
3239 // Respond to resource loading request
3340 $resourceLoader = new MwEmbedResourceLoader();
34 -$resourceLoader->respond( new MwEmbedResourceLoaderContext( $resourceLoader, $wgRequest ) );
 41+$output = $resourceLoader->respond( new MwEmbedResourceLoaderContext( $resourceLoader, $wgRequest ) );
3542
 43+// Save the current response to the cache
 44+MwEmbedPoorManSquidProxy::saveCacheRespond($output);
3645
 46+/*
 47+ * For profile info:
 48+*/
 49+/*
 50+arsort( $wgSimpleProfile );
 51+print_r( $wgSimpleProfile );
 52+*/
Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedMediaWikiGlobalFunctions.php
@@ -279,9 +279,11 @@
280280 global $IP;
281281 $filePath = mwGetFilePathFromKey( $key );
282282 $path = dirname( $filePath );
283 - $ok = mkdir( $path, 0777, true ); // PHP5 <3
284 - if( !$ok ){
285 - return false;
 283+ if( !is_dir($path ) ){
 284+ $ok = mkdir( $path, 0777, true ); // PHP5 <3
 285+ if( !$ok ){
 286+ return false;
 287+ }
286288 }
287289 return @file_put_contents( $filePath, serialize( $data ) );
288290 }
Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedAutoLoader.php
@@ -27,6 +27,7 @@
2828
2929 # MwEmbed files ( that get autoloaded ):
3030 'MwEmbedResourceLoaderContext' => 'includes/MwEmbedResourceLoaderContext.php',
 31+ 'MwEmbedPoorManSquidProxy' => 'includes/MwEmbedPoorManSquidProxy.php',
3132 'MwEmbedResourceLoader' => 'includes/MwEmbedResourceLoader.php',
3233 'MwEmbedResourceLoaderFileModule' => 'includes/MwEmbedResourceLoaderFileModule.php',
3334 'MwEmbedResourceLoaderStartUpModule' => 'includes/MwEmbedResourceLoaderStartUpModule.php',
Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedPoorManSquidProxy.php
@@ -0,0 +1,92 @@
 2+<?php
 3+
 4+class MwEmbedPoorManSquidProxy {
 5+ public static $hash = null;
 6+ public static $debug = null;
 7+
 8+ public static function checkCacheRespond( ){
 9+ global $wgDefaultSkin, $wgResourceLoaderMaxage, $wgResourceLoaderDebug, $wgRequest;
 10+ $request = $wgRequest;
 11+ self::$debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug ) ;
 12+ // Never respond in debug mode getDebug
 13+ if( self::$debug ){
 14+ return ;
 15+ }
 16+ $modules = $request->getVal( 'modules' );
 17+ $modules = $modules ? explode( '|', $modules ) : array();
 18+ if( count( $modules ) == 0 ){
 19+ $modules[] = 'startup';
 20+ }
 21+ $skin = $request->getVal( 'skin' );
 22+ if ( !$skin ) {
 23+ $skin = $wgDefaultSkin;
 24+ }
 25+ $only = $request->getVal( 'only' );
 26+ $version = $request->getVal( 'version' );
 27+
 28+ // Set the version:
 29+ if( !$version ){
 30+ $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
 31+ } else {
 32+ $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
 33+ }
 34+ $modulesString = implode( '', $modules);
 35+ self::$hash = md5( implode( '', array( $version, $skin, $only, $modulesString ) ) );
 36+
 37+ /**
 38+ * Handle the response
 39+ */
 40+
 41+ // Check if we have a cached file:
 42+ if( !is_file( mwGetFilePathFromKey( self::$hash ) ) ){
 43+ return ;
 44+ }
 45+
 46+ // Check file modified time:
 47+ $fileTime = wfTimestamp( TS_UNIX, filemtime( mwGetFilePathFromKey( self::$hash ) ) );
 48+ if( wfTimestamp( TS_UNIX, time() ) - $fileTime > $smaxage){
 49+ // Run the normal resource loader
 50+ return ;
 51+ }
 52+
 53+ // Check if we can send a 304 to the client:
 54+ // 'If-Modified-Since' and 'version' we can assume there is no modification:
 55+ $ims = $request->getHeader( 'If-Modified-Since' );
 56+ if ( $ims !== false && $version ) {
 57+ for ( $i = 0; $i < ob_get_level(); $i++ ) {
 58+ ob_end_clean();
 59+ }
 60+ header( 'HTTP/1.0 304 Not Modified' );
 61+ header( 'Status: 304 Not Modified' );
 62+ wfProfileOut( __METHOD__ );
 63+ return;
 64+ }
 65+
 66+ // Send the same headers as ResourceLoader:
 67+ if ( $only === 'styles' ) {
 68+ header( 'Content-Type: text/css' );
 69+ } else {
 70+ header( 'Content-Type: text/javascript' );
 71+ }
 72+ header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $fileTime ) );
 73+ header( "Cache-Control: public, max-age=$smaxage, s-maxage=$smaxage" );
 74+ header( 'Expires: ' . wfTimestamp( TS_RFC2822, $smaxage + time() ) );
 75+
 76+ echo mweGetFromFileCache( self::$hash );
 77+ // exit ( don't continue resource loader handling )
 78+ exit(1);
 79+ }
 80+ public static function saveCacheRespond(& $output){
 81+ global $mwUsePoorManSquidProxy;
 82+ // Don't cache debug output and only cache if squid proxy is enabled:
 83+ if( !self::$debug && $mwUsePoorManSquidProxy ){
 84+ mweSaveFileToCache( self::$hash, $output);
 85+ }
 86+ // Headers already set in Resource loader )
 87+ echo $output;
 88+ }
 89+}
 90+
 91+
 92+
 93+?>
\ No newline at end of file
Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedResourceLoaderContext.php
@@ -5,7 +5,6 @@
66 * of a specific loader request
77 */
88 class MwEmbedResourceLoaderContext extends ResourceLoaderContext{
9 -
109 public function getDirection() {
1110 if ( $this->direction === null ) {
1211 $this->direction = $this->request->getVal( 'dir' );
@@ -13,5 +12,5 @@
1413 return $this->direction;
1514 }
1615 }
17 -
 16+
1817 ?>
\ No newline at end of file
Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/DefaultSettings.php
@@ -22,6 +22,9 @@
2323 // Default Load Script path
2424 $wgLoadScript = $wgServer . $wgScriptPath . 'load.php';
2525
 26+// If we should use simple php file cache infront of resource loader
 27+// helps performance in situations where you don't reverse proxy the resource loader.
 28+$mwUsePoorManSquidProxy = true;
2629
2730 // The list of enabled modules
2831 $wgMwEmbedEnabledModules = array();
@@ -60,8 +63,26 @@
6164 /* Default skin can be any jquery based skin */
6265 $wgDefaultSkin = 'kaltura-dark';
6366
 67+// If the resource loader is in 'debug mode'
 68+$wgResourceLoaderDebug = false;
6469
6570
 71+/**
 72+ * Maximum time in seconds to cache resources served by the resource loader
 73+ */
 74+$wgResourceLoaderMaxage = array(
 75+ 'versioned' => array(
 76+ // Squid/Varnish but also any other public proxy cache between the client and MediaWiki
 77+ 'server' => 30 * 24 * 60 * 60, // 30 days
 78+ // On the client side (e.g. in the browser cache).
 79+ 'client' => 30 * 24 * 60 * 60, // 30 days
 80+ ),
 81+ 'unversioned' => array(
 82+ 'server' => 5 * 60, // 5 minutes
 83+ 'client' => 5 * 60, // 5 minutes
 84+ ),
 85+);
 86+
6687 /*********************************************************
6788 * Default Kaltura Configuration:
6889 * TODO move kaltura configuration to KalturaSupport module ( part of ResourceLoader update )
Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/MwEmbedMediaWikiStubs.php
@@ -7,10 +7,15 @@
88 /**
99 * global functions
1010 */
 11+$wgSimpleProfile = array();
1112 function wfProfileIn($na){
 13+ global $wgSimpleProfile;
 14+ $wgSimpleProfile[$na] = microtime();
1215 return ;
1316 }
1417 function wfProfileOut($na){
 18+ global $wgSimpleProfile;
 19+ $wgSimpleProfile[$na] = microtime() - $wgSimpleProfile[$na];
1520 return ;
1621 }
1722 function wfDebug( $text, $logonly = false ) {
@@ -246,7 +251,14 @@
247252 * @return Boolean
248253 */
249254 public function getFuzzyBool( $name, $default = false ) {
250 - return ( isset( $this->data[ $name ] ) )? ( $this->data[ $name ] ) : $default;
 255+ if( isset( $this->data[ $name ] ) ){
 256+ if( $this->data[ $name ] === 'false' ){
 257+ return false;
 258+ }
 259+ return true;
 260+ } else {
 261+ return $default;
 262+ }
251263 }
252264
253265 /**
Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/includes/resourceloader/ResourceLoader.php
@@ -411,7 +411,7 @@
412412
413413 // Remove the output buffer and output the response
414414 ob_end_clean();
415 - echo $response;
 415+ return $response;
416416
417417 wfProfileOut( __METHOD__ );
418418 }
Index: branches/MwEmbedStandAloneRL1_17/MwEmbedStandAlone/modules/EmbedPlayer/tests/Player_Sources.html
@@ -3,7 +3,7 @@
44 <head>
55 <title>Player sources</title>
66
7 -<script type="text/javascript" src="../../../load.php?modules=startup&only=scripts&debug=true"></script>
 7+<script type="text/javascript" src="../../../load.php?modules=startup&only=scripts"></script>
88 </head>
99 <body>
1010

Status & tagging log