Merge "Exclude redirects from Special:Fewestrevisions"
[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->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() {
45 global $wgMainCacheType, $wgMemCachedTimeout, $wgObjectCaches;
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' ) {
72 $class = MemcachedPhpBagOStuff::class;
73 } elseif ( $type === 'pecl' ) {
74 $class = MemcachedPeclBagOStuff::class;
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
82 /** @var BagOStuff $mcc */
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
95 /**
96 * @param BagOStuff $mcc
97 * @param int $iterations
98 */
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
164 /**
165 * @param BagOStuff $mcc
166 * @param int $iterations
167 */
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
203 /**
204 * @param BagOStuff $mcc
205 * @param int $iterations
206 */
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
244 $maintClass = McTest::class;
245 require_once RUN_MAINTENANCE_IF_MAIN;