3 * Makes several 'set', 'incr' and 'get' requests on every memcached
4 * server and shows a report.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
25 require_once __DIR__
. '/Maintenance.php';
28 * Maintenance script that makes several 'set', 'incr' and 'get' requests
29 * on every memcached server and shows a report.
31 * @ingroup Maintenance
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 );
44 public function execute() {
45 global $wgMainCacheType, $wgMemCachedTimeout, $wgObjectCaches;
47 $memcachedTypes = [ CACHE_MEMCACHED
, 'memcached-php', 'memcached-pecl' ];
49 $cache = $this->getOption( 'cache' );
50 $iterations = $this->getOption( 'i', 100 );
52 if ( !isset( $wgObjectCaches[$cache] ) ) {
53 $this->fatalError( "MediaWiki isn't configured with a cache named '$cache'" );
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'];
64 $this->fatalError( "MediaWiki isn't configured for Memcached usage" );
67 # find out the longest server string to nicely align output later on
68 $maxSrvLen = $servers ?
max( array_map( 'strlen', $servers ) ) : 0;
70 $type = $this->getOption( 'driver', 'php' );
71 if ( $type === 'php' ) {
72 $class = MemcachedPhpBagOStuff
::class;
73 } elseif ( $type === 'pecl' ) {
74 $class = MemcachedPeclBagOStuff
::class;
76 $this->fatalError( "Invalid driver type '$type'" );
79 foreach ( $servers as $server ) {
80 $this->output( str_pad( $server, $maxSrvLen ) . "\n" );
82 /** @var BagOStuff $mcc */
84 'servers' => [ $server ],
86 'timeout' => $wgMemCachedTimeout
89 $this->benchmarkSingleKeyOps( $mcc, $iterations );
90 $this->benchmarkMultiKeyOpsImmediateBlocking( $mcc, $iterations );
91 $this->benchmarkMultiKeyOpsDeferredBlocking( $mcc, $iterations );
96 * @param BagOStuff $mcc
97 * @param int $iterations
99 private function benchmarkSingleKeyOps( $mcc, $iterations ) {
107 for ( $i = 1; $i <= $iterations; $i++
) {
111 // Clear out any old values
112 $mcc->deleteMulti( $keys );
114 $time_start = microtime( true );
115 foreach ( $keys as $key ) {
116 if ( $mcc->add( $key, $i ) ) {
120 $addMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
122 $time_start = microtime( true );
123 foreach ( $keys as $key ) {
124 if ( $mcc->set( $key, $i ) ) {
128 $setMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
130 $time_start = microtime( true );
131 foreach ( $keys as $key ) {
132 if ( !is_null( $mcc->incr( $key, $i ) ) ) {
136 $incrMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
138 $time_start = microtime( true );
139 foreach ( $keys as $key ) {
140 $value = $mcc->get( $key );
141 if ( $value == $i * 2 ) {
145 $getMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
147 $time_start = microtime( true );
148 foreach ( $keys as $key ) {
149 if ( $mcc->delete( $key ) ) {
153 $delMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
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"
165 * @param BagOStuff $mcc
166 * @param int $iterations
168 private function benchmarkMultiKeyOpsImmediateBlocking( $mcc, $iterations ) {
170 for ( $i = 1; $i <= $iterations; $i++
) {
171 $keysByValue["test$i"] = 'S' . str_pad( $i, 2048 );
173 $keyList = array_keys( $keysByValue );
175 $time_start = microtime( true );
176 $mSetOk = $mcc->setMulti( $keysByValue ) ?
'S' : 'F';
177 $mSetMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
179 $time_start = microtime( true );
180 $found = $mcc->getMulti( $keyList );
181 $mGetMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
183 foreach ( $found as $key => $value ) {
184 $mGetOk +
= ( $value === $keysByValue[$key] );
187 $time_start = microtime( true );
188 $mChangeTTLOk = $mcc->changeTTLMulti( $keyList, 3600 ) ?
'S' : 'F';
189 $mChangeTTTMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
191 $time_start = microtime( true );
192 $mDelOk = $mcc->deleteMulti( $keyList ) ?
'S' : 'F';
193 $mDelMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
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"
204 * @param BagOStuff $mcc
205 * @param int $iterations
207 private function benchmarkMultiKeyOpsDeferredBlocking( $mcc, $iterations ) {
208 $flags = $mcc::WRITE_BACKGROUND
;
210 for ( $i = 1; $i <= $iterations; $i++
) {
211 $keysByValue["test$i"] = 'A' . str_pad( $i, 2048 );
213 $keyList = array_keys( $keysByValue );
215 $time_start = microtime( true );
216 $mSetOk = $mcc->setMulti( $keysByValue, 0, $flags ) ?
'S' : 'F';
217 $mSetMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
219 $time_start = microtime( true );
220 $found = $mcc->getMulti( $keyList );
221 $mGetMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
223 foreach ( $found as $key => $value ) {
224 $mGetOk +
= ( $value === $keysByValue[$key] );
227 $time_start = microtime( true );
228 $mChangeTTLOk = $mcc->changeTTLMulti( $keyList, 3600, $flags ) ?
'S' : 'F';
229 $mChangeTTTMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
231 $time_start = microtime( true );
232 $mDelOk = $mcc->deleteMulti( $keyList, $flags ) ?
'S' : 'F';
233 $mDelMs = intval( 1e3
* ( microtime( true ) - $time_start ) );
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"
244 $maintClass = McTest
::class;
245 require_once RUN_MAINTENANCE_IF_MAIN
;