領測軟件測試網
php一致性hash性能測試(flexihash/memcache/memcached)
一致性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測試
view source
print?
01 |
require_once 'flexihash.php' ; |
06 |
public $memcache = null; |
07 |
public $connectPool = null; |
09 |
public function __construct() |
11 |
$this ->hash = new Flexihash(); |
14 |
public function addServers( $servers ) |
16 |
foreach ( $servers as $server ) |
18 |
$node = $server [ 'host' ] . ':' . $server [ 'port' ]; |
19 |
$this ->connectPool[ $node ] = false; |
22 |
$this ->hash->addTargets( $targets ); |
25 |
public function set( $key , $value ) |
27 |
$nodes = $this ->hash->lookupList( $key , count ( $this ->connectPool ) ); |
28 |
foreach ( $nodes as $node ) |
30 |
if (! $this ->connectPool[ $node ]) |
32 |
$server = explode ( ':' , $node ); |
33 |
$this ->connectPool[ $node ] = @memcache_connect( $server [0], $server [1] ); |
35 |
if ( $this ->connectPool[ $node ]) |
37 |
if (memcache_set( $this ->connectPool[ $node ], $key , $value )) |
46 |
public function get( $key ) |
48 |
$nodes = $this ->hash->lookupList( $key , count ( $this ->connectPool ) ); |
49 |
foreach ( $nodes as $node ) |
51 |
if (! $this ->connectPool[ $node ]) |
53 |
$server = explode ( ':' , $node ); |
54 |
$this ->connectPool[ $node ] = @memcache_connect( $server [0], $server [1] ); |
56 |
if ( $this ->connectPool[ $node ]) |
58 |
if (memcache_get( $this ->connectPool[ $node ], $key )) |
測試代碼:
view source
print?
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 ); |
08 |
foreach ( $words as $word ) |
14 |
$inc = $mem ->set( $word , $word ); |
16 |
$et = microtime( true ) - $st ; |
17 |
echo "time used:" . $et ; |
文章來源于領測軟件測試網 http://www.k11sc111.com/