public void HashedVsRandom(int realCardinality) { const int precision = 18; var hllRandom = new HyperLogLog(precision); for (var i = 0; i < realCardinality; i++) { var randomBytes = new byte[8]; RandomNumberGenerator.GetBytes(randomBytes); var hashValue = BitConverter.ToUInt64(randomBytes, 0); hllRandom.Add(hashValue); } var cardinalityEstimateRandom = hllRandom.Count(); var hllHashed = new HyperLogLog(precision); using (var hashAlgorithm = new SHA1CryptoServiceProvider()) { for (var i = 0; i < realCardinality; i++) { var randomBytes = new byte[12]; RandomNumberGenerator.GetBytes(randomBytes); var hashValue = HyperLogLog.Hash(hashAlgorithm, randomBytes); hllHashed.Add(hashValue); } } var cardinalityEstimateHashed = hllHashed.Count(); var percentageDifference = GetPercentageDifference(cardinalityEstimateRandom, cardinalityEstimateHashed); Assert.True(percentageDifference < ZeroFivePercent); }
public void HashConstantWithSHA1() { const ulong value = 0x2D51AF5C52FDE6B4ul; using (var hashAlgorithm = new SHA1CryptoServiceProvider()) { var hashValue = HyperLogLog.Hash(hashAlgorithm, BitConverter.GetBytes(value)); Assert.Equal(17851087020509344997ul, hashValue); } }
public void HashConstantWithMD5() { const ulong value = 0x2D51AF5C52FDE6B4ul; using (var hashAlgorithm = new MD5CryptoServiceProvider()) { var hashValue = HyperLogLog.Hash(hashAlgorithm, BitConverter.GetBytes(value)); Assert.Equal(16663394367412432550ul, hashValue); } }
public void TheHashedRandomTheory(int realCardinality) { const int precision = 18; var hll = new HyperLogLog(precision); using (var hashAlgorithm = new SHA1CryptoServiceProvider()) { for (var i = 0; i < realCardinality; i++) { var randomBytes = new byte[12]; RandomNumberGenerator.GetBytes(randomBytes); var hashValue = HyperLogLog.Hash(hashAlgorithm, randomBytes); hll.Add(hashValue); } } var cardinalityEstimate = hll.Count(); var percentageDifference = GetPercentageDifference(realCardinality, cardinalityEstimate); Assert.True(percentageDifference < OnePercent); }