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 testHashDistributionWithCrc32Hasher() { $hashSpace = new Flexihash(new Crc32Hasher()); foreach (range(1, $this->_targets) as $i) { $hashSpace->addTarget("target{$i}"); } $results = array(); foreach (range(1, $this->_lookups) as $i) { $results[$i] = $hashSpace->lookup("t{$i}"); } $distribution = array(); foreach ($hashSpace->getAllTargets() as $target) { $distribution[$target] = count(array_keys($results, $target)); } echo sprintf("\nDistribution of %d lookups per target (min/max/median/avg): %d/%d/%d/%d \n", $this->_lookups / $this->_targets, min($distribution), max($distribution), round($this->_median($distribution)), round(array_sum($distribution) / count($distribution))); }