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 );
115 private function benchmarkSingleKeyOps(
BagOStuff $mcc, array $valueByKey ) {
122 $keys = array_keys( $valueByKey );
123 $count = count( $valueByKey );
128 $time_start = microtime(
true );
129 foreach ( $valueByKey as $key => $value ) {
130 if ( $mcc->
add( $key, $value ) ) {
134 $addMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
136 $time_start = microtime(
true );
137 foreach ( $valueByKey as $key => $value ) {
138 if ( $mcc->
set( $key, $value ) ) {
142 $setMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
144 $time_start = microtime(
true );
145 foreach ( $valueByKey as $key => $value ) {
146 if ( $mcc->
get( $key ) === $value ) {
150 $getMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
152 $time_start = microtime(
true );
153 foreach ( $keys as $key ) {
154 if ( $mcc->
delete( $key ) ) {
158 $delMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
160 $time_start = microtime(
true );
161 foreach ( $keys as $index => $key ) {
162 if ( $mcc->
incrWithInit( $key, $mcc::TTL_INDEFINITE, $index ) === $index ) {
166 $incrMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
169 " add: $add/$count {$addMs}ms " .
170 "set: $set/$count {$setMs}ms " .
171 "get: $get/$count ({$getMs}ms) " .
172 "delete: $delete/$count ({$delMs}ms) " .
173 "incr: $incr/$count ({$incrMs}ms)\n"
177 private function benchmarkMultiKeyOpsImmediateBlocking(
BagOStuff $mcc, array $valueByKey ) {
178 $keys = array_keys( $valueByKey );
179 $iterations = count( $valueByKey );
181 $time_start = microtime(
true );
182 $mSetOk = $mcc->
setMulti( $valueByKey ) ?
'✓' :
'✗';
183 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
185 $time_start = microtime(
true );
187 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
189 foreach ( $found as $key => $value ) {
190 $mGetOk += ( $value === $valueByKey[$key] );
193 $time_start = microtime(
true );
195 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
197 $time_start = microtime(
true );
199 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
202 " setMulti (IB): $mSetOk {$mSetMs}ms " .
203 "getMulti (IB): $mGetOk/$iterations {$mGetMs}ms " .
204 "changeTTLMulti (IB): $mChangeTTLOk {$mChangeTTTMs}ms " .
205 "deleteMulti (IB): $mDelOk {$mDelMs}ms\n"
209 private function benchmarkMultiKeyOpsDeferredBlocking(
BagOStuff $mcc, array $valueByKey ) {
210 $keys = array_keys( $valueByKey );
211 $iterations = count( $valueByKey );
212 $flags = $mcc::WRITE_BACKGROUND;
214 $time_start = microtime(
true );
215 $mSetOk = $mcc->
setMulti( $valueByKey, 0, $flags ) ?
'✓' :
'✗';
216 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
218 $time_start = microtime(
true );
220 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
222 foreach ( $found as $key => $value ) {
223 $mGetOk += ( $value === $valueByKey[$key] );
226 $time_start = microtime(
true );
227 $mChangeTTLOk = $mcc->
changeTTLMulti( $keys, 3600, $flags ) ?
'✓' :
'✗';
228 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
230 $time_start = microtime(
true );
231 $mDelOk = $mcc->
deleteMulti( $keys, $flags ) ?
'✓' :
'✗';
232 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
235 " setMulti (DB): $mSetOk {$mSetMs}ms " .
236 "getMulti (DB): $mGetOk/$iterations {$mGetMs}ms " .
237 "changeTTLMulti (DB): $mChangeTTLOk {$mChangeTTTMs}ms " .
238 "deleteMulti (DB): $mDelOk {$mDelMs}ms\n"
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.