Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 137
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
McTest
0.00% covered (danger)
0.00%
0 / 134
0.00% covered (danger)
0.00%
0 / 5
992
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
90
 benchmarkSingleKeyOps
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
132
 benchmarkMultiKeyOpsImmediateBlocking
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
30
 benchmarkMultiKeyOpsDeferredBlocking
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Makes several 'set', 'incr' and 'get' requests on every memcached
4 * server and shows a report.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25use MediaWiki\MainConfigNames;
26
27require_once __DIR__ . '/Maintenance.php';
28
29/**
30 * Maintenance script that  makes several 'set', 'incr' and 'get' requests
31 * on every memcached server and shows a report.
32 *
33 * @ingroup Maintenance
34 */
35class McTest extends Maintenance {
36    public function __construct() {
37        parent::__construct();
38        $this->addDescription(
39            "Makes several operation requests on every cache server and shows a report.\n" .
40            "This tests both per-key and batched *Multi() methods as well as WRITE_BACKGROUND.\n" .
41            "\"IB\" means \"immediate blocking\" and \"DB\" means \"deferred blocking.\""
42        );
43        $this->addOption( 'cache', 'Use servers from this $wgObjectCaches store', true, true );
44        $this->addOption( 'class', 'Override the store "class" parameter', false, true );
45        $this->addOption( 'i', 'Number of iterations', false, true );
46        $this->addArg( 'server[:port]', 'Cache server to test, with optional port', false );
47    }
48
49    public function execute() {
50        $config = $this->getConfig();
51        $objectCaches = $config->get( MainConfigNames::ObjectCaches );
52
53        $cacheType = $this->getOption( 'cache', $config->get( MainConfigNames::MainCacheType ) );
54        $iterations = $this->getOption( 'i', 100 );
55        $classOverride = $this->getOption( 'class' );
56        $server = $this->getArg( 0 );
57
58        if ( !isset( $objectCaches[$cacheType] ) ) {
59            $this->fatalError( "No configured '$cacheType' cache" );
60        }
61
62        if ( $classOverride !== null ) {
63            if ( !is_subclass_of( $classOverride, BagOStuff::class ) ) {
64                $this->fatalError( "Invalid class '$classOverride' for cache" );
65            }
66            $class = $classOverride;
67        } else {
68            $class = $objectCaches[$cacheType]['class'];
69        }
70
71        if ( $server !== null ) {
72            $servers = [ $server ];
73        } else {
74            // Note that some caches, like apcu, do not have a server list
75            $servers = $objectCaches[$cacheType]['servers'] ?? [ null ];
76        }
77
78        // Use longest server string for output alignment
79        $maxSrvLen = max( array_map( 'strlen', $servers ) );
80
81        $this->output( "Warming up connections to cache servers..." );
82        /** @var BagOStuff[] $cacheByServer */
83        $cacheByServer = [];
84        foreach ( $servers as $server ) {
85            $conf = $objectCaches[$cacheType];
86            if ( $server !== null ) {
87                $conf['servers'] = [ $server ];
88                $host = $server;
89            } else {
90                $host = 'localhost';
91            }
92            $cacheByServer[$host] = new $class( $conf );
93            $cacheByServer[$host]->get( 'key' );
94        }
95        $this->output( "done\n" );
96        $this->output( "Single and batched operation profiling/test results:\n" );
97
98        $valueByKey = [];
99        for ( $i = 1; $i <= $iterations; $i++ ) {
100            $valueByKey["test$i"] = 'S' . str_pad( (string)$i, 2048 );
101        }
102
103        foreach ( $cacheByServer as $host => $mcc ) {
104            $this->output( str_pad( $host, $maxSrvLen ) . "\n" );
105            $this->benchmarkSingleKeyOps( $mcc, $valueByKey );
106            $this->benchmarkMultiKeyOpsImmediateBlocking( $mcc, $valueByKey );
107            $this->benchmarkMultiKeyOpsDeferredBlocking( $mcc, $valueByKey );
108        }
109    }
110
111    /**
112     * @param BagOStuff $mcc
113     * @param array $valueByKey
114     */
115    private function benchmarkSingleKeyOps( BagOStuff $mcc, array $valueByKey ) {
116        $add = 0;
117        $set = 0;
118        $incr = 0;
119        $get = 0;
120        $delete = 0;
121
122        $keys = array_keys( $valueByKey );
123        $count = count( $valueByKey );
124
125        // Clear out any old values
126        $mcc->deleteMulti( $keys );
127
128        $time_start = microtime( true );
129        foreach ( $valueByKey as $key => $value ) {
130            if ( $mcc->add( $key, $value ) ) {
131                $add++;
132            }
133        }
134        $addMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
135
136        $time_start = microtime( true );
137        foreach ( $valueByKey as $key => $value ) {
138            if ( $mcc->set( $key, $value ) ) {
139                $set++;
140            }
141        }
142        $setMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
143
144        $time_start = microtime( true );
145        foreach ( $valueByKey as $key => $value ) {
146            if ( $mcc->get( $key ) === $value ) {
147                $get++;
148            }
149        }
150        $getMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
151
152        $time_start = microtime( true );
153        foreach ( $keys as $key ) {
154            if ( $mcc->delete( $key ) ) {
155                $delete++;
156            }
157        }
158        $delMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
159
160        $time_start = microtime( true );
161        foreach ( $keys as $index => $key ) {
162            if ( $mcc->incrWithInit( $key, $mcc::TTL_INDEFINITE, $index ) === $index ) {
163                $incr++;
164            }
165        }
166        $incrMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
167
168        $this->output(
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"
174        );
175    }
176
177    /**
178     * @param BagOStuff $mcc
179     * @param array $valueByKey
180     */
181    private function benchmarkMultiKeyOpsImmediateBlocking( BagOStuff $mcc, array $valueByKey ) {
182        $keys = array_keys( $valueByKey );
183        $iterations = count( $valueByKey );
184
185        $time_start = microtime( true );
186        $mSetOk = $mcc->setMulti( $valueByKey ) ? '✓' : '✗';
187        $mSetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
188
189        $time_start = microtime( true );
190        $found = $mcc->getMulti( $keys );
191        $mGetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
192        $mGetOk = 0;
193        foreach ( $found as $key => $value ) {
194            $mGetOk += ( $value === $valueByKey[$key] );
195        }
196
197        $time_start = microtime( true );
198        $mChangeTTLOk = $mcc->changeTTLMulti( $keys, 3600 ) ? '✓' : '✗';
199        $mChangeTTTMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
200
201        $time_start = microtime( true );
202        $mDelOk = $mcc->deleteMulti( $keys ) ? '✓' : '✗';
203        $mDelMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
204
205        $this->output(
206            " setMulti (IB): $mSetOk {$mSetMs}ms   " .
207            "getMulti (IB): $mGetOk/$iterations {$mGetMs}ms   " .
208            "changeTTLMulti (IB): $mChangeTTLOk {$mChangeTTTMs}ms   " .
209            "deleteMulti (IB): $mDelOk {$mDelMs}ms\n"
210        );
211    }
212
213    /**
214     * @param BagOStuff $mcc
215     * @param array $valueByKey
216     */
217    private function benchmarkMultiKeyOpsDeferredBlocking( BagOStuff $mcc, array $valueByKey ) {
218        $keys = array_keys( $valueByKey );
219        $iterations = count( $valueByKey );
220        $flags = $mcc::WRITE_BACKGROUND;
221
222        $time_start = microtime( true );
223        $mSetOk = $mcc->setMulti( $valueByKey, 0, $flags ) ? '✓' : '✗';
224        $mSetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
225
226        $time_start = microtime( true );
227        $found = $mcc->getMulti( $keys );
228        $mGetMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
229        $mGetOk = 0;
230        foreach ( $found as $key => $value ) {
231            $mGetOk += ( $value === $valueByKey[$key] );
232        }
233
234        $time_start = microtime( true );
235        $mChangeTTLOk = $mcc->changeTTLMulti( $keys, 3600, $flags ) ? '✓' : '✗';
236        $mChangeTTTMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
237
238        $time_start = microtime( true );
239        $mDelOk = $mcc->deleteMulti( $keys, $flags ) ? '✓' : '✗';
240        $mDelMs = intval( 1e3 * ( microtime( true ) - $time_start ) );
241
242        $this->output(
243            " setMulti (DB): $mSetOk {$mSetMs}ms   " .
244            "getMulti (DB): $mGetOk/$iterations {$mGetMs}ms   " .
245            "changeTTLMulti (DB): $mChangeTTLOk {$mChangeTTTMs}ms   " .
246            "deleteMulti (DB): $mDelOk {$mDelMs}ms\n"
247        );
248    }
249}
250
251$maintClass = McTest::class;
252require_once RUN_MAINTENANCE_IF_MAIN;