MediaWiki master
mctest.php
Go to the documentation of this file.
1<?php
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/Maintenance.php';
31// @codeCoverageIgnoreEnd
32
39class McTest extends Maintenance {
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription(
43 "Makes several operation requests on every cache server and shows a report.\n" .
44 "This tests both per-key and batched *Multi() methods as well as WRITE_BACKGROUND.\n" .
45 "\"IB\" means \"immediate blocking\" and \"DB\" means \"deferred blocking.\""
46 );
47 $this->addOption( 'cache', 'Use servers from this $wgObjectCaches store', true, true );
48 $this->addOption( 'class', 'Override the store "class" parameter', false, true );
49 $this->addOption( 'i', 'Number of iterations', false, true );
50 $this->addArg( 'server[:port]', 'Cache server to test, with optional port', false );
51 }
52
53 public function execute() {
54 $config = $this->getConfig();
55 $objectCaches = $config->get( MainConfigNames::ObjectCaches );
56
57 $cacheType = $this->getOption( 'cache', $config->get( MainConfigNames::MainCacheType ) );
58 $iterations = $this->getOption( 'i', 100 );
59 $classOverride = $this->getOption( 'class' );
60 $server = $this->getArg( 0 );
61
62 if ( !isset( $objectCaches[$cacheType] ) ) {
63 $this->fatalError( "No configured '$cacheType' cache" );
64 }
65
66 if ( $classOverride !== null ) {
67 if ( !is_subclass_of( $classOverride, BagOStuff::class ) ) {
68 $this->fatalError( "Invalid class '$classOverride' for cache" );
69 }
70 $class = $classOverride;
71 } else {
72 $class = $objectCaches[$cacheType]['class'];
73 }
74
75 if ( $server !== null ) {
76 $servers = [ $server ];
77 } else {
78 // Note that some caches, like apcu, do not have a server list
79 $servers = $objectCaches[$cacheType]['servers'] ?? [ null ];
80 }
81
82 // Use longest server string for output alignment
83 $maxSrvLen = max( array_map( 'strlen', $servers ) );
84
85 $this->output( "Warming up connections to cache servers..." );
87 $cacheByServer = [];
88 foreach ( $servers as $server ) {
89 $conf = $objectCaches[$cacheType];
90 if ( $server !== null ) {
91 $conf['servers'] = [ $server ];
92 $host = $server;
93 } else {
94 $host = 'localhost';
95 }
96 $cacheByServer[$host] = new $class( $conf );
97 $cacheByServer[$host]->get( 'key' );
98 }
99 $this->output( "done\n" );
100 $this->output( "Single and batched operation profiling/test results:\n" );
101
102 $valueByKey = [];
103 for ( $i = 1; $i <= $iterations; $i++ ) {
104 $valueByKey["test$i"] = 'S' . str_pad( (string)$i, 2048 );
105 }
106
107 foreach ( $cacheByServer as $host => $mcc ) {
108 $this->output( str_pad( $host, $maxSrvLen ) . "\n" );
109 $this->benchmarkSingleKeyOps( $mcc, $valueByKey );
110 $this->benchmarkMultiKeyOpsImmediateBlocking( $mcc, $valueByKey );
111 $this->benchmarkMultiKeyOpsDeferredBlocking( $mcc, $valueByKey );
112 }
113 }
114
119 private function benchmarkSingleKeyOps( BagOStuff $mcc, array $valueByKey ) {
120 $add = 0;
121 $set = 0;
122 $incr = 0;
123 $get = 0;
124 $delete = 0;
125
126 $keys = array_keys( $valueByKey );
127 $count = count( $valueByKey );
128
129 // Clear out any old values
130 $mcc->deleteMulti( $keys );
131
132 $time_start = microtime( true );
133 foreach ( $valueByKey as $key => $value ) {
134 if ( $mcc->add( $key, $value ) ) {
135 $add++;
136 }
137 }
138 $addMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
139
140 $time_start = microtime( true );
141 foreach ( $valueByKey as $key => $value ) {
142 if ( $mcc->set( $key, $value ) ) {
143 $set++;
144 }
145 }
146 $setMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
147
148 $time_start = microtime( true );
149 foreach ( $valueByKey as $key => $value ) {
150 if ( $mcc->get( $key ) === $value ) {
151 $get++;
152 }
153 }
154 $getMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
155
156 $time_start = microtime( true );
157 foreach ( $keys as $key ) {
158 if ( $mcc->delete( $key ) ) {
159 $delete++;
160 }
161 }
162 $delMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
163
164 $time_start = microtime( true );
165 foreach ( $keys as $index => $key ) {
166 if ( $mcc->incrWithInit( $key, $mcc::TTL_INDEFINITE, $index ) === $index ) {
167 $incr++;
168 }
169 }
170 $incrMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
171
172 $this->output(
173 " add: $add/$count {$addMs}ms " .
174 "set: $set/$count {$setMs}ms " .
175 "get: $get/$count ({$getMs}ms) " .
176 "delete: $delete/$count ({$delMs}ms) " .
177 "incr: $incr/$count ({$incrMs}ms)\n"
178 );
179 }
180
185 private function benchmarkMultiKeyOpsImmediateBlocking( BagOStuff $mcc, array $valueByKey ) {
186 $keys = array_keys( $valueByKey );
187 $iterations = count( $valueByKey );
188
189 $time_start = microtime( true );
190 $mSetOk = $mcc->setMulti( $valueByKey ) ? '✓' : '✗';
191 $mSetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
192
193 $time_start = microtime( true );
194 $found = $mcc->getMulti( $keys );
195 $mGetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
196 $mGetOk = 0;
197 foreach ( $found as $key => $value ) {
198 $mGetOk += ( $value === $valueByKey[$key] );
199 }
200
201 $time_start = microtime( true );
202 $mChangeTTLOk = $mcc->changeTTLMulti( $keys, 3600 ) ? '✓' : '✗';
203 $mChangeTTTMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
204
205 $time_start = microtime( true );
206 $mDelOk = $mcc->deleteMulti( $keys ) ? '✓' : '✗';
207 $mDelMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
208
209 $this->output(
210 " setMulti (IB): $mSetOk {$mSetMs}ms " .
211 "getMulti (IB): $mGetOk/$iterations {$mGetMs}ms " .
212 "changeTTLMulti (IB): $mChangeTTLOk {$mChangeTTTMs}ms " .
213 "deleteMulti (IB): $mDelOk {$mDelMs}ms\n"
214 );
215 }
216
221 private function benchmarkMultiKeyOpsDeferredBlocking( BagOStuff $mcc, array $valueByKey ) {
222 $keys = array_keys( $valueByKey );
223 $iterations = count( $valueByKey );
224 $flags = $mcc::WRITE_BACKGROUND;
225
226 $time_start = microtime( true );
227 $mSetOk = $mcc->setMulti( $valueByKey, 0, $flags ) ? '✓' : '✗';
228 $mSetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
229
230 $time_start = microtime( true );
231 $found = $mcc->getMulti( $keys );
232 $mGetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
233 $mGetOk = 0;
234 foreach ( $found as $key => $value ) {
235 $mGetOk += ( $value === $valueByKey[$key] );
236 }
237
238 $time_start = microtime( true );
239 $mChangeTTLOk = $mcc->changeTTLMulti( $keys, 3600, $flags ) ? '✓' : '✗';
240 $mChangeTTTMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
241
242 $time_start = microtime( true );
243 $mDelOk = $mcc->deleteMulti( $keys, $flags ) ? '✓' : '✗';
244 $mDelMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
245
246 $this->output(
247 " setMulti (DB): $mSetOk {$mSetMs}ms " .
248 "getMulti (DB): $mGetOk/$iterations {$mGetMs}ms " .
249 "changeTTLMulti (DB): $mChangeTTLOk {$mChangeTTTMs}ms " .
250 "deleteMulti (DB): $mDelOk {$mDelMs}ms\n"
251 );
252 }
253}
254
255// @codeCoverageIgnoreStart
256$maintClass = McTest::class;
257require_once RUN_MAINTENANCE_IF_MAIN;
258// @codeCoverageIgnoreEnd
Maintenance script that makes several 'set', 'incr' and 'get' requests on every memcached server and ...
Definition mctest.php:39
__construct()
Default constructor.
Definition mctest.php:40
execute()
Do the actual work.
Definition mctest.php:53
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
Abstract class for any ephemeral data store.
Definition BagOStuff.php:89
incrWithInit( $key, $exptime, $step=1, $init=null, $flags=0)
Increase the value of the given key (no TTL change) if it exists or create it otherwise.
getMulti(array $keys, $flags=0)
Get a batch of items.
delete( $key, $flags=0)
Delete an item if it exists.
set( $key, $value, $exptime=0, $flags=0)
Set an item.
deleteMulti(array $keys, $flags=0)
Delete a batch of items.
get( $key, $flags=0)
Get an item.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple items.
setMulti(array $valueByKey, $exptime=0, $flags=0)
Set a batch of items.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
$maintClass
Definition mctest.php:256