MediaWiki master
mctest.php
Go to the documentation of this file.
1<?php
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
25class McTest extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription(
29 "Makes several operation requests on every cache server and shows a report.\n" .
30 "This tests both per-key and batched *Multi() methods as well as WRITE_BACKGROUND.\n" .
31 "\"IB\" means \"immediate blocking\" and \"DB\" means \"deferred blocking.\""
32 );
33 $this->addOption( 'cache', 'Use servers from this $wgObjectCaches store', true, true );
34 $this->addOption( 'class', 'Override the store "class" parameter', false, true );
35 $this->addOption( 'i', 'Number of iterations', false, true );
36 $this->addArg( 'server[:port]', 'Cache server to test, with optional port', false );
37 }
38
39 public function execute() {
40 $config = $this->getConfig();
41 $objectCaches = $config->get( MainConfigNames::ObjectCaches );
42
43 $cacheType = $this->getOption( 'cache', $config->get( MainConfigNames::MainCacheType ) );
44 $iterations = $this->getOption( 'i', 100 );
45 $classOverride = $this->getOption( 'class' );
46 $server = $this->getArg( 0 );
47
48 if ( !isset( $objectCaches[$cacheType] ) ) {
49 $this->fatalError( "No configured '$cacheType' cache" );
50 }
51
52 if ( $classOverride !== null ) {
53 if ( !is_subclass_of( $classOverride, BagOStuff::class ) ) {
54 $this->fatalError( "Invalid class '$classOverride' for cache" );
55 }
56 $class = $classOverride;
57 } else {
58 $class = $objectCaches[$cacheType]['class'];
59 }
60
61 if ( $server !== null ) {
62 $servers = [ $server ];
63 } else {
64 // Note that some caches, like apcu, do not have a server list
65 $servers = $objectCaches[$cacheType]['servers'] ?? [ null ];
66 }
67
68 // Use longest server string for output alignment
69 $maxSrvLen = max( array_map( 'strlen', $servers ) );
70
71 $this->output( "Warming up connections to cache servers..." );
73 $cacheByServer = [];
74 foreach ( $servers as $server ) {
75 $conf = $objectCaches[$cacheType];
76 if ( $server !== null ) {
77 $conf['servers'] = [ $server ];
78 $host = $server;
79 } else {
80 $host = 'localhost';
81 }
82 $cacheByServer[$host] = new $class( $conf );
83 $cacheByServer[$host]->get( 'key' );
84 }
85 $this->output( "done\n" );
86 $this->output( "Single and batched operation profiling/test results:\n" );
87
88 $valueByKey = [];
89 for ( $i = 1; $i <= $iterations; $i++ ) {
90 $valueByKey["test$i"] = 'S' . str_pad( (string)$i, 2048 );
91 }
92
93 foreach ( $cacheByServer as $host => $mcc ) {
94 $this->output( str_pad( $host, $maxSrvLen ) . "\n" );
95 $this->benchmarkSingleKeyOps( $mcc, $valueByKey );
96 $this->benchmarkMultiKeyOpsImmediateBlocking( $mcc, $valueByKey );
97 $this->benchmarkMultiKeyOpsDeferredBlocking( $mcc, $valueByKey );
98 }
99 }
100
101 private function benchmarkSingleKeyOps( BagOStuff $mcc, array $valueByKey ) {
102 $add = 0;
103 $set = 0;
104 $incr = 0;
105 $get = 0;
106 $delete = 0;
107
108 $keys = array_keys( $valueByKey );
109 $count = count( $valueByKey );
110
111 // Clear out any old values
112 $mcc->deleteMulti( $keys );
113
114 $time_start = microtime( true );
115 foreach ( $valueByKey as $key => $value ) {
116 if ( $mcc->add( $key, $value ) ) {
117 $add++;
118 }
119 }
120 $addMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
121
122 $time_start = microtime( true );
123 foreach ( $valueByKey as $key => $value ) {
124 if ( $mcc->set( $key, $value ) ) {
125 $set++;
126 }
127 }
128 $setMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
129
130 $time_start = microtime( true );
131 foreach ( $valueByKey as $key => $value ) {
132 if ( $mcc->get( $key ) === $value ) {
133 $get++;
134 }
135 }
136 $getMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
137
138 $time_start = microtime( true );
139 foreach ( $keys as $key ) {
140 if ( $mcc->delete( $key ) ) {
141 $delete++;
142 }
143 }
144 $delMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
145
146 $time_start = microtime( true );
147 foreach ( $keys as $index => $key ) {
148 if ( $mcc->incrWithInit( $key, $mcc::TTL_INDEFINITE, $index ) === $index ) {
149 $incr++;
150 }
151 }
152 $incrMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
153
154 $this->output(
155 " add: $add/$count {$addMs}ms " .
156 "set: $set/$count {$setMs}ms " .
157 "get: $get/$count ({$getMs}ms) " .
158 "delete: $delete/$count ({$delMs}ms) " .
159 "incr: $incr/$count ({$incrMs}ms)\n"
160 );
161 }
162
163 private function benchmarkMultiKeyOpsImmediateBlocking( BagOStuff $mcc, array $valueByKey ) {
164 $keys = array_keys( $valueByKey );
165 $iterations = count( $valueByKey );
166
167 $time_start = microtime( true );
168 $mSetOk = $mcc->setMulti( $valueByKey ) ? '✓' : '✗';
169 $mSetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
170
171 $time_start = microtime( true );
172 $found = $mcc->getMulti( $keys );
173 $mGetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
174 $mGetOk = 0;
175 foreach ( $found as $key => $value ) {
176 $mGetOk += ( $value === $valueByKey[$key] );
177 }
178
179 $time_start = microtime( true );
180 $mChangeTTLOk = $mcc->changeTTLMulti( $keys, 3600 ) ? '✓' : '✗';
181 $mChangeTTTMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
182
183 $time_start = microtime( true );
184 $mDelOk = $mcc->deleteMulti( $keys ) ? '✓' : '✗';
185 $mDelMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
186
187 $this->output(
188 " setMulti (IB): $mSetOk {$mSetMs}ms " .
189 "getMulti (IB): $mGetOk/$iterations {$mGetMs}ms " .
190 "changeTTLMulti (IB): $mChangeTTLOk {$mChangeTTTMs}ms " .
191 "deleteMulti (IB): $mDelOk {$mDelMs}ms\n"
192 );
193 }
194
195 private function benchmarkMultiKeyOpsDeferredBlocking( BagOStuff $mcc, array $valueByKey ) {
196 $keys = array_keys( $valueByKey );
197 $iterations = count( $valueByKey );
198 $flags = $mcc::WRITE_BACKGROUND;
199
200 $time_start = microtime( true );
201 $mSetOk = $mcc->setMulti( $valueByKey, 0, $flags ) ? '✓' : '✗';
202 $mSetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
203
204 $time_start = microtime( true );
205 $found = $mcc->getMulti( $keys );
206 $mGetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
207 $mGetOk = 0;
208 foreach ( $found as $key => $value ) {
209 $mGetOk += ( $value === $valueByKey[$key] );
210 }
211
212 $time_start = microtime( true );
213 $mChangeTTLOk = $mcc->changeTTLMulti( $keys, 3600, $flags ) ? '✓' : '✗';
214 $mChangeTTTMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
215
216 $time_start = microtime( true );
217 $mDelOk = $mcc->deleteMulti( $keys, $flags ) ? '✓' : '✗';
218 $mDelMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
219
220 $this->output(
221 " setMulti (DB): $mSetOk {$mSetMs}ms " .
222 "getMulti (DB): $mGetOk/$iterations {$mGetMs}ms " .
223 "changeTTLMulti (DB): $mChangeTTLOk {$mChangeTTTMs}ms " .
224 "deleteMulti (DB): $mDelOk {$mDelMs}ms\n"
225 );
226 }
227}
228
229// @codeCoverageIgnoreStart
230$maintClass = McTest::class;
231require_once RUN_MAINTENANCE_IF_MAIN;
232// @codeCoverageIgnoreEnd
Maintenance script that makes several 'set', 'incr' and 'get' requests on every memcached server and ...
Definition mctest.php:25
__construct()
Default constructor.
Definition mctest.php:26
execute()
Do the actual work.
Definition mctest.php:39
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:73
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:230