public void MaxWeightHasherEvenDistributionTest()
        {
            Pseudorandom pseudo = new Pseudorandom();
            int trials = 10000000;
            MaxWeightHashing<string> hasher = CreateMaxWeightHasher();

            double expectedFraction = 1d / ((double) _nodeNames.Length);


            Dictionary<string, Int64> counts = new Dictionary<string, Int64>();
            foreach (string number in _nodeNames)
                counts[number] = 0;

            for (int i = 0; i < trials; i++)
            {
                string rand = pseudo.GetString(13);
                string word = hasher.FindMemberResponsible(rand);
                counts[word]++;
            }

            double greatestAdjustedError = 0;
            foreach (string word in _nodeNames)
            {
                double freq = ((double)counts[word]) / (double)trials;
                double error = freq - expectedFraction;
                double adjustedError = error / expectedFraction;
                if (adjustedError > greatestAdjustedError)
                    greatestAdjustedError = adjustedError;
                Assert.True(Math.Abs(adjustedError) < 0.005);
            }

            Console.Out.WriteLine("Highest adjusted error: {0}", greatestAdjustedError);
        }
Пример #2
0
        public void UniversalHashTestBias()
        {
            Pseudorandom pseudo = new Pseudorandom();
            UniversalHashFunction f = new UniversalHashFunction("Louis Tully as played by Rick Moranis!");
            ulong trials = 100000000;

            ulong[] bitCounts = new ulong[64];

            for (ulong trial = 0; trial < trials; trial++)
            {
                string randomString = pseudo.GetString(8);
                UInt64 supposedlyUnbiasedBits = f.Hash(randomString, UniversalHashFunction.MaximumNumberOfResultBitsAllowing32BiasedBits);
                for (int bit=0; bit < bitCounts.Length; bit++)
                {
                    if ((supposedlyUnbiasedBits & (0x8000000000000000ul >> bit)) != 0ul)
                        bitCounts[bit]++;
                }
            }

            double[] biases = bitCounts.Select(count => ( (0.5d - (((double)count) / ((double)trials)))) / 0.5d ).ToArray();

            /// The first 32 bits should be unbiased
            for (int bit = 0; bit < 32; bit++)
            {
                double bias = biases[bit];
                double biasAbs = Math.Abs(bias);
                Assert.True(biasAbs < 0.0005d);
            }

        }
Пример #3
0
        public void UniversalHashTestBias()
        {
            Pseudorandom          pseudo = new Pseudorandom();
            UniversalHashFunction f      = new UniversalHashFunction("Louis Tully as played by Rick Moranis!");
            ulong trials = 100000000;

            ulong[] bitCounts = new ulong[64];

            for (ulong trial = 0; trial < trials; trial++)
            {
                string randomString           = pseudo.GetString(8);
                UInt64 supposedlyUnbiasedBits = f.Hash(randomString, UniversalHashFunction.MaximumNumberOfResultBitsAllowing32BiasedBits);
                for (int bit = 0; bit < bitCounts.Length; bit++)
                {
                    if ((supposedlyUnbiasedBits & (0x8000000000000000ul >> bit)) != 0ul)
                    {
                        bitCounts[bit]++;
                    }
                }
            }

            double[] biases = bitCounts.Select(count => ((0.5d - (((double)count) / ((double)trials)))) / 0.5d).ToArray();

            /// The first 32 bits should be unbiased
            for (int bit = 0; bit < 32; bit++)
            {
                double bias    = biases[bit];
                double biasAbs = Math.Abs(bias);
                Assert.True(biasAbs < 0.0005d);
            }
        }
Пример #4
0
        public void HashRingEvenDistributionTest()
        {
            Pseudorandom pseudo = new Pseudorandom();
            int          trials = 10000000;
            ConsistentHashRing <string> ring         = CreateConsistentHashRingForTenValues();
            Dictionary <string, double> ringCoverage = ring.FractionalCoverage;

            double expectedFraction = 1d / 10d;

            foreach (string word in _oneToTenAsWords)
            {
                double share   = ringCoverage[word];
                double bias    = (expectedFraction - share) / expectedFraction;
                double absBias = Math.Abs(bias);
                Assert.True(bias < .15d);
                Assert.True(bias < .15d);
            }


            Dictionary <string, Int64>  counts      = new Dictionary <string, Int64>();
            Dictionary <string, double> frequencies = new Dictionary <string, double>();

            foreach (string number in _oneToTenAsWords)
            {
                counts[number] = 0;
            }

            for (int i = 0; i < trials; i++)
            {
                string rand = pseudo.GetString(13);
                string word = ring.FindMemberResponsible(rand);
                counts[word]++;
            }
            foreach (string word in _oneToTenAsWords)
            {
                double freq  = frequencies[word] = ((double)counts[word]) / (double)trials;
                double error = freq - ringCoverage[word];
                Assert.True(Math.Abs(error) < 0.01);
            }
        }
Пример #5
0
        public void MaxWeightHasherEvenDistributionTest()
        {
            Pseudorandom pseudo = new Pseudorandom();
            int          trials = 10000000;
            MaxWeightHashing <string> hasher = CreateMaxWeightHasher();

            double expectedFraction = 1d / ((double)_nodeNames.Length);


            Dictionary <string, Int64> counts = new Dictionary <string, Int64>();

            foreach (string number in _nodeNames)
            {
                counts[number] = 0;
            }

            for (int i = 0; i < trials; i++)
            {
                string rand = pseudo.GetString(13);
                string word = hasher.FindMemberResponsible(rand);
                counts[word]++;
            }

            double greatestAdjustedError = 0;

            foreach (string word in _nodeNames)
            {
                double freq          = ((double)counts[word]) / (double)trials;
                double error         = freq - expectedFraction;
                double adjustedError = error / expectedFraction;
                if (adjustedError > greatestAdjustedError)
                {
                    greatestAdjustedError = adjustedError;
                }
                Assert.True(Math.Abs(adjustedError) < 0.005);
            }

            Console.Out.WriteLine("Highest adjusted error: {0}", greatestAdjustedError);
        }
        public void HashRingEvenDistributionTest()
        {
            Pseudorandom pseudo = new Pseudorandom();
            int trials = 10000000;
            ConsistentHashRing<string> ring = CreateConsistentHashRingForTenValues();
            Dictionary<string,double> ringCoverage = ring.FractionalCoverage;

            double expectedFraction = 1d / 10d;

            foreach (string word in _oneToTenAsWords)
            {
                double share = ringCoverage[word];
                double bias = (expectedFraction - share) / expectedFraction;
                double absBias = Math.Abs(bias);
                Assert.True(bias < .15d);
                Assert.True(bias < .15d);
            }


            Dictionary<string, Int64> counts = new Dictionary<string, Int64>();
            Dictionary<string, double> frequencies = new Dictionary<string, double>();
            foreach (string number in _oneToTenAsWords)
                counts[number] = 0;

            for (int i = 0; i < trials; i++)
            {
                string rand = pseudo.GetString(13);
                string word = ring.FindMemberResponsible(rand);
                counts[word]++;
            }
            foreach (string word in _oneToTenAsWords)
            {
                double freq = frequencies[word] = ((double)counts[word]) / (double)trials;
                double error = freq - ringCoverage[word];
                Assert.True(Math.Abs(error) < 0.01);
            }

        }