class Program
{
const int ItemCount = 100_0000;
const int NumberOfNodes = 10;
const int NumberOfReplicas = 200;
static void Main(string[] args)
{
var nodes = new List<Node>();
for (int i = 1; i <= NumberOfNodes; i++)
{
nodes.Add(new Node($"10.1.1.{i}"));
}
var serverCluster = new ConsistentHash<Node>(new MurmurHash3(4049661204), NumberOfReplicas, nodes);
TestAddOrRemove(serverCluster);
Statistics(nodes, serverCluster);
}
private static void TestAddOrRemove(ConsistentHash<Node> cluster)
{
var keys = new[] { "66778899", "hello world", "consistent hashing" };
cluster.FindNode(keys);
var newNode = new Node($"10.1.1.{NumberOfNodes + 1}");
cluster.AddToRing(newNode);
Console.WriteLine($"\nAfter added a new Node [{newNode}]:\n--------------------");
cluster.FindNode(keys);
cluster.RemoveFromRing(newNode);
Console.WriteLine($"\nAfter removed a Node [{newNode}]:\n--------------------");
cluster.FindNode(keys);
}
private static void Statistics(IList<Node> nodes, ConsistentHash<Node> cluster)
{
var statDict = new Dictionary<String, int>();
foreach (var n in nodes)
{
statDict.Add(n.IP, 0);
}
for (int i = 0; i < ItemCount; i++)
{
var node = cluster.GetNode($"key_{i}");
statDict[node.IP] += 1;
}
var mean = statDict.Values.Average();
var std = statDict.Values.StdDev();
Console.WriteLine(@$"
Algorithm Statistics:
{cluster.ToString()}
--------------------
Total: {ItemCount:N0}
Mean: {mean:N0}
StdDev: {std:F2}
");
foreach (var (k, v) in statDict)
{
Console.WriteLine("Node [{0}]: {1}", k, v);
}
}
//...
}
评论