Esempio n. 1
0
        private void RunTestParse()
        {
            // Function for calculating new average
            Func <float, float, double, int> newAvg = (i, old, nw) => (int)(i == 0 ? nw : old * i / (i + 1) + nw / (i + 1));

            if (_parserTest == null)
            {
                Result += "Parser not set. Enter levels first\r\n";
            }
            if (string.IsNullOrEmpty(_generatedInputFull))
            {
                Result += "No input. Prepare test first\r\n";
            }
            if (_parserTest == null || string.IsNullOrEmpty(_generatedInputFull))
            {
                return;
            }

            Result = "running";

            // Prepare query test
            string letters             = "bcdefg";
            var    levels              = Benchmark.GetLevelListFromCode();
            List <List <string> > sets = GetIdSets(levels);
            long sum = 0;

            Random rnd = new Random();

            // Local function to add integer to sum.
            void AddAllCombinations2(int level, int number, TextElement elem)
            {
                if (level < sets.Count() - 1)
                {
                    string find = sets[level][rnd.Next(levels[level])];
                    AddAllCombinations2(level + 1, number,
                                        elem.Codes().First(e => e.Name == letters.Substring(level, 1) &&
                                                           e.Codes().First().Value == find));
                }
                else
                {
                    CodeElement el = elem.Codes().First(e => e.Name == letters.Substring(level, 1) &&
                                                        e.Codes().First().Value == sets[level][number]);
                    sum += int.Parse(el.Codes().First(e => e.Name == "int").Value);
                }
            }

            Action parse = () =>
            {
                // ---------- MEMORY ---------------
                Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
                long    memoryUsage1   = currentProcess.WorkingSet64;

                // ---------- PARSING ---------------
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
                CodeDocument doc = CodeDocument.Load(_parserTest, _generatedInputFull);
                stopWatch.Stop();

                // ---------- MEMORY RESULT ---------------
                currentProcess = System.Diagnostics.Process.GetCurrentProcess();
                long memoryUsage2 = (currentProcess.WorkingSet64 - memoryUsage1) / 1024;
                if (memoryUsage2 > int.MaxValue)
                {
                    Result += "Memory use too large.\r\n";
                }
                else
                {
                    Benchmark.MemParse = newAvg(Benchmark.TestRuns, Benchmark.TimeParse, memoryUsage2);
                }

                if (stopWatch.Elapsed.TotalMilliseconds > int.MaxValue)
                {
                    Result += "Parse time too long.\r\n";
                }
                else
                {
                    Benchmark.TimeParse = newAvg(Benchmark.TestRuns, Benchmark.TimeParse, stopWatch.Elapsed.TotalMilliseconds);
                }

                // ---------- QUERYING ---------------
                int number    = -1;
                int lastLevel = levels.Last();
                stopWatch = new Stopwatch();
                stopWatch.Start();

                for (long l = 0; l < Benchmark.Combi; l++)
                {
                    if (++number == lastLevel)
                    {
                        number = 0;
                    }
                    AddAllCombinations2(0, number, doc);
                }
                stopWatch.Stop();

                long expected = ((lastLevel - 1) * lastLevel) / 2;
                for (int i = 0; i < levels.Count - 1; i++)
                {
                    expected *= levels[i];
                }

                if (expected != sum)
                {
                    throw new Exception("test sum is off");
                }

                if (stopWatch.Elapsed.TotalMilliseconds > int.MaxValue)
                {
                    Result += "Query time too long.\r\n";
                }
                else
                {
                    Benchmark.TimeQuery = newAvg(Benchmark.TestRuns, Benchmark.TimeQuery, stopWatch.Elapsed.TotalMilliseconds);
                }

                // ---------- OUTPUT ---------------
                Benchmark.TestRuns++;
                Result = "run " + Benchmark.TestRuns;
                if (BenchmarkCompare != null)
                {
                    Result += "\r\n" + Benchmark.Compare(BenchmarkCompare);
                }
            };

            Thread thread = new Thread(new ThreadStart(parse));

            thread.Start();
        }