memcached分布式hash策略测试


版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan

 

配合memcache监控程序,测试memcache分布式,文章参考了网上某位牛人的文章。

测试crc32 fnv standard consistent 组合情况下的item分布情况及添加新服务器对现有item命中率的影响。

 

先开memcached服务器

#!/bin/bash
##############################
# MyZ 2009-07-12
# File:memcached_12.sh
##############################
port=11212
for (( i=11212; i<11224; i++ ))
do
 /usr/local/bin/memcached -u root -p $i -m 10 &
done

./memcached.sh

 

结论
使用memcache这种客户端下的方法保证分配均匀的基础下,存货率做高的是crc32+ consistent。

 

测试结果

//crc32 standard
192.168.1.101:11212 => items:9812
192.168.1.101:11213 => items:10153
192.168.1.101:11214 => items:9895
192.168.1.101:11215 => items:9985
192.168.1.101:11216 => items:10206
192.168.1.101:11217 => items:9958
192.168.1.101:11218 => items:10159
192.168.1.101:11219 => items:9941
192.168.1.101:11220 => items:9924
192.168.1.101:11221 => items:9967
192.168.1.101:11222 => items:0%
192.168.1.101:11223 => items:0%

 

//fnv standard
192.168.1.101:11212 => items:9911
192.168.1.101:11213 => items:9978
192.168.1.101:11214 => items:9928
192.168.1.101:11215 => items:10053
192.168.1.101:11216 => items:9952
192.168.1.101:11217 => items:10055
192.168.1.101:11218 => items:10013
192.168.1.101:11219 => items:10089
192.168.1.101:11220 => items:9947
192.168.1.101:11221 => items:10074
192.168.1.101:11222 => items:0
192.168.1.101:11223 => items:0

 

//crc32 consistent
192.168.1.101:11212 => items:12466
192.168.1.101:11213 => items:10175
192.168.1.101:11214 => items:8436
192.168.1.101:11215 => items:9118
192.168.1.101:11216 => items:11757
192.168.1.101:11217 => items:9208
192.168.1.101:11218 => items:10343
192.168.1.101:11219 => items:8417
192.168.1.101:11220 => items:8716
192.168.1.101:11221 => items:11364
192.168.1.101:11222 => items:0
192.168.1.101:11223 => items:0

 

//fnv consistent
192.168.1.101:11212 => items:9014
192.168.1.101:11213 => items:13178
192.168.1.101:11214 => items:10053
192.168.1.101:11215 => items:4302
192.168.1.101:11216 => items:10875
192.168.1.101:11217 => items:4906
192.168.1.101:11218 => items:15593
192.168.1.101:11219 => items:9993
192.168.1.101:11220 => items:8093
192.168.1.101:11221 => items:13993
192.168.1.101:11222 => items:0
192.168.1.101:11223 => items:0

 

 

用于检测memcache的程序配置部分

for ($i = 11212; $i < 11222; $i++) {
 $MEMCACHE_SERVERS[] = '192.168.1.101:'.$i; // add more as an array
}
$MEMCACHE_SERVERS[] = '192.168.1.101:11222'; // add more as an array
$MEMCACHE_SERVERS[] = '192.168.1.101:11223'; // add more as an array

 

测试代码

 

  1. <?php  
  2. /** 
  3.  * @name test.php 
  4.  * @date Thu Jul 12 21:18:59 CST 2009 
  5.  * @copyright 马永占(MyZ) 
  6.  * @author 马永占(MyZ)  
  7.  * @link http://blog.csdn.net/mayongzhan/ 
  8.  */  
  9. set_time_limit(0);  
  10.   
  11. //crc32 standard  
  12. //ini_set('memcache.hash_function','crc32');  
  13. //ini_set('memcache.hash_strategy','standard');  
  14. //crc32 consistent  
  15. ini_set('memcache.hash_function','crc32');  
  16. ini_set('memcache.hash_strategy','consistent');  
  17. //fnv standard  
  18. //ini_set('memcache.hash_function','fnv');  
  19. //ini_set('memcache.hash_strategy','standard');  
  20. //fnv consistent  
  21. //ini_set('memcache.hash_function','fnv');  
  22. //ini_set('memcache.hash_strategy','consistent');  
  23.   
  24. $memcache_server_ip = '192.168.1.101';  
  25. for ($i = 11212; $i < 11222; $i++) {  
  26.  $memcache_servers_ports[] = $i;  
  27. }  
  28. $memcache_servers_ports_add[] = 11222;  
  29. $memcache_servers_ports_add[] = 11223;  
  30.   
  31. $mem = new Memcache;  
  32. //add servers  
  33. foreach ($memcache_servers_ports as $value) {  
  34.  $mem->addServer($memcache_server_ip$value);  
  35. }  
  36. //set items  
  37. for($i = 0; $i < 100000; $i++) {  
  38.  $mem->set($i$i, 0, 3600);  
  39. }  
  40. //add 2 new servers  
  41. foreach ($memcache_servers_ports_add as $value) {  
  42.  $mem->addServer($memcache_server_ip$value);  
  43. }  
  44. //get items  
  45. for($i = 0; $i < 100000; $i++) {  
  46.  $mem->get($i);  
  47. }  
  48. $status = $mem->getExtendedStats();  
  49. foreach ($status as $key=>$value) {  
  50.  echo $key   
  51.  . ' => items:'   
  52.  . $value['curr_items']   
  53.  . '<br />';  
  54. }  
  55. $mem->close();  
  56. ?>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值