Esempio n. 1
0
        private void Execute()
        {
            try
            {
                DbProviders.DbProvider db = benchmark.ConnectionSettings.DbProvider;
                db.Connect();

                // Init script.
                if (runInitScript)
                {
                    db.OnBeforeInitScript();

                    Benchmark.StatementList initScriptStatements = benchmark.InitScript.GetStatementList(db.Name);
                    foreach (Benchmark.Statement statement in initScriptStatements.Statements)
                    {
                        if (interruptTesting)
                        {
                            testingThread = null;
                            OnTestingEnded();
                            return;
                        }

                        string commandText = statement.CommandText;
                        try
                        {
                            db.Execute(commandText);

                            ExecutorMessage message = new ExecutorMessage();
                            message.Message     = "Statement completed";
                            message.MessageType = ExecutorMessageType.Info;
                            message.Statement   = commandText;

                            OnMessage(message);
                        }
                        catch (Exception ex)
                        {
                            ExecutorMessage message = new ExecutorMessage();
                            message.Message     = GetAllExceptionMessages(ex);
                            message.MessageType = ExecutorMessageType.Error;
                            message.Statement   = commandText;

                            OnMessage(message);
                            testingThread = null;
                            OnTestingEnded();
                            return;
                        }
                    }

                    db.OnAfterInitScript();
                }

                try
                {
                    foreach (Benchmark.TestGroup testGroup in benchmark.TestGroups)
                    {
                        if (interruptTesting)
                        {
                            testingThread = null;
                            OnTestingEnded();
                            return;
                        }
                        if (stopTesting)
                        {
                            break;
                        }

                        bool activeTests = false;
                        foreach (Benchmark.Test test in testGroup.Tests)
                        {
                            if (test.Active)
                            {
                                activeTests = true;
                                break;
                            }
                        }

                        if (activeTests)
                        {
                            foreach (Benchmark.Configuration configuration in testGroup.Configurations)
                            {
                                if (interruptTesting)
                                {
                                    testingThread = null;
                                    OnTestingEnded();
                                    return;
                                }
                                if (stopTesting)
                                {
                                    break;
                                }

                                Benchmark.ConfigurationResult currentConfigurationResult = null;
                                foreach (Benchmark.ConfigurationResult configurationResult in testRun.ConfigurationResults)
                                {
                                    if (configurationResult.ConfigurationId == configuration.Id)
                                    {
                                        currentConfigurationResult = configurationResult;
                                        break;
                                    }
                                }

                                try
                                {
                                    db.OnBeforeConfigurationInitScript(configuration);

                                    // Init script.
                                    currentConfigurationResult.InitScriptStarted = true;

                                    Benchmark.StatementList configurationInitScriptStatements = configuration.InitScript.GetStatementList(db.Name);
                                    foreach (Benchmark.Statement statement in configurationInitScriptStatements.Statements)
                                    {
                                        if (interruptTesting)
                                        {
                                            testingThread = null;
                                            OnTestingEnded();
                                            return;
                                        }

                                        string commandText = statement.CommandText;
                                        try
                                        {
                                            db.Execute(commandText);

                                            ExecutorMessage message = new ExecutorMessage();
                                            message.Message     = "Statement completed";
                                            message.MessageType = ExecutorMessageType.Info;
                                            message.Statement   = commandText;

                                            OnMessage(message);
                                        }
                                        catch (Exception ex)
                                        {
                                            currentConfigurationResult.InitScriptErrorMessage = GetAllExceptionMessages(ex);

                                            ExecutorMessage message = new ExecutorMessage();
                                            message.Message     = GetAllExceptionMessages(ex);
                                            message.MessageType = ExecutorMessageType.Error;
                                            message.Statement   = commandText;

                                            OnMessage(message);
                                            testingThread = null;
                                            OnTestingEnded();
                                            return;
                                        }
                                    }
                                    currentConfigurationResult.InitScriptCompleted = true;

                                    db.OnAfterConfigurationInitScript(configuration);

                                    foreach (Benchmark.Test test in testGroup.Tests)
                                    {
                                        try
                                        {
                                            if (!test.Active)
                                            {
                                                continue;
                                            }

                                            if (interruptTesting)
                                            {
                                                testingThread = null;
                                                OnTestingEnded();
                                                return;
                                            }
                                            if (stopTesting)
                                            {
                                                break;
                                            }

                                            foreach (Benchmark.TestResult testResult in testRun.TestResults)
                                            {
                                                if (testResult.TestId == test.Id &&
                                                    testResult.ConfigurationId == configuration.Id)
                                                {
                                                    if (testResult is Benchmark.PlanEquivalenceTestResult planEquivalenceTestResult)
                                                    {
                                                        try
                                                        {
                                                            planEquivalenceTestResult.Started = true;
                                                            HashSet <DbProviders.QueryPlan> distinctPlans = new HashSet <DbProviders.QueryPlan>();
                                                            planEquivalenceTestResult.SuccessfullyCompletedVariants = 0;

                                                            Benchmark.PlanEquivalenceTest planEquivalenceTest = (Benchmark.PlanEquivalenceTest)test;

                                                            List <DataTable> results = new List <DataTable>();

                                                            foreach (Benchmark.QueryVariantResult queryVariantResult in planEquivalenceTestResult.QueryVariantResults)
                                                            {
                                                                try
                                                                {
                                                                    if (interruptTesting)
                                                                    {
                                                                        testingThread = null;
                                                                        OnTestingEnded();
                                                                        return;
                                                                    }
                                                                    if (stopTesting)
                                                                    {
                                                                        break;
                                                                    }

                                                                    queryVariantResult.Started = true;

                                                                    List <TimeSpan>             timeSpans = new List <TimeSpan>();
                                                                    DbProviders.QueryStatistics lastStats = null;
                                                                    for (int i = 0; i < queryRuns; i++)
                                                                    {
                                                                        DbProviders.QueryStatistics stats = db.GetQueryStatistics(queryVariantResult.Query, compareResults);

                                                                        if (checkResultSizes && stats.ResultSize != queryVariantResult.ExpectedResultSize)
                                                                        {
                                                                            throw new Exception(string.Format("Unexpected result size ({0} instead of {1}).", stats.ResultSize, planEquivalenceTest.ExpectedResultSize));
                                                                        }

                                                                        timeSpans.Add(stats.QueryProcessingTime);
                                                                        lastStats = stats;
                                                                    }

                                                                    queryVariantResult.QueryProcessingTime = ComputeAvgTimeSpan(timeSpans);
                                                                    queryVariantResult.ResultSize          = lastStats.ResultSize;

                                                                    DbProviders.QueryPlan plan = db.GetQueryPlan(queryVariantResult.Query);
                                                                    queryVariantResult.QueryPlan = plan;

                                                                    if (compareResults)
                                                                    {
                                                                        results.Add(lastStats.Result);
                                                                    }

                                                                    //// TODO - remove.
                                                                    //planEquivalenceTest.ExpectedResultSize = stats.ResultSize;
                                                                    //break;

                                                                    if (!distinctPlans.Contains(plan))
                                                                    {
                                                                        distinctPlans.Add(plan);
                                                                    }

                                                                    ExecutorMessage message = new ExecutorMessage();
                                                                    message.Message     = "Query executed";
                                                                    message.MessageType = ExecutorMessageType.Info;
                                                                    message.Statement   = queryVariantResult.Query;
                                                                    planEquivalenceTestResult.SuccessfullyCompletedVariants++;

                                                                    OnMessage(message);
                                                                }
                                                                catch (Exception ex)
                                                                {
                                                                    queryVariantResult.ErrorMessage = GetAllExceptionMessages(ex);

                                                                    ExecutorMessage message = new ExecutorMessage();
                                                                    message.Message     = GetAllExceptionMessages(ex);
                                                                    message.MessageType = ExecutorMessageType.Error;
                                                                    message.Statement   = queryVariantResult.Query;

                                                                    OnMessage(message);
                                                                }
                                                                queryVariantResult.Completed = true;
                                                            }

                                                            planEquivalenceTestResult.DistinctQueryPlans = distinctPlans.Count;
                                                            planEquivalenceTestResult.Completed          = true;

                                                            if (compareResults)
                                                            {
                                                                if (!CheckResultsEquality(results))
                                                                {
                                                                    throw new Exception(string.Format("The query variants returned different results."));
                                                                }
                                                            }

                                                            ExecutorMessage messageCompleted = new ExecutorMessage();
                                                            messageCompleted.Message     = string.Format("Test {0} completed.", planEquivalenceTestResult.TestName);
                                                            messageCompleted.MessageType = ExecutorMessageType.Info;
                                                            messageCompleted.Statement   = string.Empty;
                                                            OnMessage(messageCompleted);
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            planEquivalenceTestResult.ErrorMessage = GetAllExceptionMessages(ex);

                                                            ExecutorMessage message = new ExecutorMessage();
                                                            message.Message     = GetAllExceptionMessages(ex);
                                                            message.MessageType = ExecutorMessageType.Error;
                                                            message.Statement   = string.Empty;
                                                            OnMessage(message);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            ExecutorMessage message = new ExecutorMessage();
                                            message.Message     = GetAllExceptionMessages(ex);
                                            message.MessageType = ExecutorMessageType.Error;
                                            message.Statement   = string.Empty;
                                            OnMessage(message);
                                        }
                                    }

                                    db.OnBeforeConfigurationCleanUpScript(configuration);

                                    // Clean up script.
                                    currentConfigurationResult.CleanUpScriptStarted = true;

                                    Benchmark.StatementList configurationCleanUpScriptStatements = configuration.CleanUpScript.GetStatementList(db.Name);
                                    foreach (Benchmark.Statement statement in configurationCleanUpScriptStatements.Statements)
                                    {
                                        if (interruptTesting)
                                        {
                                            testingThread = null;
                                            OnTestingEnded();
                                            return;
                                        }

                                        string commandText = statement.CommandText;
                                        try
                                        {
                                            db.Execute(commandText);

                                            ExecutorMessage message = new ExecutorMessage();
                                            message.Message     = "Statement completed";
                                            message.MessageType = ExecutorMessageType.Info;
                                            message.Statement   = commandText;

                                            OnMessage(message);
                                        }
                                        catch (Exception ex)
                                        {
                                            currentConfigurationResult.CleanUpScriptErrorMessage = GetAllExceptionMessages(ex);

                                            ExecutorMessage message = new ExecutorMessage();
                                            message.Message     = GetAllExceptionMessages(ex);
                                            message.MessageType = ExecutorMessageType.Error;
                                            message.Statement   = commandText;

                                            OnMessage(message);
                                            testingThread = null;
                                            OnTestingEnded();
                                            return;
                                        }
                                    }
                                    currentConfigurationResult.CleanUpScriptCompleted = true;

                                    db.OnAfterConfigurationCleanUpScript(configuration);
                                }
                                catch (Exception ex)
                                {
                                    ExecutorMessage message = new ExecutorMessage();
                                    message.Message     = GetAllExceptionMessages(ex);
                                    message.MessageType = ExecutorMessageType.Error;
                                    message.Statement   = string.Empty;
                                    OnMessage(message);
                                }

                                //// TODO - remove.
                                //break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ExecutorMessage message = new ExecutorMessage();
                    message.Message     = GetAllExceptionMessages(ex);
                    message.MessageType = ExecutorMessageType.Error;
                    message.Statement   = string.Empty;
                    OnMessage(message);
                }

                // Clean up script.
                if (runCleanUpScript)
                {
                    db.OnBeforeCleanUpScript();

                    Benchmark.StatementList cleanUpScriptStatements = benchmark.CleanUpScript.GetStatementList(db.Name);
                    foreach (Benchmark.Statement statement in cleanUpScriptStatements.Statements)
                    {
                        if (interruptTesting)
                        {
                            testingThread = null;
                            OnTestingEnded();
                            return;
                        }

                        string commandText = statement.CommandText;
                        try
                        {
                            db.Execute(commandText);

                            ExecutorMessage message = new ExecutorMessage();
                            message.Message     = "Statement completed";
                            message.MessageType = ExecutorMessageType.Info;
                            message.Statement   = commandText;

                            OnMessage(message);
                        }
                        catch (Exception ex)
                        {
                            ExecutorMessage message = new ExecutorMessage();
                            message.Message     = GetAllExceptionMessages(ex);
                            message.MessageType = ExecutorMessageType.Error;
                            message.Statement   = commandText;

                            OnMessage(message);
                            testingThread = null;
                            OnTestingEnded();
                            return;
                        }
                    }

                    db.OnAfterCleanUpScript();
                }

                db.Close();
            }
            catch (Exception ex)
            {
                ExecutorMessage message = new ExecutorMessage();
                message.Message     = GetAllExceptionMessages(ex);
                message.MessageType = ExecutorMessageType.Error;
                message.Statement   = string.Empty;
                OnMessage(message);
            }

            testingThread = null;
            OnTestingEnded();

            if (!stopTesting)
            {
                // Zajisteni opakovaneho spusteni testu.
                currentLoop++;
                if (currentLoop < testLoops)
                {
                    OnInvokeStartTesting();
                }
                else
                {
                    if (closeOnComplete)
                    {
                        OnInvokeClose();
                    }
                }
            }
        }
Esempio n. 2
0
        public void Prepare()
        {
            if (benchmark == null)
            {
                throw new Exception("Benchmark is not set.");
            }

            DbProviders.DbProvider db = benchmark.ConnectionSettings.DbProvider;

            testRun = new Benchmark.TestRun(benchmark);

            if (testLoops == 1)
            {
                testRun.Name = testRunName;
            }
            else
            {
                testRun.Name = string.Format("{0} ({1}/{2})", testRunName, currentLoop + 1, testLoops);
            }

            testRun.ExecutorInfo = this.GetExecutorInfoStr();
            testRun.SettingsInfo = db.GetSettingsInfo();

            foreach (Benchmark.TestGroup testGroup in benchmark.TestGroups)
            {
                Benchmark.TestGroupResult testGroupResult = new Benchmark.TestGroupResult(testRun);
                testGroupResult.TestGroupId     = testGroup.Id;
                testGroupResult.TestGroupNumber = testGroup.Number;
                testGroupResult.TestGroupName   = testGroup.Name;
                testRun.TestGroupResults.Add(testGroupResult);

                foreach (Benchmark.Configuration configuration in testGroup.Configurations)
                {
                    Benchmark.ConfigurationResult configurationResult = new Benchmark.ConfigurationResult(testRun);
                    configurationResult.ConfigurationId     = configuration.Id;
                    configurationResult.ConfigurationNumber = configuration.Number;
                    configurationResult.ConfigurationName   = configuration.Name;
                    testRun.ConfigurationResults.Add(configurationResult);

                    foreach (Benchmark.Test test in testGroup.Tests)
                    {
                        if (!test.Active)
                        {
                            continue;
                        }

                        if (test is Benchmark.PlanEquivalenceTest planEquivalenceTest)
                        {
                            if (IgnoreTest(planEquivalenceTest))
                            {
                                continue;
                            }

                            if (planEquivalenceTest.Parametrized)
                            {
                                foreach (Benchmark.Template template in planEquivalenceTest.Templates)
                                {
                                    if (IgnoreTemplate(template))
                                    {
                                        continue;
                                    }

                                    PreparePlanEquivalenceTest(planEquivalenceTest, test, testGroup, configuration, db, template);
                                }
                            }
                            else
                            {
                                PreparePlanEquivalenceTest(planEquivalenceTest, test, testGroup, configuration, db);
                            }
                        }
                        // TODO - other test types.
                    }
                }
            }

            foreach (Benchmark.Annotation annotation in benchmark.Annotations)
            {
                Benchmark.AnnotationResult annotationResult = new Benchmark.AnnotationResult(testRun);
                annotationResult.AnnotationId     = annotation.Id;
                annotationResult.AnnotationNumber = annotation.Number;
                annotationResult.AnnotationName   = annotation.Name;
                testRun.AnnotationResults.Add(annotationResult);
            }

            benchmark.TestRuns.Add(testRun);
        }