513edf391120c52313261442464812ea4c429eb8
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->addArg( 'server[:port]', 'Memcached server to test, with optional port', false );
43 public function execute() {
44 global $wgMainCacheType, $wgMemCachedTimeout, $wgObjectCaches;
46 $memcachedTypes = [ CACHE_MEMCACHED
, 'memcached-php', 'memcached-pecl' ];
48 $cache = $this->getOption( 'cache' );
49 $iterations = $this->getOption( 'i', 100 );
51 if ( !isset( $wgObjectCaches[$cache] ) ) {
52 $this->fatalError( "MediaWiki isn't configured with a cache named '$cache'" );
54 $servers = $wgObjectCaches[$cache]['servers'];
55 } elseif ( $this->hasArg( 0 ) ) {
56 $servers = [ $this->getArg( 0 ) ];
57 } elseif ( in_array( $wgMainCacheType, $memcachedTypes, true ) ) {
58 global $wgMemCachedServers;
59 $servers = $wgMemCachedServers;
60 } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
61 $servers = $wgObjectCaches[$wgMainCacheType]['servers'];
63 $this->fatalError( "MediaWiki isn't configured for Memcached usage" );
66 # find out the longest server string to nicely align output later on
67 $maxSrvLen = $servers ?
max( array_map( 'strlen', $servers ) ) : 0;
69 foreach ( $servers as $server ) {
71 str_pad( $server, $maxSrvLen ),
72 $server # output channel
75 $mcc = new MemcachedClient( [
77 'timeout' => $wgMemCachedTimeout
79 $mcc->set_servers( [ $server ] );
83 $time_start = microtime( true );
84 for ( $i = 1; $i <= $iterations; $i++
) {
85 if ( $mcc->set( "test$i", $i ) ) {
89 for ( $i = 1; $i <= $iterations; $i++
) {
90 if ( !is_null( $mcc->incr( "test$i", $i ) ) ) {
94 for ( $i = 1; $i <= $iterations; $i++
) {
95 $value = $mcc->get( "test$i" );
96 if ( $value == $i * 2 ) {
100 $exectime = microtime( true ) - $time_start;
102 $this->output( " set: $set incr: $incr get: $get time: $exectime", $server );
107 $maintClass = McTest
::class;
108 require_once RUN_MAINTENANCE_IF_MAIN
;