| 1 |
<?php
|
| 2 |
// $Id: memcachetests.php,v 1.4.2.1 2007/05/11 06:12:17 robertDouglass Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
* Some unit tests used while developing. You may find them instructional.
|
| 6 |
* Place the memcachetests.php file in the root Drupal directory. Start memcache servers
|
| 7 |
* on localhost:11211 and localhost:11212
|
| 8 |
*/
|
| 9 |
|
| 10 |
// dummy implementations to keep memcache.inc happy
|
| 11 |
define('WATCHDOG_ERROR', 2);
|
| 12 |
function variable_get($name, $default) {
|
| 13 |
return $default;
|
| 14 |
}
|
| 15 |
function watchdog($type, $message, $severity = WATCHDOG_NOTICE, $link = NULL) {
|
| 16 |
}
|
| 17 |
|
| 18 |
|
| 19 |
include_once './includes/memcache.inc';
|
| 20 |
|
| 21 |
print "<h1>Memcache configuration details</h1>";
|
| 22 |
print "<ul><li>memcache.allow_failover=". ini_get('memcache.allow_failover'). "</li>";
|
| 23 |
print "<li>memcache.max_failover_attempts=". ini_get('memcache.max_failover_attempts'). "</li>";
|
| 24 |
print "<li>memcache.chunk_size=". ini_get('memcache.chunk_size'). "</li>";
|
| 25 |
print "<li>memcache.default_port=". ini_get('memcache.default_port'). "</li>";
|
| 26 |
print "</ul>";
|
| 27 |
|
| 28 |
global $conf;
|
| 29 |
|
| 30 |
// by not defining any $conf for memcache we're essentially saying "give me localhost:11211"
|
| 31 |
// and only the 'default' bin will be active.
|
| 32 |
|
| 33 |
##############
|
| 34 |
// Begin Tests
|
| 35 |
##############
|
| 36 |
|
| 37 |
// Test 1. Connect to server and retrieve stats
|
| 38 |
###############################################
|
| 39 |
printHeader();
|
| 40 |
$mc = dmemcache_object();
|
| 41 |
formatStats($mc->getStats());
|
| 42 |
$mc->close();
|
| 43 |
unset($mc);
|
| 44 |
|
| 45 |
// Test 2. Set a number of keys and retrieve their values
|
| 46 |
#########################################################
|
| 47 |
printHeader();
|
| 48 |
$mc = dmemcache_object();
|
| 49 |
$keys = array('a', time(), 'http://www.robshouse.net/home/page?q=xyz&p=x', 'What about space?');
|
| 50 |
|
| 51 |
print '<ol>';
|
| 52 |
foreach ($keys as $key) {
|
| 53 |
testKeyValue($mc, $key, 'Hi Robert');
|
| 54 |
}
|
| 55 |
print '</ol>';
|
| 56 |
$mc->close();
|
| 57 |
unset($mc);
|
| 58 |
|
| 59 |
// Test 3. Set a number of PROBLEMATIC keys and retrieve their values
|
| 60 |
#####################################################################
|
| 61 |
printHeader();
|
| 62 |
|
| 63 |
$mc = dmemcache_object();
|
| 64 |
print '<ol>';
|
| 65 |
$key = ' ';
|
| 66 |
testKeyValue($mc, $key, 'Hi Dude');
|
| 67 |
|
| 68 |
$key = "\n";
|
| 69 |
testKeyValue($mc, $key, 'Hi Dude 2');
|
| 70 |
|
| 71 |
print '<li><em>space and line break different?</em>='. $mc->get(' ').'</li>';
|
| 72 |
print '</ol>';
|
| 73 |
$mc->close();
|
| 74 |
unset($mc);
|
| 75 |
|
| 76 |
// Test 4. Test flushing the $mc object from dmemcache_object
|
| 77 |
#############################################################
|
| 78 |
printHeader();
|
| 79 |
|
| 80 |
$mc = dmemcache_object();
|
| 81 |
formatStats($mc->getStats());
|
| 82 |
unset($conf['memcache']);
|
| 83 |
$conf['memcache'][] = array(
|
| 84 |
'#servers' => array('localhost:11212'),
|
| 85 |
'#bins' => array('default'),
|
| 86 |
);
|
| 87 |
$mc->close();
|
| 88 |
unset($mc);
|
| 89 |
|
| 90 |
$mc = dmemcache_object('default', TRUE);
|
| 91 |
formatStats($mc->getStats());
|
| 92 |
$mc->close();
|
| 93 |
unset($mc);
|
| 94 |
|
| 95 |
|
| 96 |
// Test 5. Confirm that space and line break are treated as the same character
|
| 97 |
##############################################################################
|
| 98 |
printHeader();
|
| 99 |
$mc = dmemcache_object();
|
| 100 |
$mc->set("\n", "This is a new line", FALSE, 5);
|
| 101 |
print '<ol>';
|
| 102 |
print '<li>'. $mc->get("\n"). '</li>';
|
| 103 |
print '<li>'. $mc->get(" "). '</li>';
|
| 104 |
print '</ol>';
|
| 105 |
$mc->close();
|
| 106 |
unset($mc);
|
| 107 |
|
| 108 |
// Test 6. Try out dmemcache_set and dmemcache_get
|
| 109 |
##################################################
|
| 110 |
printHeader();
|
| 111 |
$keys = array('a', time(), 'http://www.robshouse.net/home/page?q=xyz&p=x', 'What about space?');
|
| 112 |
|
| 113 |
print '<ol>';
|
| 114 |
foreach ($keys as $key) {
|
| 115 |
dtestKeyValue($key, 'Hi Robert 2');
|
| 116 |
}
|
| 117 |
print '</ol>';
|
| 118 |
|
| 119 |
// Test 7. See if newline and space are identical using dmemcache
|
| 120 |
// conclusion: the urlencode() fixes this problem.
|
| 121 |
#################################################################
|
| 122 |
printHeader();
|
| 123 |
dmemcache_set("\n", "This is a new line");
|
| 124 |
print '<ol>';
|
| 125 |
print '<li>'. dmemcache_get("\n"). '</li>';
|
| 126 |
print '<li>'. dmemcache_get(" "). '</li>';
|
| 127 |
print '</ol>';
|
| 128 |
|
| 129 |
// Test 8. See if addServer actually pools the server resources
|
| 130 |
###############################################################
|
| 131 |
printHeader();
|
| 132 |
|
| 133 |
// Set up $conf so that both available servers map to default
|
| 134 |
unset($conf['memcache']);
|
| 135 |
$conf['memcache'][] = array(
|
| 136 |
'#servers' => array('localhost:11211', 'localhost:11212'),
|
| 137 |
'#bins' => array('default'),
|
| 138 |
);
|
| 139 |
|
| 140 |
|
| 141 |
// clear the $mc object
|
| 142 |
dmemcache_object('default', TRUE);
|
| 143 |
|
| 144 |
|
| 145 |
// make independent connections so we can display stats. These will be used in the next 3 tests.
|
| 146 |
$mc1 = new Memcache;
|
| 147 |
$mc1->connect('localhost', 11211);
|
| 148 |
$mc2 = new Memcache;
|
| 149 |
$mc2->connect('localhost', 11212);
|
| 150 |
|
| 151 |
$last_key = $last_value = '';
|
| 152 |
$time = microtime();
|
| 153 |
for ($i = 1; $i < 10001; $i++) {
|
| 154 |
$last_key = $time.$i. 'key';
|
| 155 |
$last_value = 'Some very random thoughts about things in general'. $time;
|
| 156 |
dmemcache_set($last_key, $last_value, FALSE, 0);
|
| 157 |
if ($i % 1000 == 1) {
|
| 158 |
formatStats2($mc1, $mc2);
|
| 159 |
flush();
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
print "</table>";
|
| 164 |
|
| 165 |
|
| 166 |
// Test 9. Try using flush to clear servers
|
| 167 |
// Conclusion: It probably works, except that it doesn't actuall clear the memory.
|
| 168 |
// It only sets it as invalid so that it gets overwritten.
|
| 169 |
###########################################
|
| 170 |
printHeader();
|
| 171 |
|
| 172 |
formatStats2($mc1, $mc2, TRUE);
|
| 173 |
print "</table>";
|
| 174 |
dmemcache_flush();
|
| 175 |
formatStats2($mc1, $mc2, TRUE);
|
| 176 |
print "</table>";
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
// Test 10. See what the extended stats offer
|
| 181 |
#############################################
|
| 182 |
printHeader();
|
| 183 |
|
| 184 |
print "<pre>";
|
| 185 |
$types = array('reset', 'malloc', 'maps', 'slabs', 'items', 'sizes');
|
| 186 |
foreach ($types as $type) {
|
| 187 |
print "<h2>$type</h2>";
|
| 188 |
print "<h3>Server 1</h3>";
|
| 189 |
print_r($mc1->getExtendedStats($type));
|
| 190 |
print "<h3>Server 2</h3>";
|
| 191 |
print_r($mc2->getExtendedStats($type));
|
| 192 |
}
|
| 193 |
|
| 194 |
print "</pre>";
|
| 195 |
|
| 196 |
// done with mc1 and mc2
|
| 197 |
$mc1->close();
|
| 198 |
$mc2->close();
|
| 199 |
unset($mc1);
|
| 200 |
unset($mc2);
|
| 201 |
|
| 202 |
// Test 11. Test delete.
|
| 203 |
########################
|
| 204 |
printHeader();
|
| 205 |
|
| 206 |
dmemcache_set('delete me', 'Goodbye world');
|
| 207 |
print "<h2>". dmemcache_get('delete me'). "</h2>";
|
| 208 |
dmemcache_delete('delete me');
|
| 209 |
print "<h2>Nothing here ---->". dmemcache_get('delete me'). "<--</h2>";
|
| 210 |
|
| 211 |
// Test 12. Save things to different bins
|
| 212 |
#########################################
|
| 213 |
printHeader();
|
| 214 |
|
| 215 |
$mc1 = new Memcache;
|
| 216 |
$mc1->connect('localhost', 11211);
|
| 217 |
$mc2 = new Memcache;
|
| 218 |
$mc2->connect('localhost', 11212);
|
| 219 |
|
| 220 |
// Set up two clusters with four bins
|
| 221 |
unset($conf['memcache']);
|
| 222 |
$conf['memcache'][] = array(
|
| 223 |
'#servers' => array('localhost:11211'),
|
| 224 |
'#bins' => array('default', 'antwerp'),
|
| 225 |
);
|
| 226 |
$conf['memcache'][] = array(
|
| 227 |
'#servers' => array('localhost:11212'),
|
| 228 |
'#bins' => array('vancouver', 'barcelona'),
|
| 229 |
);
|
| 230 |
|
| 231 |
// flush the cluster cache
|
| 232 |
dmemcache_object('', TRUE);
|
| 233 |
|
| 234 |
$first = TRUE;
|
| 235 |
|
| 236 |
print "<h3>Bin: default</h3>";
|
| 237 |
$last_key = $last_value = '';
|
| 238 |
$time = microtime();
|
| 239 |
for ($i = 1; $i < 10001; $i++) {
|
| 240 |
$last_key = $time.$i. 'key';
|
| 241 |
$last_value = 'default '. $i;
|
| 242 |
dmemcache_set($last_key, $last_value, 20, 'default');
|
| 243 |
if ($i % 1000 == 1) {
|
| 244 |
$cluster = dmemcache_object('default');
|
| 245 |
print_r($cluster);
|
| 246 |
formatStats2($mc1, $mc2, $first);
|
| 247 |
$first = FALSE;
|
| 248 |
flush();
|
| 249 |
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'default'));
|
| 250 |
}
|
| 251 |
}
|
| 252 |
print "</table>";
|
| 253 |
print "<h4>Values for default</h4>";
|
| 254 |
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
|
| 255 |
foreach ($keyin_keyout as $values) {
|
| 256 |
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
|
| 257 |
}
|
| 258 |
print "</table>";
|
| 259 |
|
| 260 |
print "<h3>Bin: vancouver</h3>";
|
| 261 |
$first = TRUE;
|
| 262 |
$keyin_keyout = array();
|
| 263 |
$last_key = $last_value = '';
|
| 264 |
$time = microtime();
|
| 265 |
for ($i = 1; $i < 10001; $i++) {
|
| 266 |
$last_key = $time.$i. 'key';
|
| 267 |
$last_value = 'vancouver '. $i;
|
| 268 |
dmemcache_set($last_key, $last_value, 20, 'vancouver');
|
| 269 |
if ($i % 1000 == 1) {
|
| 270 |
$cluster = dmemcache_object('vancouver');
|
| 271 |
print_r($cluster);
|
| 272 |
formatStats2($mc1, $mc2, $first);
|
| 273 |
$first = FALSE;
|
| 274 |
flush();
|
| 275 |
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'vancouver'));
|
| 276 |
}
|
| 277 |
}
|
| 278 |
print "</table>";
|
| 279 |
print "<h4>Values for vancouver</h4>";
|
| 280 |
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
|
| 281 |
foreach ($keyin_keyout as $values) {
|
| 282 |
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
|
| 283 |
}
|
| 284 |
print "</table>";
|
| 285 |
|
| 286 |
print "<h3>Bin: barcelona</h3>";
|
| 287 |
$first = TRUE;
|
| 288 |
$keyin_keyout = array();
|
| 289 |
$last_key = $last_value = '';
|
| 290 |
$time = microtime();
|
| 291 |
for ($i = 1; $i < 10001; $i++) {
|
| 292 |
$last_key = $time.$i. 'key';
|
| 293 |
$last_value = 'barcelona '. $i;
|
| 294 |
dmemcache_set($last_key, $last_value, 20, 'barcelona');
|
| 295 |
if ($i % 1000 == 1) {
|
| 296 |
$cluster = dmemcache_object('barcelona');
|
| 297 |
print_r($cluster);
|
| 298 |
formatStats2($mc1, $mc2, $first);
|
| 299 |
$first = FALSE;
|
| 300 |
flush();
|
| 301 |
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'barcelona'));
|
| 302 |
}
|
| 303 |
}
|
| 304 |
print "</table>";
|
| 305 |
print "<h4>Values for barcelona</h4>";
|
| 306 |
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
|
| 307 |
foreach ($keyin_keyout as $values) {
|
| 308 |
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
|
| 309 |
}
|
| 310 |
print "</table>";
|
| 311 |
|
| 312 |
|
| 313 |
print "<h3>Bin: antwerp</h3>";
|
| 314 |
$first = TRUE;
|
| 315 |
$keyin_keyout = array();
|
| 316 |
$last_key = $last_value = '';
|
| 317 |
$time = microtime();
|
| 318 |
for ($i = 1; $i < 10001; $i++) {
|
| 319 |
$last_key = $time.$i. 'key';
|
| 320 |
$last_value = 'antwerp '. $i;
|
| 321 |
dmemcache_set($last_key, $last_value, 20, 'antwerp');
|
| 322 |
if ($i % 1000 == 1) {
|
| 323 |
$cluster = dmemcache_object('antwerp');
|
| 324 |
print_r($cluster);
|
| 325 |
formatStats2($mc1, $mc2, $first);
|
| 326 |
$first = FALSE;
|
| 327 |
flush();
|
| 328 |
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'antwerp'));
|
| 329 |
}
|
| 330 |
}
|
| 331 |
print "</table>";
|
| 332 |
print "<h4>Values for antwerp</h4>";
|
| 333 |
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
|
| 334 |
foreach ($keyin_keyout as $values) {
|
| 335 |
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
|
| 336 |
}
|
| 337 |
print "</table>";
|
| 338 |
|
| 339 |
$mc1->close();
|
| 340 |
$mc2->close();
|
| 341 |
unset($mc1);
|
| 342 |
unset($mc2);
|
| 343 |
|
| 344 |
// Test 13. View the globaldebug messages
|
| 345 |
#########################################
|
| 346 |
printHeader();
|
| 347 |
|
| 348 |
|
| 349 |
###################
|
| 350 |
// Helper functions
|
| 351 |
###################
|
| 352 |
|
| 353 |
function testKeyValue($mc, $key, $value) {
|
| 354 |
$mc->set($key, $value, FALSE, 5);
|
| 355 |
printKeyValue($key, $mc->get($key));
|
| 356 |
}
|
| 357 |
|
| 358 |
function dtestKeyValue($key, $value, $bin = 'default') {
|
| 359 |
dmemcache_set($key, $value, 5, $bin);
|
| 360 |
printKeyValue($key, dmemcache_get($key));
|
| 361 |
}
|
| 362 |
|
| 363 |
function printKeyValue($key, $value) {
|
| 364 |
print '<li>'. $key. '='. $value. '</li>';
|
| 365 |
}
|
| 366 |
|
| 367 |
function formatStats($stats = array()) {
|
| 368 |
print '<ul>';
|
| 369 |
foreach ($stats as $name => $value) {
|
| 370 |
print '<li>'. $name. '='. $value. '</li>';
|
| 371 |
}
|
| 372 |
print '</ul>';
|
| 373 |
}
|
| 374 |
|
| 375 |
function printHeader() {
|
| 376 |
static $count = 1;
|
| 377 |
print "<a name='$count'><h2>Test ". $count++. "</h2></a>";
|
| 378 |
}
|
| 379 |
|
| 380 |
function formatStats2($mc1, $mc2, $flush = FALSE) {
|
| 381 |
static $count, $first;
|
| 382 |
|
| 383 |
$stats1 = $mc1->getStats();
|
| 384 |
$stats2 = $mc2->getStats();
|
| 385 |
|
| 386 |
if ($flush) {
|
| 387 |
unset($first);
|
| 388 |
}
|
| 389 |
|
| 390 |
if (!isset($first)) {
|
| 391 |
$count = 1;
|
| 392 |
$first = FALSE;
|
| 393 |
print "<table border='1'><tr><th> </th><th>Server 1</th><th>Server 2</th></tr>";
|
| 394 |
}
|
| 395 |
print "<tr><td>$count</td><td>". $stats1['bytes']. "</td><td>". $stats2['bytes']. "</td></tr>";
|
| 396 |
$count++;
|
| 397 |
}
|
| 398 |
?>
|