MediaWiki  REL1_22
mctest.php
Go to the documentation of this file.
00001 <?php
00025 require_once __DIR__ . '/Maintenance.php';
00026 
00033 class mcTest extends Maintenance {
00034     public function __construct() {
00035         parent::__construct();
00036         $this->mDescription = "Makes several 'set', 'incr' and 'get' requests on every"
00037             . " memcached server and shows a report";
00038         $this->addOption( 'i', 'Number of iterations', false, true );
00039         $this->addOption( 'cache', 'Use servers from this $wgObjectCaches store', false, true );
00040         $this->addArg( 'server[:port]', 'Memcached server to test, with optional port', false );
00041     }
00042 
00043     public function execute() {
00044         global $wgMainCacheType, $wgMemCachedTimeout, $wgObjectCaches;
00045 
00046         $cache = $this->getOption( 'cache' );
00047         $iterations = $this->getOption( 'i', 100 );
00048         if ( $cache ) {
00049             if ( !isset( $wgObjectCaches[$cache] ) ) {
00050                 $this->error( "MediaWiki isn't configured with a cache named '$cache'", 1 );
00051             }
00052             $servers = $wgObjectCaches[$cache]['servers'];
00053         } elseif ( $this->hasArg() ) {
00054             $servers = array( $this->getArg() );
00055         } elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
00056             global $wgMemCachedServers;
00057             $servers = $wgMemCachedServers;
00058         } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
00059             $servers = $wgObjectCaches[$wgMainCacheType]['servers'];
00060         } else {
00061             $this->error( "MediaWiki isn't configured for Memcached usage", 1 );
00062         }
00063 
00064         # find out the longest server string to nicely align output later on
00065         $maxSrvLen = $servers ? max( array_map( 'strlen', $servers ) ) : 0;
00066 
00067         foreach ( $servers as $server ) {
00068             $this->output(
00069                 str_pad( $server, $maxSrvLen ),
00070                 $server  # output channel
00071             );
00072 
00073             $mcc = new MemCachedClientforWiki( array(
00074                 'persistant' => true,
00075                 'timeout' => $wgMemCachedTimeout
00076             ) );
00077             $mcc->set_servers( array( $server ) );
00078             $set = 0;
00079             $incr = 0;
00080             $get = 0;
00081             $time_start = $this->microtime_float();
00082             for ( $i = 1; $i <= $iterations; $i++ ) {
00083                 if ( $mcc->set( "test$i", $i ) ) {
00084                     $set++;
00085                 }
00086             }
00087             for ( $i = 1; $i <= $iterations; $i++ ) {
00088                 if ( !is_null( $mcc->incr( "test$i", $i ) ) ) {
00089                     $incr++;
00090                 }
00091             }
00092             for ( $i = 1; $i <= $iterations; $i++ ) {
00093                 $value = $mcc->get( "test$i" );
00094                 if ( $value == $i * 2 ) {
00095                     $get++;
00096                 }
00097             }
00098             $exectime = $this->microtime_float() - $time_start;
00099 
00100             $this->output( " set: $set   incr: $incr   get: $get time: $exectime", $server );
00101         }
00102     }
00103 
00108     private function microtime_float() {
00109         list( $usec, $sec ) = explode( " ", microtime() );
00110         return ( (float)$usec + (float)$sec );
00111     }
00112 }
00113 
00114 $maintClass = "mcTest";
00115 require_once RUN_MAINTENANCE_IF_MAIN;