php一致性hash性能测试(flexihash/memcache/memcached)
发表于:2011-02-09来源:作者:点击数:
标签:
php一致性hash 性能测试 (flexihash/memcache/memcached) 一致性hash的使用在 PHP 中有三种选择分别是原生的memcache扩展,memcached扩展,还有一个是网上比较流行的flexihash类。 最近有项目需要使用flexihash类操作memcacheq,想看看,单纯使用php的flexih
php一致性hash
一致性hash的使用在
最近有项目需要使用flexihash类操作memcacheq,想看看,单纯使用php的flexihash一致性hash,分布均匀程度,性能差多少。
php一致性hash类
测试环境:I7 四核 LINUX FEDORA 使用
测试结果:
其中,单节点指的是,在只有一个节点工作情况下的,测试结果。
小结
如上所示,就memcache扩展与memcached扩展比较来看,在当全部节点工作正常的时候,测试条件memcached略快,但基本可以忽略不计。当只有单节点正常工作的时候,memcached扩展性能比 memcache快,我想可能是因为,memcached扩展在检测到连接无效的时候,没有再进行连接测试,直接将数据hash到连接有效的节点。当然这个只是猜测,需要看源码才能理解。
48万条数据操作,使用flexihash做一致性hash与使用扩展一致性hash,在hash这个过程中,速度仍旧在一个数量级上,大约是使用扩展速度的一半,其效率可以接受。在分布均匀性上,两个扩展分布基本比较均匀,在使用flexihash不使用虚拟节点时候,分布非常不均匀,在使用16个虚拟节点后,分布均匀性已经接近扩展了。在使用虚拟节点后, set速度相比较没使用时候略慢,get操作反而变快。
下面给出测试源码
flexihash一致性hash测试
view sourceprint?
01
require_once 'flexihash.php';
02
Class FMemcache
03
{
04
05
public $hash = null;
06
public $memcache = null;
07
public $connectPool = null;
08
09
public function __construct()
10
{
11
$this->hash =new Flexihash();
12
}
13
14
public function addServers($servers )
15
{
16
foreach ($servers as $server)
17
{
18
$node =$server['host'] .':' .$server['port'];
19
$this->connectPool[$node] = false;
20
$targets[] =$node;
21
}
22
$this->hash->addTargets($targets );
23
}
24
25
public function set($key,$value )
26
{
27
$nodes =$this->hash->lookupList($key,count($this->connectPool ) );
28
foreach ($nodes as $node)
29
{
30
if (!$this->connectPool[$node])
31
{
32
$server =explode(':',$node );
33
$this->connectPool[$node] = @memcache_connect($server[0],$server[1] );
34
}
35
if ($this->connectPool[$node])
36
{
37
if (memcache_set($this->connectPool[$node],$key,$value ))
38
{
39
return true;
40
}
41
}
42
}
43
return false;
44
}
45
46
public function get($key )
47
{
48
$nodes =$this->hash->lookupList($key,count($this->connectPool ) );
49
foreach ($nodes as $node)
50
{
51
if (!$this->connectPool[$node])
52
{
53
$server =explode(':',$node );
54
$this->connectPool[$node] = @memcache_connect($server[0],$server[1] );
55
}
56
if ($this->connectPool[$node])
57
{
58
if (memcache_get($this->connectPool[$node],$key ))
59
{
60
return true;
61
}
62
}
63
}
64
return false;
65
}
66
67
}
测试代码:
view sourceprint?
01
require_once 'flexihash_memcache.php';
02
require_once 'Config.php';
03
$st = microtime( true );
04
$mem =new FMemcache();
05
$mem->addServers($tt_server_pool );
06
$wordAmount = 0;
07
$getCount = 0;
08
foreach ($words as $word)
09
{
10
if (empty($word ))
11
{
12
continue;
13
}
14
$inc =$mem->set($word,$word );
15
}
16
$et = microtime( true ) -$st;
17
echo "time used:" .$et;