41 parent::__construct();
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.\""
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 );
55 $objectCaches = $config->get( MainConfigNames::ObjectCaches );
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 );
62 if ( !isset( $objectCaches[$cacheType] ) ) {
63 $this->
fatalError(
"No configured '$cacheType' cache" );
66 if ( $classOverride !==
null ) {
67 if ( !is_subclass_of( $classOverride, BagOStuff::class ) ) {
68 $this->
fatalError(
"Invalid class '$classOverride' for cache" );
70 $class = $classOverride;
72 $class = $objectCaches[$cacheType][
'class'];
75 if ( $server !==
null ) {
76 $servers = [ $server ];
79 $servers = $objectCaches[$cacheType][
'servers'] ?? [ null ];
83 $maxSrvLen = max( array_map(
'strlen', $servers ) );
85 $this->
output(
"Warming up connections to cache servers..." );
88 foreach ( $servers as $server ) {
89 $conf = $objectCaches[$cacheType];
90 if ( $server !==
null ) {
91 $conf[
'servers'] = [ $server ];
96 $cacheByServer[$host] =
new $class( $conf );
97 $cacheByServer[$host]->get(
'key' );
100 $this->
output(
"Single and batched operation profiling/test results:\n" );
103 for ( $i = 1; $i <= $iterations; $i++ ) {
104 $valueByKey[
"test$i"] =
'S' . str_pad( (
string)$i, 2048 );
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 );
119 private function benchmarkSingleKeyOps(
BagOStuff $mcc, array $valueByKey ) {
126 $keys = array_keys( $valueByKey );
127 $count = count( $valueByKey );
132 $time_start = microtime(
true );
133 foreach ( $valueByKey as $key => $value ) {
134 if ( $mcc->
add( $key, $value ) ) {
138 $addMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
140 $time_start = microtime(
true );
141 foreach ( $valueByKey as $key => $value ) {
142 if ( $mcc->
set( $key, $value ) ) {
146 $setMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
148 $time_start = microtime(
true );
149 foreach ( $valueByKey as $key => $value ) {
150 if ( $mcc->
get( $key ) === $value ) {
154 $getMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
156 $time_start = microtime(
true );
157 foreach ( $keys as $key ) {
158 if ( $mcc->
delete( $key ) ) {
162 $delMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
164 $time_start = microtime(
true );
165 foreach ( $keys as $index => $key ) {
166 if ( $mcc->
incrWithInit( $key, $mcc::TTL_INDEFINITE, $index ) === $index ) {
170 $incrMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
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"
185 private function benchmarkMultiKeyOpsImmediateBlocking(
BagOStuff $mcc, array $valueByKey ) {
186 $keys = array_keys( $valueByKey );
187 $iterations = count( $valueByKey );
189 $time_start = microtime(
true );
190 $mSetOk = $mcc->
setMulti( $valueByKey ) ?
'✓' :
'✗';
191 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
193 $time_start = microtime(
true );
195 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
197 foreach ( $found as $key => $value ) {
198 $mGetOk += ( $value === $valueByKey[$key] );
201 $time_start = microtime(
true );
203 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
205 $time_start = microtime(
true );
207 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
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"
221 private function benchmarkMultiKeyOpsDeferredBlocking(
BagOStuff $mcc, array $valueByKey ) {
222 $keys = array_keys( $valueByKey );
223 $iterations = count( $valueByKey );
224 $flags = $mcc::WRITE_BACKGROUND;
226 $time_start = microtime(
true );
227 $mSetOk = $mcc->
setMulti( $valueByKey, 0, $flags ) ?
'✓' :
'✗';
228 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
230 $time_start = microtime(
true );
232 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
234 foreach ( $found as $key => $value ) {
235 $mGetOk += ( $value === $valueByKey[$key] );
238 $time_start = microtime(
true );
239 $mChangeTTLOk = $mcc->
changeTTLMulti( $keys, 3600, $flags ) ?
'✓' :
'✗';
240 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
242 $time_start = microtime(
true );
243 $mDelOk = $mcc->
deleteMulti( $keys, $flags ) ?
'✓' :
'✗';
244 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
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"
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.