27 parent::__construct();
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.\""
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 );
41 $objectCaches = $config->get( MainConfigNames::ObjectCaches );
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 );
48 if ( !isset( $objectCaches[$cacheType] ) ) {
49 $this->
fatalError(
"No configured '$cacheType' cache" );
52 if ( $classOverride !==
null ) {
53 if ( !is_subclass_of( $classOverride, BagOStuff::class ) ) {
54 $this->
fatalError(
"Invalid class '$classOverride' for cache" );
56 $class = $classOverride;
58 $class = $objectCaches[$cacheType][
'class'];
61 if ( $server !==
null ) {
62 $servers = [ $server ];
65 $servers = $objectCaches[$cacheType][
'servers'] ?? [ null ];
69 $maxSrvLen = max( array_map(
'strlen', $servers ) );
71 $this->
output(
"Warming up connections to cache servers..." );
74 foreach ( $servers as $server ) {
75 $conf = $objectCaches[$cacheType];
76 if ( $server !==
null ) {
77 $conf[
'servers'] = [ $server ];
82 $cacheByServer[$host] =
new $class( $conf );
83 $cacheByServer[$host]->get(
'key' );
86 $this->
output(
"Single and batched operation profiling/test results:\n" );
89 for ( $i = 1; $i <= $iterations; $i++ ) {
90 $valueByKey[
"test$i"] =
'S' . str_pad( (
string)$i, 2048 );
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 );
101 private function benchmarkSingleKeyOps(
BagOStuff $mcc, array $valueByKey ) {
108 $keys = array_keys( $valueByKey );
109 $count = count( $valueByKey );
114 $time_start = microtime(
true );
115 foreach ( $valueByKey as $key => $value ) {
116 if ( $mcc->
add( $key, $value ) ) {
120 $addMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
122 $time_start = microtime(
true );
123 foreach ( $valueByKey as $key => $value ) {
124 if ( $mcc->
set( $key, $value ) ) {
128 $setMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
130 $time_start = microtime(
true );
131 foreach ( $valueByKey as $key => $value ) {
132 if ( $mcc->
get( $key ) === $value ) {
136 $getMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
138 $time_start = microtime(
true );
139 foreach ( $keys as $key ) {
140 if ( $mcc->
delete( $key ) ) {
144 $delMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
146 $time_start = microtime(
true );
147 foreach ( $keys as $index => $key ) {
148 if ( $mcc->
incrWithInit( $key, $mcc::TTL_INDEFINITE, $index ) === $index ) {
152 $incrMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
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"
163 private function benchmarkMultiKeyOpsImmediateBlocking(
BagOStuff $mcc, array $valueByKey ) {
164 $keys = array_keys( $valueByKey );
165 $iterations = count( $valueByKey );
167 $time_start = microtime(
true );
168 $mSetOk = $mcc->
setMulti( $valueByKey ) ?
'✓' :
'✗';
169 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
171 $time_start = microtime(
true );
173 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
175 foreach ( $found as $key => $value ) {
176 $mGetOk += ( $value === $valueByKey[$key] );
179 $time_start = microtime(
true );
181 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
183 $time_start = microtime(
true );
185 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
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"
195 private function benchmarkMultiKeyOpsDeferredBlocking(
BagOStuff $mcc, array $valueByKey ) {
196 $keys = array_keys( $valueByKey );
197 $iterations = count( $valueByKey );
198 $flags = $mcc::WRITE_BACKGROUND;
200 $time_start = microtime(
true );
201 $mSetOk = $mcc->
setMulti( $valueByKey, 0, $flags ) ?
'✓' :
'✗';
202 $mSetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
204 $time_start = microtime(
true );
206 $mGetMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
208 foreach ( $found as $key => $value ) {
209 $mGetOk += ( $value === $valueByKey[$key] );
212 $time_start = microtime(
true );
213 $mChangeTTLOk = $mcc->
changeTTLMulti( $keys, 3600, $flags ) ?
'✓' :
'✗';
214 $mChangeTTTMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
216 $time_start = microtime(
true );
217 $mDelOk = $mcc->
deleteMulti( $keys, $flags ) ?
'✓' :
'✗';
218 $mDelMs = intval( 1e3 * ( microtime(
true ) - $time_start ) );
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"
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.