40 parent::__construct();
41 $this->addDescription(
42 "Makes several operation requests on every cache server and shows a report.\n" .
43 "This tests both per-key and batched *Multi() methods as well as WRITE_BACKGROUND.\n" .
44 "\"IB\" means \"immediate blocking\" and \"DB\" means \"deferred blocking.\""
46 $this->addOption(
'cache',
'Use servers from this $wgObjectCaches store',
true,
true );
47 $this->addOption(
'class',
'Override the store "class" parameter',
false,
true );
48 $this->addOption(
'i',
'Number of iterations',
false,
true );
49 $this->addArg(
'server[:port]',
'Cache server to test, with optional port',
false );
53 $config = $this->getConfig();
54 $objectCaches = $config->get( MainConfigNames::ObjectCaches );
56 $cacheType = $this->getOption(
'cache', $config->get( MainConfigNames::MainCacheType ) );
57 $iterations = $this->getOption(
'i', 100 );
58 $classOverride = $this->getOption(
'class' );
59 $server = $this->getArg( 0 );
61 if ( !isset( $objectCaches[$cacheType] ) ) {
62 $this->fatalError(
"No configured '$cacheType' cache" );
65 if ( $classOverride !==
null ) {
66 if ( !is_subclass_of( $classOverride, BagOStuff::class ) ) {
67 $this->fatalError(
"Invalid class '$classOverride' for cache" );
69 $class = $classOverride;
71 $class = $objectCaches[$cacheType][
'class'];
74 if ( $server !==
null ) {
75 $servers = [ $server ];
78 $servers = $objectCaches[$cacheType][
'servers'] ?? [ null ];
82 $maxSrvLen = max( array_map(
'strlen', $servers ) );
84 $this->output(
"Warming up connections to cache servers..." );
87 foreach ( $servers as $server ) {
88 $conf = $objectCaches[$cacheType];
89 if ( $server !==
null ) {
90 $conf[
'servers'] = [ $server ];
95 $cacheByServer[$host] =
new $class( $conf );
96 $cacheByServer[$host]->get(
'key' );
98 $this->output(
"done\n" );
99 $this->output(
"Single and batched operation profiling/test results:\n" );
102 for ( $i = 1; $i <= $iterations; $i++ ) {
103 $valueByKey[
"test$i"] =
'S' . str_pad( (
string)$i, 2048 );
106 foreach ( $cacheByServer as $host => $mcc ) {
107 $this->output( str_pad( $host, $maxSrvLen ) .
"\n" );
108 $this->benchmarkSingleKeyOps( $mcc, $valueByKey );
109 $this->benchmarkMultiKeyOpsImmediateBlocking( $mcc, $valueByKey );
110 $this->benchmarkMultiKeyOpsDeferredBlocking( $mcc, $valueByKey );
118 private function benchmarkSingleKeyOps(
BagOStuff $mcc, array $valueByKey ) {
125 $keys = array_keys( $valueByKey );
126 $count = count( $valueByKey );
131 $time_start = microtime(
true );
132 foreach ( $valueByKey as $key => $value ) {
133 if ( $mcc->
add( $key, $value ) ) {
137 $addMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
139 $time_start = microtime(
true );
140 foreach ( $valueByKey as $key => $value ) {
141 if ( $mcc->
set( $key, $value ) ) {
145 $setMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
147 $time_start = microtime(
true );
148 foreach ( $valueByKey as $key => $value ) {
149 if ( $mcc->
get( $key ) === $value ) {
153 $getMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
155 $time_start = microtime(
true );
156 foreach ( $keys as $key ) {
157 if ( $mcc->
delete( $key ) ) {
161 $delMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
163 $time_start = microtime(
true );
164 foreach ( $keys as $index => $key ) {
165 if ( $mcc->
incrWithInit( $key, $mcc::TTL_INDEFINITE, $index ) === $index ) {
169 $incrMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
172 " add: $add/$count {$addMs}ms " .
173 "set: $set/$count {$setMs}ms " .
174 "get: $get/$count ({$getMs}ms) " .
175 "delete: $delete/$count ({$delMs}ms) " .
176 "incr: $incr/$count ({$incrMs}ms)\n"
184 private function benchmarkMultiKeyOpsImmediateBlocking(
BagOStuff $mcc, array $valueByKey ) {
185 $keys = array_keys( $valueByKey );
186 $iterations = count( $valueByKey );
188 $time_start = microtime(
true );
189 $mSetOk = $mcc->
setMulti( $valueByKey ) ?
'✓' :
'✗';
190 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
192 $time_start = microtime(
true );
194 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
196 foreach ( $found as $key => $value ) {
197 $mGetOk += ( $value === $valueByKey[$key] );
200 $time_start = microtime(
true );
202 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
204 $time_start = microtime(
true );
206 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
209 " setMulti (IB): $mSetOk {$mSetMs}ms " .
210 "getMulti (IB): $mGetOk/$iterations {$mGetMs}ms " .
211 "changeTTLMulti (IB): $mChangeTTLOk {$mChangeTTTMs}ms " .
212 "deleteMulti (IB): $mDelOk {$mDelMs}ms\n"
220 private function benchmarkMultiKeyOpsDeferredBlocking(
BagOStuff $mcc, array $valueByKey ) {
221 $keys = array_keys( $valueByKey );
222 $iterations = count( $valueByKey );
223 $flags = $mcc::WRITE_BACKGROUND;
225 $time_start = microtime(
true );
226 $mSetOk = $mcc->
setMulti( $valueByKey, 0, $flags ) ?
'✓' :
'✗';
227 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
229 $time_start = microtime(
true );
231 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
233 foreach ( $found as $key => $value ) {
234 $mGetOk += ( $value === $valueByKey[$key] );
237 $time_start = microtime(
true );
238 $mChangeTTLOk = $mcc->
changeTTLMulti( $keys, 3600, $flags ) ?
'✓' :
'✗';
239 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
241 $time_start = microtime(
true );
242 $mDelOk = $mcc->
deleteMulti( $keys, $flags ) ?
'✓' :
'✗';
243 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
246 " setMulti (DB): $mSetOk {$mSetMs}ms " .
247 "getMulti (DB): $mGetOk/$iterations {$mGetMs}ms " .
248 "changeTTLMulti (DB): $mChangeTTLOk {$mChangeTTTMs}ms " .
249 "deleteMulti (DB): $mDelOk {$mDelMs}ms\n"