Ejemplo n.º 1
0
        private static EvaluationResult Evaluate(GenerationParameters candidate, object @lock, bool generate = true)
        {
            using (new Timer(String.Format("TestRunner.Evaluate(ID{0:000})", candidate.Id)))
            {
                try
                {
                    Console.WriteLine("Starting evaluation of candidate {0}...", candidate.Id);

                    var dataPath           = Path.Combine(Settings.TempFolder, String.Format("ID{0:000}", candidate.Id));
                    var dataTxtFilePath    = Path.Combine(dataPath, "data.txt");
                    var dataBinaryFilePath = Path.Combine(dataPath, "data.bin");

                    if (generate && Directory.Exists(dataPath))
                    {
                        foreach (var file in Directory.GetFiles(dataPath))
                        {
                            File.Delete(file);
                        }
                    }

                    Directory.CreateDirectory(dataPath);

                    if (generate)
                    {
                        var generator = new DataGenerator();
                        var generated = generator.Generate(candidate);

                        var serializer = new DataSerializer();
                        serializer.Serialize(generated, dataTxtFilePath, dataBinaryFilePath);
                    }

                    var dataBlobInfo   = Utils.GenerateDataBlob(candidate, dataTxtFilePath, dataBinaryFilePath);
                    var javascriptFile = Utils.GenerateJavascript(dataPath, dataBlobInfo.DataTxtLength, dataBlobInfo.DataBinLength);
                    var totalSize      = dataBlobInfo.BlobFile.Length + javascriptFile.Length;
                    var sizeSuccess    = totalSize <= (64 * 1024);

                    var executor   = new TestExecutor();
                    var testResult = executor.Test(candidate, dataTxtFilePath, dataBinaryFilePath);

                    lock (@lock)
                    {
                        Console.BackgroundColor = sizeSuccess ? ConsoleColor.Green : ConsoleColor.Red;
                        Console.ForegroundColor = sizeSuccess ? ConsoleColor.Black : ConsoleColor.White;
                        Console.WriteLine("Evaluation of candidate {0} finished! Size: {1} Score: {2}",
                                          candidate.Id,
                                          sizeSuccess ? "OK" : "TOO BIG",
                                          testResult.AvgScore);
                        Console.ResetColor();
                    }

                    WriteFalsePositives(dataPath, testResult.FalsePositives.OrderBy(v => v, StringComparer.Ordinal));

                    return(new EvaluationResult
                    {
                        Parameters = candidate,
                        TestResult = testResult,
                        TotalSize = totalSize,
                        SizeSuccess = sizeSuccess,
                    });
                }
                catch (Exception)
                {
                    Console.WriteLine("Evaluation of candidate {0} has yielded an error", candidate.Id);
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        private void BuildBloomFilter(BloomFilterParameters parameters, GeneratedData data)
        {
            if (parameters.Disabled)
            {
                return;
            }

            using (new Timer("DataGenerator.BuildBloomFilter"))
            {
                var filter  = new BloomFilter(parameters.FilterSizeBytes * 8, parameters.HashFunctionsCount);
                var counter = new TestExecutor.MatchCounter();
                foreach (var word in Words.Value)
                {
                    // if (word.Length < 3) continue;
                    if (!TestExecutor.Match(data, word, data.Parameters, counter, skipBloomFilter: true))
                    {
                        continue;
                    }

                    var processed = ProcessWordForBloomFilter(parameters, word);
                    filter.add(processed);
                }

                data.BloomFilter = filter;

                using (new Timer("DataGenerator.BuildBloomFilter[retouch]"))
                {
                    if (parameters.RetouchWordCount > 0)
                    {
                        var retouched = FakeWordsByFrequency.Value
                                        .Where(t => t.Item2 > 2)
                                        .Where(t => TestExecutor.Match(data, t.Item1, data.Parameters, counter))
                                        .Take(parameters.RetouchWordCount.Value)
                                        .ToArray();

                        foreach (var tuple in retouched)
                        {
                            var word      = tuple.Item1;
                            var processed = ProcessWordForBloomFilter(parameters, word);
                            filter.retouch(processed, parameters.RetouchMaxWeight ?? 0);
                        }

                        // WriteRetouched(data.Parameters.Id.Value, retouched);
                    }
                    else if (parameters.RetouchMinRelWeight != null)
                    {
                        var falsePositives = FakeWordsByFrequency.Value
                                             .Where(t => t.Item2 > 2)
                                             .Where(t => TestExecutor.Match(data, t.Item1, data.Parameters, counter))
                                             .ToArray();

                        var falsePositivesBF = new BloomFilter(filter.m, filter.k);
                        foreach (var fp in falsePositives)
                        {
                            var processed = ProcessWordForBloomFilter(parameters, fp.Item1);
                            falsePositivesBF.add(processed, fp.Item2);
                        }

                        filter.retouch(falsePositivesBF, parameters.RetouchMinRelWeight.Value);
                    }
                }
            }
        }