Use __DIR__ instead of dirname( __FILE__ )
[lhc/web/wiklou.git] / maintenance / mctest.php
1 <?php
2 /**
3 * Makes several 'set', 'incr' and 'get' requests on every memcached
4 * server and shows a report.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once( __DIR__ . '/Maintenance.php' );
26
27 /**
28 * Maintenance script that makes several 'set', 'incr' and 'get' requests
29 * on every memcached server and shows a report.
30 *
31 * @ingroup Maintenance
32 */
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->addArg( 'server[:port]', 'Memcached server to test, with optional port', false );
40 }
41
42 public function execute() {
43 global $wgMemCachedServers, $wgMemCachedTimeout;
44
45 $iterations = $this->getOption( 'i', 100 );
46 if ( $this->hasArg() ) {
47 $wgMemCachedServers = array( $this->getArg() );
48 }
49
50 foreach ( $wgMemCachedServers as $server ) {
51 $this->output( $server . " ", $server );
52 $mcc = new MemCachedClientforWiki( array(
53 'persistant' => true,
54 'timeout' => $wgMemCachedTimeout
55 ) );
56 $mcc->set_servers( array( $server ) );
57 $set = 0;
58 $incr = 0;
59 $get = 0;
60 $time_start = $this->microtime_float();
61 for ( $i = 1; $i <= $iterations; $i++ ) {
62 if ( !is_null( $mcc->set( "test$i", $i ) ) ) {
63 $set++;
64 }
65 }
66 for ( $i = 1; $i <= $iterations; $i++ ) {
67 if ( !is_null( $mcc->incr( "test$i", $i ) ) ) {
68 $incr++;
69 }
70 }
71 for ( $i = 1; $i <= $iterations; $i++ ) {
72 $value = $mcc->get( "test$i" );
73 if ( $value == $i * 2 ) {
74 $get++;
75 }
76 }
77 $exectime = $this->microtime_float() - $time_start;
78
79 $this->output( "set: $set incr: $incr get: $get time: $exectime", $server );
80 }
81 }
82
83 /**
84 * Return microtime() as a float
85 * @return float
86 */
87 private function microtime_float() {
88 list( $usec, $sec ) = explode( " ", microtime() );
89 return ( (float)$usec + (float)$sec );
90 }
91 }
92
93 $maintClass = "mcTest";
94 require_once( RUN_MAINTENANCE_IF_MAIN );