38 parent::__construct();
40 "Makes several operation requests on every cache server and shows a report.\n" .
41 "This tests both per-key and batched *Multi() methods as well as WRITE_BACKGROUND.\n" .
42 "\"IB\" means \"immediate blocking\" and \"DB\" means \"deferred blocking.\""
44 $this->
addOption(
'cache',
'Use servers from this $wgObjectCaches store',
true,
true );
45 $this->
addOption(
'class',
'Override the store "class" parameter',
false,
true );
46 $this->
addOption(
'i',
'Number of iterations',
false,
true );
47 $this->
addArg(
'server[:port]',
'Cache server to test, with optional port',
false );
52 $objectCaches = $config->get( MainConfigNames::ObjectCaches );
54 $cacheType = $this->
getOption(
'cache', $config->get( MainConfigNames::MainCacheType ) );
55 $iterations = $this->
getOption(
'i', 100 );
56 $classOverride = $this->
getOption(
'class' );
57 $server = $this->
getArg( 0 );
59 if ( !isset( $objectCaches[$cacheType] ) ) {
60 $this->
fatalError(
"No configured '$cacheType' cache" );
63 if ( $classOverride !==
null ) {
64 if ( !is_subclass_of( $classOverride, BagOStuff::class ) ) {
65 $this->
fatalError(
"Invalid class '$classOverride' for cache" );
67 $class = $classOverride;
69 $class = $objectCaches[$cacheType][
'class'];
72 if ( $server !==
null ) {
73 $servers = [ $server ];
76 $servers = $objectCaches[$cacheType][
'servers'] ?? [ null ];
80 $maxSrvLen = max( array_map(
'strlen', $servers ) );
82 $this->
output(
"Warming up connections to cache servers..." );
85 foreach ( $servers as $server ) {
86 $conf = $objectCaches[$cacheType];
87 if ( $server !==
null ) {
88 $conf[
'servers'] = [ $server ];
93 $cacheByServer[$host] =
new $class( $conf );
94 $cacheByServer[$host]->get(
'key' );
97 $this->
output(
"Single and batched operation profiling/test results:\n" );
100 for ( $i = 1; $i <= $iterations; $i++ ) {
101 $valueByKey[
"test$i"] =
'S' . str_pad( (
string)$i, 2048 );
104 foreach ( $cacheByServer as $host => $mcc ) {
105 $this->
output( str_pad( $host, $maxSrvLen ) .
"\n" );
106 $this->benchmarkSingleKeyOps( $mcc, $valueByKey );
107 $this->benchmarkMultiKeyOpsImmediateBlocking( $mcc, $valueByKey );
108 $this->benchmarkMultiKeyOpsDeferredBlocking( $mcc, $valueByKey );
116 private function benchmarkSingleKeyOps(
BagOStuff $mcc, array $valueByKey ) {
123 $keys = array_keys( $valueByKey );
124 $count = count( $valueByKey );
129 $time_start = microtime(
true );
130 foreach ( $valueByKey as $key => $value ) {
131 if ( $mcc->
add( $key, $value ) ) {
135 $addMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
137 $time_start = microtime(
true );
138 foreach ( $valueByKey as $key => $value ) {
139 if ( $mcc->
set( $key, $value ) ) {
143 $setMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
145 $time_start = microtime(
true );
146 foreach ( $valueByKey as $key => $value ) {
147 if ( $mcc->
get( $key ) === $value ) {
151 $getMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
153 $time_start = microtime(
true );
154 foreach ( $keys as $key ) {
155 if ( $mcc->
delete( $key ) ) {
159 $delMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
161 $time_start = microtime(
true );
162 foreach ( $keys as $index => $key ) {
163 if ( $mcc->
incrWithInit( $key, $mcc::TTL_INDEFINITE, $index ) === $index ) {
167 $incrMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
170 " add: $add/$count {$addMs}ms " .
171 "set: $set/$count {$setMs}ms " .
172 "get: $get/$count ({$getMs}ms) " .
173 "delete: $delete/$count ({$delMs}ms) " .
174 "incr: $incr/$count ({$incrMs}ms)\n"
182 private function benchmarkMultiKeyOpsImmediateBlocking(
BagOStuff $mcc, array $valueByKey ) {
183 $keys = array_keys( $valueByKey );
184 $iterations = count( $valueByKey );
186 $time_start = microtime(
true );
187 $mSetOk = $mcc->
setMulti( $valueByKey ) ?
'✓' :
'✗';
188 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
190 $time_start = microtime(
true );
192 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
194 foreach ( $found as $key => $value ) {
195 $mGetOk += ( $value === $valueByKey[$key] );
198 $time_start = microtime(
true );
200 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
202 $time_start = microtime(
true );
204 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
207 " setMulti (IB): $mSetOk {$mSetMs}ms " .
208 "getMulti (IB): $mGetOk/$iterations {$mGetMs}ms " .
209 "changeTTLMulti (IB): $mChangeTTLOk {$mChangeTTTMs}ms " .
210 "deleteMulti (IB): $mDelOk {$mDelMs}ms\n"
218 private function benchmarkMultiKeyOpsDeferredBlocking(
BagOStuff $mcc, array $valueByKey ) {
219 $keys = array_keys( $valueByKey );
220 $iterations = count( $valueByKey );
221 $flags = $mcc::WRITE_BACKGROUND;
223 $time_start = microtime(
true );
224 $mSetOk = $mcc->
setMulti( $valueByKey, 0, $flags ) ?
'✓' :
'✗';
225 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
227 $time_start = microtime(
true );
229 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
231 foreach ( $found as $key => $value ) {
232 $mGetOk += ( $value === $valueByKey[$key] );
235 $time_start = microtime(
true );
236 $mChangeTTLOk = $mcc->
changeTTLMulti( $keys, 3600, $flags ) ?
'✓' :
'✗';
237 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
239 $time_start = microtime(
true );
240 $mDelOk = $mcc->
deleteMulti( $keys, $flags ) ?
'✓' :
'✗';
241 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
244 " setMulti (DB): $mSetOk {$mSetMs}ms " .
245 "getMulti (DB): $mGetOk/$iterations {$mGetMs}ms " .
246 "changeTTLMulti (DB): $mChangeTTLOk {$mChangeTTTMs}ms " .
247 "deleteMulti (DB): $mDelOk {$mDelMs}ms\n"
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.
output( $out, $channel=null)
Throw some output to the user.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.