Last active
December 10, 2015 16:18
-
-
Save mia-0032/4460171 to your computer and use it in GitHub Desktop.
ConsistentHashingで本当に分散できているのかを検証するためのプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
require_once '../class/ConsistentHasher.php'; | |
$start_time = microtime(TRUE); | |
$nodes = array('1', '2', '3', '4'); | |
$another_nodes = array('1', '2', '3', '4', '5'); | |
$ids = range(1, 10000); | |
$hasher = new ConsistentHasher($nodes); | |
//結果を格納する配列を作る | |
$count_results = array(); | |
foreach ($nodes as $name) { | |
$count_results[$name] = 0; | |
} | |
$node_results = array(); | |
//実行 | |
foreach ($ids as $id) { | |
$name = $hasher->getNode($id); | |
$count_results[$name]++; | |
$node_results[$id] = $name; | |
} | |
//実行結果表示(個数) | |
echo "CountPerNode\n"; | |
foreach ($count_results as $name => $count) { | |
echo 'node' . $name . ':' . $count . "\n"; | |
} | |
echo "\n"; | |
echo "\n"; | |
//ここからノード増やしたときにどの程度変わるかチェック | |
$hit_count = 0; | |
$added_hasher = new ConsistentHasher($another_nodes); | |
foreach ($ids as $id) { | |
$added_name = $added_hasher->getNode($id); | |
if ($node_results[$id] === $added_name) { | |
$hit_count++; | |
} | |
} | |
echo 'node diff:' . (count($another_nodes) - count($nodes)) . ':' . $hit_count * 100 / count($ids) . "%\n"; | |
echo "\n"; | |
echo "\n"; | |
//比較用に単なる剰余のバージョンも計算しておく | |
$hit_count = 0; | |
$before_num = count($nodes); | |
$after_num = count($another_nodes); | |
foreach ($ids as $id) { | |
if ($id % $before_num === $id % $after_num) { | |
$hit_count++; | |
} | |
} | |
echo '(remainder)node diff:' . (count($another_nodes) - count($nodes)) . ':' . $hit_count * 100 / count($ids) . "%\n"; | |
echo "\n"; | |
echo "\n"; | |
//終了! | |
echo "Time:" . (microtime(TRUE) - $start_time) . "sec\n"; | |
echo "Memory:" . memory_get_peak_usage() / 1024 / 1024 . "MB\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment