MediaWiki  master
mctest.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
33 class McTest extends Maintenance {
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( "Makes several 'set', 'incr' and 'get' requests on every"
37  . " memcached server and shows a report" );
38  $this->addOption( 'i', 'Number of iterations', false, true );
39  $this->addOption( 'cache', 'Use servers from this $wgObjectCaches store', false, true );
40  $this->addOption( 'driver', 'Either "php" or "pecl"', false, true );
41  $this->addArg( 'server[:port]', 'Memcached server to test, with optional port', false );
42  }
43 
44  public function execute() {
46 
47  $memcachedTypes = [ CACHE_MEMCACHED, 'memcached-php', 'memcached-pecl' ];
48 
49  $cache = $this->getOption( 'cache' );
50  $iterations = $this->getOption( 'i', 100 );
51  if ( $cache ) {
52  if ( !isset( $wgObjectCaches[$cache] ) ) {
53  $this->fatalError( "MediaWiki isn't configured with a cache named '$cache'" );
54  }
55  $servers = $wgObjectCaches[$cache]['servers'];
56  } elseif ( $this->hasArg( 0 ) ) {
57  $servers = [ $this->getArg( 0 ) ];
58  } elseif ( in_array( $wgMainCacheType, $memcachedTypes, true ) ) {
59  global $wgMemCachedServers;
60  $servers = $wgMemCachedServers;
61  } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
62  $servers = $wgObjectCaches[$wgMainCacheType]['servers'];
63  } else {
64  $this->fatalError( "MediaWiki isn't configured for Memcached usage" );
65  }
66 
67  # find out the longest server string to nicely align output later on
68  $maxSrvLen = $servers ? max( array_map( 'strlen', $servers ) ) : 0;
69 
70  $type = $this->getOption( 'driver', 'php' );
71  if ( $type === 'php' ) {
73  } elseif ( $type === 'pecl' ) {
75  } else {
76  $this->fatalError( "Invalid driver type '$type'" );
77  }
78 
79  foreach ( $servers as $server ) {
80  $this->output( str_pad( $server, $maxSrvLen ) . "\n" );
81 
83  $mcc = new $class( [
84  'servers' => [ $server ],
85  'persistent' => true,
86  'timeout' => $wgMemCachedTimeout
87  ] );
88 
89  $this->benchmarkSingleKeyOps( $mcc, $iterations );
90  $this->benchmarkMultiKeyOpsImmediateBlocking( $mcc, $iterations );
91  $this->benchmarkMultiKeyOpsDeferredBlocking( $mcc, $iterations );
92  }
93  }
94 
99  private function benchmarkSingleKeyOps( $mcc, $iterations ) {
100  $add = 0;
101  $set = 0;
102  $incr = 0;
103  $get = 0;
104  $delete = 0;
105 
106  $keys = [];
107  for ( $i = 1; $i <= $iterations; $i++ ) {
108  $keys[] = "test$i";
109  }
110 
111  // Clear out any old values
112  $mcc->deleteMulti( $keys );
113 
114  $time_start = microtime( true );
115  foreach ( $keys as $key ) {
116  if ( $mcc->add( $key, $i ) ) {
117  $add++;
118  }
119  }
120  $addMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
121 
122  $time_start = microtime( true );
123  foreach ( $keys as $key ) {
124  if ( $mcc->set( $key, $i ) ) {
125  $set++;
126  }
127  }
128  $setMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
129 
130  $time_start = microtime( true );
131  foreach ( $keys as $key ) {
132  if ( !is_null( $mcc->incr( $key, $i ) ) ) {
133  $incr++;
134  }
135  }
136  $incrMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
137 
138  $time_start = microtime( true );
139  foreach ( $keys as $key ) {
140  $value = $mcc->get( $key );
141  if ( $value == $i * 2 ) {
142  $get++;
143  }
144  }
145  $getMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
146 
147  $time_start = microtime( true );
148  foreach ( $keys as $key ) {
149  if ( $mcc->delete( $key ) ) {
150  $delete++;
151  }
152  }
153  $delMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
154 
155  $this->output(
156  " add: $add/$iterations {$addMs}ms " .
157  "set: $set/$iterations {$setMs}ms " .
158  "incr: $incr/$iterations {$incrMs}ms " .
159  "get: $get/$iterations ({$getMs}ms) " .
160  "delete: $delete/$iterations ({$delMs}ms)\n"
161  );
162  }
163 
168  private function benchmarkMultiKeyOpsImmediateBlocking( $mcc, $iterations ) {
169  $keysByValue = [];
170  for ( $i = 1; $i <= $iterations; $i++ ) {
171  $keysByValue["test$i"] = 'S' . str_pad( $i, 2048 );
172  }
173  $keyList = array_keys( $keysByValue );
174 
175  $time_start = microtime( true );
176  $mSetOk = $mcc->setMulti( $keysByValue ) ? 'S' : 'F';
177  $mSetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
178 
179  $time_start = microtime( true );
180  $found = $mcc->getMulti( $keyList );
181  $mGetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
182  $mGetOk = 0;
183  foreach ( $found as $key => $value ) {
184  $mGetOk += ( $value === $keysByValue[$key] );
185  }
186 
187  $time_start = microtime( true );
188  $mChangeTTLOk = $mcc->changeTTLMulti( $keyList, 3600 ) ? 'S' : 'F';
189  $mChangeTTTMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
190 
191  $time_start = microtime( true );
192  $mDelOk = $mcc->deleteMulti( $keyList ) ? 'S' : 'F';
193  $mDelMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
194 
195  $this->output(
196  " setMulti (IB): $mSetOk {$mSetMs}ms " .
197  "getMulti (IB): $mGetOk/$iterations {$mGetMs}ms " .
198  "changeTTLMulti (IB): $mChangeTTLOk {$mChangeTTTMs}ms " .
199  "deleteMulti (IB): $mDelOk {$mDelMs}ms\n"
200  );
201  }
202 
207  private function benchmarkMultiKeyOpsDeferredBlocking( $mcc, $iterations ) {
208  $flags = $mcc::WRITE_BACKGROUND;
209  $keysByValue = [];
210  for ( $i = 1; $i <= $iterations; $i++ ) {
211  $keysByValue["test$i"] = 'A' . str_pad( $i, 2048 );
212  }
213  $keyList = array_keys( $keysByValue );
214 
215  $time_start = microtime( true );
216  $mSetOk = $mcc->setMulti( $keysByValue, 0, $flags ) ? 'S' : 'F';
217  $mSetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
218 
219  $time_start = microtime( true );
220  $found = $mcc->getMulti( $keyList );
221  $mGetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
222  $mGetOk = 0;
223  foreach ( $found as $key => $value ) {
224  $mGetOk += ( $value === $keysByValue[$key] );
225  }
226 
227  $time_start = microtime( true );
228  $mChangeTTLOk = $mcc->changeTTLMulti( $keyList, 3600, $flags ) ? 'S' : 'F';
229  $mChangeTTTMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
230 
231  $time_start = microtime( true );
232  $mDelOk = $mcc->deleteMulti( $keyList, $flags ) ? 'S' : 'F';
233  $mDelMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
234 
235  $this->output(
236  " setMulti (DB): $mSetOk {$mSetMs}ms " .
237  "getMulti (DB): $mGetOk/$iterations {$mGetMs}ms " .
238  "changeTTLMulti (DB): $mChangeTTLOk {$mChangeTTTMs}ms " .
239  "deleteMulti (DB): $mDelOk {$mDelMs}ms\n"
240  );
241  }
242 }
243 
245 require_once RUN_MAINTENANCE_IF_MAIN;
getArg( $argId=0, $default=null)
Get an argument.
CACHE_MEMCACHED $wgMainCacheType
Definition: memcached.txt:63
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$value
__construct()
Definition: mctest.php:34
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$maintClass
Definition: mctest.php:244
const CACHE_MEMCACHED
Definition: Defines.php:84
benchmarkMultiKeyOpsDeferredBlocking( $mcc, $iterations)
Definition: mctest.php:207
benchmarkSingleKeyOps( $mcc, $iterations)
Definition: mctest.php:99
hasArg( $argId=0)
Does a given argument exist?
addDescription( $text)
Set the description text.
$cache
Definition: mcc.php:33
$wgMemCachedServers
Definition: memcached.txt:64
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
benchmarkMultiKeyOpsImmediateBlocking( $mcc, $iterations)
Definition: mctest.php:168
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
if( $help) $mcc
Definition: mcc.php:39
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
$wgObjectCaches
Advanced object cache configuration.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
execute()
Definition: mctest.php:44
Maintenance script that makes several &#39;set&#39;, &#39;incr&#39; and &#39;get&#39; requests on every memcached server and ...
Definition: mctest.php:33
$wgMemCachedTimeout
Read/write timeout for MemCached server communication, in microseconds.