Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / maintenance / mctest.php
blob26e1e18543be128e2dc88cbd55cbfde4fbf1f0eb
1 <?php
2 /**
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
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
27 /**
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->mDescription = "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 $cache = $this->getOption( 'cache' );
47 $iterations = $this->getOption( 'i', 100 );
48 if ( $cache ) {
49 if ( !isset( $wgObjectCaches[$cache] ) ) {
50 $this->error( "MediaWiki isn't configured with a cache named '$cache'", 1 );
52 $servers = $wgObjectCaches[$cache]['servers'];
53 } elseif ( $this->hasArg() ) {
54 $servers = array( $this->getArg() );
55 } elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
56 global $wgMemCachedServers;
57 $servers = $wgMemCachedServers;
58 } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
59 $servers = $wgObjectCaches[$wgMainCacheType]['servers'];
60 } else {
61 $this->error( "MediaWiki isn't configured for Memcached usage", 1 );
64 foreach ( $servers as $server ) {
65 $this->output( $server . " ", $server );
66 $mcc = new MemCachedClientforWiki( array(
67 'persistant' => true,
68 'timeout' => $wgMemCachedTimeout
69 ) );
70 $mcc->set_servers( array( $server ) );
71 $set = 0;
72 $incr = 0;
73 $get = 0;
74 $time_start = $this->microtime_float();
75 for ( $i = 1; $i <= $iterations; $i++ ) {
76 if ( !is_null( $mcc->set( "test$i", $i ) ) ) {
77 $set++;
80 for ( $i = 1; $i <= $iterations; $i++ ) {
81 if ( !is_null( $mcc->incr( "test$i", $i ) ) ) {
82 $incr++;
85 for ( $i = 1; $i <= $iterations; $i++ ) {
86 $value = $mcc->get( "test$i" );
87 if ( $value == $i * 2 ) {
88 $get++;
91 $exectime = $this->microtime_float() - $time_start;
93 $this->output( "set: $set incr: $incr get: $get time: $exectime", $server );
97 /**
98 * Return microtime() as a float
99 * @return float
101 private function microtime_float() {
102 list( $usec, $sec ) = explode( " ", microtime() );
103 return ( (float)$usec + (float)$sec );
107 $maintClass = "mcTest";
108 require_once RUN_MAINTENANCE_IF_MAIN;