您的位置:首页 > 数据库 > Memcache

php一致性hash性能测试(flexihash/memcache/memcached)

2014-04-16 15:12 423 查看
一致性hash的使用在PHP中有三种选择分别是原生的memcache扩展,memcached扩展,还有一个是网上比较流行的flexihash类。

最近有项目需要使用flexihash类操作memcacheq,想看看,单纯使用php的flexihash一致性hash,分布均匀程度,性能差多少。

php一致性hash类下载地址:http://code.google.com/p/flexihash/

测试环境:I7 四核 LINUX FEDORA 使用linux英文词库作为测试用例 memcached开启4个线程

测试结果:



其中,单节点指的是,在只有一个节点工作情况下的,测试结果。

小结

如上所示,就memcache扩展与memcached扩展比较来看,在当全部节点工作正常 的时候,测试条件memcached略快,但基本可以忽略不计。当只有单节点正常工作的时候,memcached扩展性能比 memcache快,我想可能是因为,memcached扩展在检测到连接无效的时候,没有再进行连接测试,直接将数据hash到连接有效的节点。当然这 个只是猜测,需要看源码才能理解。

48万条数据操作,使用flexihash做一致性hash与使用扩展一致性hash,在 hash这个过程中,速度仍旧在一个数量级上,大约是使用扩展速度的一半,其效率可以接受。在分布均匀性上,两个扩展分布基本比较均匀,在使用 flexihash不使用虚拟节点时候,分布非常不均匀,在使用16个虚拟节点后,分布均匀性已经接近扩展了。在使用虚拟节点后, set速度相比较没使用时候略慢,get操作反而变快。

下面给出测试源码

flexihash一致性hash测试

require_once 'flexihash.php';
Class FMemcache {

public $hash = null;
public $memcache = null;
public $connectPool = null;

public function __construct() {
$this - >hash = new Flexihash();
}

public function addServers($servers) {
foreach($servers as $server) {
$node = $server['host'].':'.$server['port'];
$this - >connectPool[$node] = false;
$targets[] = $node;
}
$this - >hash - >addTargets($targets);
}

public function set($key, $value) {
$nodes = $this - >hash - >lookupList($key, count($this - >connectPool));
foreach($nodes as $node) {
if (!$this - >connectPool[$node]) {
$server = explode(':', $node);
$this - >connectPool[$node] = @memcache_connect($server[0], $server[1]);
}
if ($this - >connectPool[$node]) {
if (memcache_set($this - >connectPool[$node], $key, $value)) {
return true;
}
}
}
return false;
}

public function get($key) {
$nodes = $this - >hash - >lookupList($key, count($this - >connectPool));
foreach($nodes as $node) {
if (!$this - >connectPool[$node]) {
$server = explode(':', $node);
$this - >connectPool[$node] = @memcache_connect($server[0], $server[1]);
}
if ($this - >connectPool[$node]) {
if (memcache_get($this - >connectPool[$node], $key)) {
return true;
}
}
}
return false;
}

}


测试示例:

require_once 'flexihash_memcache.php';
require_once 'Config.php';
$st = microtime( true );
$mem = new FMemcache();
$mem->addServers( $tt_server_pool );
$wordAmount = 0;
$getCount = 0;
foreach ($words as $word)
{
if (empty( $word ))
{
continue;
}
$inc = $mem->set( $word, $word );
}
$et = microtime( true ) - $st;
echo "time used:" . $et;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: