Ejemplo n.º 1
0
        public void CountWithoutAdd()
        {
            var hll            = new HyperLogLog(18);
            var estimatedCount = hll.Count();

            Assert.Equal(189083, estimatedCount);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        public void Add5()
        {
            var hll = new HyperLogLog(18);

            hll.Add(5);
            var estimatedCount = hll.Count();

            Assert.Equal(189084, estimatedCount);
        }
Ejemplo n.º 4
0
        public void AddMaxULong()
        {
            var hll = new HyperLogLog(18);

            hll.Add(ulong.MaxValue);
            var estimatedCount = hll.Count();

            Assert.Equal(189084, estimatedCount);
        }
Ejemplo n.º 5
0
        public void AddTwiceDoesNotChangeCount()
        {
            var hll1 = new HyperLogLog(18);

            hll1.Add(ulong.MaxValue);
            hll1.Add(ulong.MaxValue);
            var estimatedCount1 = hll1.Count();

            var hll2 = new HyperLogLog(18);

            hll2.Add(ulong.MaxValue);
            var estimatedCount2 = hll2.Count();

            Assert.Equal(estimatedCount1, estimatedCount2);
        }
        private void benchmarkCount(int registers)
        {
            var n     = 100000;
            var words = Words.Dictionary(0);
            var m     = (uint)Math.Pow(2, registers);

            var h = new HyperLogLog(m);

            foreach (var word in words)
            {
                h.Add(Encoding.ASCII.GetBytes(word));
            }

            for (int i = 0; i < n; i++)
            {
                h.Count();
            }
        }
Ejemplo n.º 7
0
        public void TheRandomTheory(int realCardinality)
        {
            const int precision = 18;

            var hll = new HyperLogLog(precision);

            for (var i = 0; i < realCardinality; i++)
            {
                var randomBytes = new byte[8];
                RandomNumberGenerator.GetBytes(randomBytes);
                var hashValue = BitConverter.ToUInt64(randomBytes, 0);
                hll.Add(hashValue);
            }

            var cardinalityEstimate = hll.Count();

            var percentageDifference = GetPercentageDifference(realCardinality, cardinalityEstimate);

            Assert.True(percentageDifference < OnePercent);
        }
        private void testHyperLogLog(int n, int lowB, int highB)
        {
            var words  = Words.Dictionary(n);
            var bad    = 0;
            var nWords = (UInt64)words.LongLength;

            var options = new ParallelOptions();

            options.MaxDegreeOfParallelism = 4;
            Parallel.For(lowB, highB, options, i =>
            {
                var m = (uint)Math.Pow(2, i);

                HyperLogLog h = null;
                try
                {
                    h = new HyperLogLog(m);
                }
                catch (Exception)
                {
                    Assert.Fail(string.Format("Can't make HyperLogLog({0})", m));
                }

                foreach (var word in words)
                {
                    h.Add(Encoding.ASCII.GetBytes(word));
                }

                var expectedError = 1.04 / Math.Sqrt(m);
                var actualError   = Math.Abs(this.geterror(nWords, h.Count()));

                if (actualError > expectedError)
                {
                    bad++;
                    //Assert.Fail(string.Format("Expected: {0}, Actual: {1}", expectedError, actualError));
                }
            });
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
 string IStreamObserver.Inspect()
 {
     return(hyperLogLog.Count().ToString());
 }