Memcache configuration details";
print "
- memcache.allow_failover=". ini_get('memcache.allow_failover'). "
";
print "- memcache.max_failover_attempts=". ini_get('memcache.max_failover_attempts'). "
";
print "- memcache.chunk_size=". ini_get('memcache.chunk_size'). "
";
print "- memcache.default_port=". ini_get('memcache.default_port'). "
";
print "
";
global $conf;
// by not defining any $conf for memcache we're essentially saying "give me localhost:11211"
// and only the 'default' bin will be active.
##############
// Begin Tests
##############
// Test 1. Connect to server and retrieve stats
###############################################
printHeader();
$mc = dmemcache_object();
formatStats($mc->getStats());
$mc->close();
unset($mc);
// Test 2. Set a number of keys and retrieve their values
#########################################################
printHeader();
$mc = dmemcache_object();
$keys = array('a', time(), 'http://www.robshouse.net/home/page?q=xyz&p=x', 'What about space?');
print '';
foreach ($keys as $key) {
testKeyValue($mc, $key, 'Hi Robert');
}
print '
';
$mc->close();
unset($mc);
// Test 3. Set a number of PROBLEMATIC keys and retrieve their values
#####################################################################
printHeader();
$mc = dmemcache_object();
print '';
$key = ' ';
testKeyValue($mc, $key, 'Hi Dude');
$key = "\n";
testKeyValue($mc, $key, 'Hi Dude 2');
print '- space and line break different?='. $mc->get(' ').'
';
print '
';
$mc->close();
unset($mc);
// Test 4. Test flushing the $mc object from dmemcache_object
#############################################################
printHeader();
$mc = dmemcache_object();
formatStats($mc->getStats());
unset($conf['memcache']);
$conf['memcache'][] = array(
'#servers' => array('localhost:11212'),
'#bins' => array('default'),
);
$mc->close();
unset($mc);
$mc = dmemcache_object('default', TRUE);
formatStats($mc->getStats());
$mc->close();
unset($mc);
// Test 5. Confirm that space and line break are treated as the same character
##############################################################################
printHeader();
$mc = dmemcache_object();
$mc->set("\n", "This is a new line", FALSE, 5);
print '';
print '- '. $mc->get("\n"). '
';
print '- '. $mc->get(" "). '
';
print '
';
$mc->close();
unset($mc);
// 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 '';
foreach ($keys as $key) {
dtestKeyValue($key, 'Hi Robert 2');
}
print '
';
// 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 '';
print '- '. dmemcache_get("\n"). '
';
print '- '. dmemcache_get(" "). '
';
print '
';
// Test 8. See if addServer actually pools the server resources
###############################################################
printHeader();
// Set up $conf so that both available servers map to default
unset($conf['memcache']);
$conf['memcache'][] = array(
'#servers' => array('localhost:11211', 'localhost:11212'),
'#bins' => array('default'),
);
// clear the $mc object
dmemcache_object('default', TRUE);
// make independent connections so we can display stats. These will be used in the next 3 tests.
$mc1 = new Memcache;
$mc1->connect('localhost', 11211);
$mc2 = new Memcache;
$mc2->connect('localhost', 11212);
$last_key = $last_value = '';
$time = microtime();
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 "";
// 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 "";
dmemcache_flush();
formatStats2($mc1, $mc2, TRUE);
print "";
// Test 10. See what the extended stats offer
#############################################
printHeader();
print "";
$types = array('reset', 'malloc', 'maps', 'slabs', 'items', 'sizes');
foreach ($types as $type) {
print "$type
";
print "Server 1
";
print_r($mc1->getExtendedStats($type));
print "Server 2
";
print_r($mc2->getExtendedStats($type));
}
print "
";
// done with mc1 and mc2
$mc1->close();
$mc2->close();
unset($mc1);
unset($mc2);
// Test 11. Test delete.
########################
printHeader();
dmemcache_set('delete me', 'Goodbye world');
print "". dmemcache_get('delete me'). "
";
dmemcache_delete('delete me');
print "Nothing here ---->". dmemcache_get('delete me'). "<--
";
// 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
unset($conf['memcache']);
$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 "Bin: default
";
$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 "";
print "Values for default
";
print 'Original | Cached |
';
foreach ($keyin_keyout as $values) {
print "". $values['orig']. " | ". $values['cache']. " |
";
}
print "
";
print "Bin: vancouver
";
$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 "";
print "Values for vancouver
";
print 'Original | Cached |
';
foreach ($keyin_keyout as $values) {
print "". $values['orig']. " | ". $values['cache']. " |
";
}
print "
";
print "Bin: barcelona
";
$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 "";
print "Values for barcelona
";
print 'Original | Cached |
';
foreach ($keyin_keyout as $values) {
print "". $values['orig']. " | ". $values['cache']. " |
";
}
print "
";
print "Bin: antwerp
";
$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 "";
print "Values for antwerp
";
print 'Original | Cached |
';
foreach ($keyin_keyout as $values) {
print "". $values['orig']. " | ". $values['cache']. " |
";
}
print "
";
$mc1->close();
$mc2->close();
unset($mc1);
unset($mc2);
// Test 13. View the globaldebug messages
#########################################
printHeader();
###################
// 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 ''. $key. '='. $value. '';
}
function formatStats($stats = array()) {
print '';
foreach ($stats as $name => $value) {
print '- '. $name. '='. $value. '
';
}
print '
';
}
function printHeader() {
static $count = 1;
print "Test ". $count++. "
";
}
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 " | Server 1 | Server 2 |
";
}
print "$count | ". $stats1['bytes']. " | ". $stats2['bytes']. " |
";
$count++;
}
?>