private KetamaHashingAlgorithm_FNV1a32Bit hashingAlgorithm; //The hashing algorithm object to actually do our hashing /// <summary> /// Initializing the KetamaContinuum object. /// </summary> /// <param name="serverIP">A string containing server 'IP:port/weight' values (e.g. '10.10.10.10:1234/5') /// Can separate multiple IP:port/weight inputs via commas, (e.g. '10.10.10.10:1234/5, 20.20.20.20:2345/6') in order /// to pass in multiple servers to connect to at once.</param> public KetamaContinuum(string serverIP = null) { _continuum = new SortedDictionary<uint, string>(); hashingAlgorithm = new KetamaHashingAlgorithm_FNV1a32Bit(); ketamaServers = new KetamaServers(); if (serverIP != null) { SyncServerConections(serverIP); } }
public void ConfirmHashingToCorrectValues() { string servers = "10.10.10.10:6379,20.20.20.20:6379,30.30.30.30:6379,40.40.40.40:6379"; KetamaContinuum ketamaContinuum = new KetamaContinuum(servers); KetamaHashingAlgorithm_FNV1a32Bit ketamaHashingAlgorithm = new KetamaHashingAlgorithm_FNV1a32Bit(); string key = "ThisIsTheBestHash!!!!"; string server = ketamaContinuum.FindServerForKey(key); uint hash = ketamaHashingAlgorithm.GetHashFromString(key); //Console.WriteLine("Hash value for {0} is: {1}, which is found on server: {2}", key, hash, server); Assert.AreEqual((uint)hash, (uint)1916906286); hash = ketamaHashingAlgorithm.GetHashFromString(key); Assert.AreEqual((uint)hash, (uint)1916906286); //Doing it twice to ensure hashing the same string multiple times isn't coming up with different results key = "thisistheWORSTHASH:( :( :("; server = ketamaContinuum.FindServerForKey(key); hash = ketamaHashingAlgorithm.GetHashFromString(key); //Console.WriteLine("Hash value for {0} is: {1}, which is found on server: {2}", key, hash, server); Assert.AreEqual((uint)hash, (uint)3993883897); hash = ketamaHashingAlgorithm.GetHashFromString(key); Assert.AreEqual((uint)hash, (uint)3993883897); //Doing it twice to ensure hashing the same string multiple times isn't coming up with different results key = "Stevieweevie"; server = ketamaContinuum.FindServerForKey(key); hash = ketamaHashingAlgorithm.GetHashFromString(key); //Console.WriteLine("Hash value for {0} is: {1}, which is found on server: {2}", key, hash, server); Assert.AreEqual((uint)hash, (uint)3692210458); hash = ketamaHashingAlgorithm.GetHashFromString(key); Assert.AreEqual((uint)hash, (uint)3692210458); //Doing it twice to ensure hashing the same string multiple times isn't coming up with different results }
private KetamaHashingAlgorithm_FNV1a32Bit hashingAlgorithm; //The hashing algorithm object to actually do our hashing /// <summary> /// Initializing the KetamaContinuum object. /// </summary> /// <param name="serverIP">A string containing server 'IP:port/weight' values (e.g. '10.10.10.10:1234/5') /// Can separate multiple IP:port/weight inputs via commas, (e.g. '10.10.10.10:1234/5, 20.20.20.20:2345/6') in order /// to pass in multiple servers to connect to at once.</param> public KetamaContinuum(string serverIP = null) { _continuum = new SortedDictionary <uint, string>(); hashingAlgorithm = new KetamaHashingAlgorithm_FNV1a32Bit(); ketamaServers = new KetamaServers(); if (serverIP != null) { SyncServerConections(serverIP); } }
//[TestMethod] //Uncomment to run a timing test (i.e., see how long it takes for all this to run) public void HashTimingTest() { KetamaHashingAlgorithm_FNV1a32Bit ketamaHashingAlgorithm = new KetamaHashingAlgorithm_FNV1a32Bit(); Stopwatch totalTime = new Stopwatch(); string stringKey = "test_string_value"; for (int total = 10; total <= 10000; total *= 10) { totalTime.Reset(); totalTime.Start(); for (int i = 0; i < total; i++) { ketamaHashingAlgorithm.GetHashFromString(stringKey + "_" + i); } totalTime.Stop(); Debug.WriteLine("Time for " + total + " hashes: " + totalTime.ElapsedMilliseconds.ToString()); } }
public void HashComparisonTest() { string servers = ""; using (StreamReader reader = new StreamReader("../../../../ketama.servers")) { string line; while ((line = reader.ReadLine()) != null) { servers = string.Concat(servers, ",", line); } servers = servers.Substring(1); //Remove the comma at the head of the string, a side effect of the concatination } if (servers == "") { Assert.Fail("No servers provided."); } KetamaContinuum ketamaContinuum = new KetamaContinuum(servers); List<uint> hashes = ketamaContinuum.GetAllHashes(); string key; uint hash; KetamaHashingAlgorithm_FNV1a32Bit ketamaHashingAlgorithm = new KetamaHashingAlgorithm_FNV1a32Bit(); using (StreamWriter writer = new StreamWriter("../../../../csharp_test.out")) { for (int i = 0; i < 100; i++) { key = "aab" + i.ToString(); hash = (uint)ketamaHashingAlgorithm.GetHashFromString(key); Debug.WriteLine(ketamaContinuum.FindServerForKey(key) + " - " + key); writer.WriteLine(ketamaContinuum.FindServerForKey(key) + " - " + key); } } }