public function testHopeRemovingTargetDoesNotChangeMuchWithCrc32Hasher() { $hashSpace = new Flexihash(new Crc32Hasher()); foreach (range(1, $this->_targets) as $i) { $hashSpace->addTarget("target{$i}"); } $results1 = array(); foreach (range(1, $this->_lookups) as $i) { $results1[$i] = $hashSpace->lookup("t{$i}"); } $hashSpace->removeTarget("target1"); $results2 = array(); foreach (range(1, $this->_lookups) as $i) { $results2[$i] = $hashSpace->lookup("t{$i}"); } $differences = 0; foreach (range(1, $this->_lookups) as $i) { if ($results1[$i] !== $results2[$i]) { $differences++; } } $percent = round($differences / $this->_lookups * 100); $this->assertEquals(9.0, $percent); echo "\nConsistentHash: {$percent}% of lookups changed " . "after removing 1 of {$this->_targets} targets\n"; }
public function testFallbackPrecedenceWhenServerRemoved() { $mockHasher = new MockHasher(); $hashSpace = new Flexihash($mockHasher, 1); $mockHasher->setHashValue(10); $hashSpace->addTarget('t1'); $mockHasher->setHashValue(20); $hashSpace->addTarget('t2'); $mockHasher->setHashValue(30); $hashSpace->addTarget('t3'); $mockHasher->setHashValue(15); $this->assertEquals($hashSpace->lookup('resource'), 't2'); $this->assertEquals($hashSpace->lookupList('resource', 3), ['t2', 't3', 't1']); $hashSpace->removeTarget('t2'); $this->assertEquals($hashSpace->lookup('resource'), 't3'); $this->assertEquals($hashSpace->lookupList('resource', 3), ['t3', 't1']); $hashSpace->removeTarget('t3'); $this->assertEquals($hashSpace->lookup('resource'), 't1'); $this->assertEquals($hashSpace->lookupList('resource', 3), ['t1']); }
public function testHopeRemovingTargetDoesNotChangeMuchWithCrc32Hasher() { $hashSpace = new Flexihash(new Crc32Hasher()); foreach (range(1, $this->targets) as $i) { $hashSpace->addTarget("target{$i}"); } $results1 = []; foreach (range(1, $this->lookups) as $i) { $results1[$i] = $hashSpace->lookup("t{$i}"); } $hashSpace->removeTarget('target1'); $results2 = []; foreach (range(1, $this->lookups) as $i) { $results2[$i] = $hashSpace->lookup("t{$i}"); } $differences = 0; foreach (range(1, $this->lookups) as $i) { if ($results1[$i] !== $results2[$i]) { ++$differences; } } $percent = round($differences / $this->lookups * 100); $this->dump("ConsistentHash: {$percent}% of lookups changed " . "after removing 1 of {$this->targets} targets"); }