Newer
Older
Robert Douglass
committed
<?php
Robert Douglass
committed
/*
* Some unit tests used while developing. You may find them instructional.
* Place the memcachetests.php file in the root Drupal directory. Start memcache servers
* on localhost:11211 and localhost:11212
*/
// dummy implementations to keep memcache.inc happy
define('WATCHDOG_ERROR', 2);
function variable_get($name, $default) {
return $default;
}
function watchdog($type, $message, $severity = WATCHDOG_NOTICE, $link = NULL) {
}
Robert Douglass
committed
include_once './includes/memcache.inc';
print "<h1>Memcache configuration details</h1>";
print "<ul><li>memcache.allow_failover=". ini_get('memcache.allow_failover'). "</li>";
print "<li>memcache.max_failover_attempts=". ini_get('memcache.max_failover_attempts'). "</li>";
print "<li>memcache.chunk_size=". ini_get('memcache.chunk_size'). "</li>";
print "<li>memcache.default_port=". ini_get('memcache.default_port'). "</li>";
print "</ul>";
global $conf;
Robert Douglass
committed
// by not defining any $conf for memcache we're essentially saying "give me localhost:11211"
// and only the 'default' bin will be active.
Robert Douglass
committed
##############
// Begin Tests
##############
// Test 1. Connect to server and retrieve stats
###############################################
printHeader();
$mc = dmemcache_object();
formatStats($mc->getStats());
Robert Douglass
committed
$mc->close();
unset($mc);
Robert Douglass
committed
// Test 2. Set a number of keys and retrieve their values
#########################################################
printHeader();
Robert Douglass
committed
$mc = dmemcache_object();
Robert Douglass
committed
$keys = array('a', time(), 'http://www.robshouse.net/home/page?q=xyz&p=x', 'What about space?');
print '<ol>';
foreach ($keys as $key) {
testKeyValue($mc, $key, 'Hi Robert');
}
print '</ol>';
Robert Douglass
committed
$mc->close();
unset($mc);
Robert Douglass
committed
// Test 3. Set a number of PROBLEMATIC keys and retrieve their values
#####################################################################
printHeader();
Robert Douglass
committed
$mc = dmemcache_object();
Robert Douglass
committed
print '<ol>';
$key = ' ';
testKeyValue($mc, $key, 'Hi Dude');
$key = "\n";
testKeyValue($mc, $key, 'Hi Dude 2');
print '<li><em>space and line break different?</em>='. $mc->get(' ').'</li>';
print '</ol>';
Robert Douglass
committed
$mc->close();
unset($mc);
Robert Douglass
committed
// Test 4. Test flushing the $mc object from dmemcache_object
#############################################################
printHeader();
Robert Douglass
committed
$mc = dmemcache_object();
Robert Douglass
committed
formatStats($mc->getStats());
Robert Douglass
committed
$conf['memcache'][] = array(
'#servers' => array('localhost:11212'),
'#bins' => array('default'),
Robert Douglass
committed
);
Robert Douglass
committed
$mc->close();
unset($mc);
Robert Douglass
committed
$mc = dmemcache_object('default', TRUE);
formatStats($mc->getStats());
Robert Douglass
committed
$mc->close();
unset($mc);
Robert Douglass
committed
// Test 5. Confirm that space and line break are treated as the same character
##############################################################################
printHeader();
Robert Douglass
committed
$mc = dmemcache_object();
Robert Douglass
committed
$mc->set("\n", "This is a new line", FALSE, 5);
print '<ol>';
print '<li>'. $mc->get("\n"). '</li>';
print '<li>'. $mc->get(" "). '</li>';
print '</ol>';
Robert Douglass
committed
$mc->close();
unset($mc);
Robert Douglass
committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Test 6. Try out dmemcache_set and dmemcache_get
##################################################
printHeader();
$keys = array('a', time(), 'http://www.robshouse.net/home/page?q=xyz&p=x', 'What about space?');
print '<ol>';
foreach ($keys as $key) {
dtestKeyValue($key, 'Hi Robert 2');
}
print '</ol>';
// Test 7. See if newline and space are identical using dmemcache
// conclusion: the urlencode() fixes this problem.
#################################################################
printHeader();
dmemcache_set("\n", "This is a new line");
print '<ol>';
print '<li>'. dmemcache_get("\n"). '</li>';
print '<li>'. dmemcache_get(" "). '</li>';
print '</ol>';
// Test 8. See if addServer actually pools the server resources
###############################################################
printHeader();
// Set up $conf so that both available servers map to default
Robert Douglass
committed
$conf['memcache'][] = array(
'#servers' => array('localhost:11211', 'localhost:11212'),
'#bins' => array('default'),
Robert Douglass
committed
);
Robert Douglass
committed
Robert Douglass
committed
// clear the $mc object
dmemcache_object('default', TRUE);
Robert Douglass
committed
// make independent connections so we can display stats. These will be used in the next 3 tests.
Robert Douglass
committed
$mc1 = new Memcache;
$mc1->connect('localhost', 11211);
$mc2 = new Memcache;
$mc2->connect('localhost', 11212);
$last_key = $last_value = '';
Robert Douglass
committed
$time = microtime();
Robert Douglass
committed
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'Some very random thoughts about things in general'. $time;
dmemcache_set($last_key, $last_value, FALSE, 0);
if ($i % 1000 == 1) {
formatStats2($mc1, $mc2);
flush();
}
}
print "</table>";
Robert Douglass
committed
Robert Douglass
committed
// Test 9. Try using flush to clear servers
// Conclusion: It probably works, except that it doesn't actuall clear the memory.
// It only sets it as invalid so that it gets overwritten.
###########################################
printHeader();
formatStats2($mc1, $mc2, TRUE);
print "</table>";
dmemcache_flush();
formatStats2($mc1, $mc2, TRUE);
print "</table>";
Robert Douglass
committed
Robert Douglass
committed
// Test 10. See what the extended stats offer
#############################################
printHeader();
print "<pre>";
$types = array('reset', 'malloc', 'maps', 'slabs', 'items', 'sizes');
foreach ($types as $type) {
print "<h2>$type</h2>";
print "<h3>Server 1</h3>";
print_r($mc1->getExtendedStats($type));
print "<h3>Server 2</h3>";
print_r($mc2->getExtendedStats($type));
}
print "</pre>";
Robert Douglass
committed
// done with mc1 and mc2
$mc1->close();
$mc2->close();
unset($mc1);
unset($mc2);
Robert Douglass
committed
// Test 11. Test delete.
########################
printHeader();
dmemcache_set('delete me', 'Goodbye world');
print "<h2>". dmemcache_get('delete me'). "</h2>";
dmemcache_delete('delete me');
print "<h2>Nothing here ---->". dmemcache_get('delete me'). "<--</h2>";
Robert Douglass
committed
// Test 12. Save things to different bins
#########################################
printHeader();
$mc1 = new Memcache;
$mc1->connect('localhost', 11211);
$mc2 = new Memcache;
$mc2->connect('localhost', 11212);
// Set up two clusters with four bins
Robert Douglass
committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
$conf['memcache'][] = array(
'#servers' => array('localhost:11211'),
'#bins' => array('default', 'antwerp'),
);
$conf['memcache'][] = array(
'#servers' => array('localhost:11212'),
'#bins' => array('vancouver', 'barcelona'),
);
// flush the cluster cache
dmemcache_object('', TRUE);
$first = TRUE;
print "<h3>Bin: default</h3>";
$last_key = $last_value = '';
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'default '. $i;
dmemcache_set($last_key, $last_value, 20, 'default');
if ($i % 1000 == 1) {
$cluster = dmemcache_object('default');
print_r($cluster);
formatStats2($mc1, $mc2, $first);
$first = FALSE;
flush();
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'default'));
}
}
print "</table>";
print "<h4>Values for default</h4>";
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
foreach ($keyin_keyout as $values) {
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
}
print "</table>";
print "<h3>Bin: vancouver</h3>";
$first = TRUE;
$keyin_keyout = array();
$last_key = $last_value = '';
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'vancouver '. $i;
dmemcache_set($last_key, $last_value, 20, 'vancouver');
if ($i % 1000 == 1) {
$cluster = dmemcache_object('vancouver');
print_r($cluster);
formatStats2($mc1, $mc2, $first);
$first = FALSE;
flush();
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'vancouver'));
}
}
print "</table>";
print "<h4>Values for vancouver</h4>";
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
foreach ($keyin_keyout as $values) {
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
}
print "</table>";
print "<h3>Bin: barcelona</h3>";
$first = TRUE;
$keyin_keyout = array();
$last_key = $last_value = '';
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'barcelona '. $i;
dmemcache_set($last_key, $last_value, 20, 'barcelona');
if ($i % 1000 == 1) {
$cluster = dmemcache_object('barcelona');
print_r($cluster);
formatStats2($mc1, $mc2, $first);
$first = FALSE;
flush();
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'barcelona'));
}
}
print "</table>";
print "<h4>Values for barcelona</h4>";
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
foreach ($keyin_keyout as $values) {
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
}
print "</table>";
print "<h3>Bin: antwerp</h3>";
$first = TRUE;
$keyin_keyout = array();
$last_key = $last_value = '';
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'antwerp '. $i;
dmemcache_set($last_key, $last_value, 20, 'antwerp');
if ($i % 1000 == 1) {
$cluster = dmemcache_object('antwerp');
print_r($cluster);
formatStats2($mc1, $mc2, $first);
$first = FALSE;
flush();
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'antwerp'));
}
}
print "</table>";
print "<h4>Values for antwerp</h4>";
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
foreach ($keyin_keyout as $values) {
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
}
print "</table>";
$mc1->close();
$mc2->close();
unset($mc1);
unset($mc2);
// Test 13. View the globaldebug messages
#########################################
printHeader();
Robert Douglass
committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
###################
// Helper functions
###################
function testKeyValue($mc, $key, $value) {
$mc->set($key, $value, FALSE, 5);
printKeyValue($key, $mc->get($key));
}
function dtestKeyValue($key, $value, $bin = 'default') {
dmemcache_set($key, $value, 5, $bin);
printKeyValue($key, dmemcache_get($key));
}
function printKeyValue($key, $value) {
print '<li>'. $key. '='. $value. '</li>';
}
function formatStats($stats = array()) {
print '<ul>';
foreach ($stats as $name => $value) {
print '<li>'. $name. '='. $value. '</li>';
}
print '</ul>';
}
function printHeader() {
static $count = 1;
print "<a name='$count'><h2>Test ". $count++. "</h2></a>";
}
Robert Douglass
committed
function formatStats2($mc1, $mc2, $flush = FALSE) {
static $count, $first;
$stats1 = $mc1->getStats();
$stats2 = $mc2->getStats();
if ($flush) {
unset($first);
}
if (!isset($first)) {
$count = 1;
$first = FALSE;
print "<table border='1'><tr><th> </th><th>Server 1</th><th>Server 2</th></tr>";
}
print "<tr><td>$count</td><td>". $stats1['bytes']. "</td><td>". $stats2['bytes']. "</td></tr>";
$count++;
}