Ejemplo n.º 1
0
        private void btnTestHashesFromFile_Click(object sender, EventArgs e)
        {
            string filename = FormHelper.OpenFileDlg(FormHelper.FiletypeFilters.SequenceFiles);

            if (string.IsNullOrWhiteSpace(filename))
            {
                return;
            }

            ShowWorkingAnimation();

            var testHashingElements = Task.Factory.StartNew(() =>
            {
                TimeSpan filterAddTimeTotal = TimeSpan.Zero;

                Stopwatch totalRuntimeTimer = Stopwatch.StartNew();
                Stopwatch filterAddTimer    = Stopwatch.StartNew();

                totalRuntimeTimer.Restart();
                string[] lines = File.ReadAllLines(filename);

                int counter = 0;
                int max     = lines.Length - 1;

                int membershipCorrect   = 0;
                int membershipIncorrect = 0;

                bool filterContains = false;

                while (counter < max)
                {
                    filterAddTimer.Restart();
                    filterContains     = filter.ContainsElement(lines[counter]);
                    filterAddTimeTotal = filterAddTimeTotal.Add(filterAddTimer.Elapsed);

                    if (filterContains)
                    {
                        membershipCorrect += 1;
                    }
                    else
                    {
                        membershipIncorrect += 1;
                    }

                    counter++;
                }

                int randomNumber = rand.Next(50000);

                ByteGenerator.SequenceGenerator randomGenerator = new ByteGenerator.SequenceGenerator(8, counter, randomNumber);

                int randomCounter          = 0;
                int falsePositiveCorrect   = 0;
                int falsePositiveIncorrect = 0;
                while (randomCounter < max)
                {
                    string nextRandom = randomGenerator.GetNext();
                    if (!lines.Contains(nextRandom))
                    {
                        filterAddTimer.Restart();
                        filterContains     = filter.ContainsElement(nextRandom);
                        filterAddTimeTotal = filterAddTimeTotal.Add(filterAddTimer.Elapsed);

                        if (filterContains)
                        {
                            falsePositiveIncorrect += 1;
                        }
                        else
                        {
                            falsePositiveCorrect += 1;
                        }

                        randomCounter++;
                    }
                }

                TimeSpan totalRuntimeTotal = totalRuntimeTimer.Elapsed;

                int totalCounter        = counter + randomCounter;
                long ticks              = filterAddTimeTotal.Ticks / totalCounter;
                TimeSpan averageAddTime = new TimeSpan(ticks);

                double membershipRate    = (double)membershipCorrect / (double)membershipIncorrect;
                double falsePositiveRate = (double)falsePositiveIncorrect / (double)falsePositiveCorrect;

                if (double.IsInfinity(membershipRate))
                {
                    membershipRate = 100.0d;
                }
                if (double.IsInfinity(falsePositiveRate))
                {
                    falsePositiveRate = 100.0d;
                }

                string correctnessReport = string.Format("Membership Correct/Incorrect: {0}/{1} ({2:0.00}%); False Positives: {3}/{4} ({5:0.00}%)", membershipCorrect, membershipIncorrect, membershipRate, falsePositiveIncorrect, falsePositiveCorrect, falsePositiveRate);

                string timersReport =
                    $"Total runtime: {FormHelper.FormatTimeSpan(totalRuntimeTotal)} (Contains elements: {FormHelper.FormatTimeSpan(filterAddTimeTotal)} ({FormHelper.FormatTimeSpan(averageAddTime)} avg.))";

                SetControlText(label5, correctnessReport);
                SetControlText(label6, timersReport);
            });

            var hideLoadingAnimation = testHashingElements.ContinueWith((task) => HideWorkingAnimation());
        }