diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Batch.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Batch.cs index f98d2bc4..8b40d7ec 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Batch.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Batch.cs @@ -303,6 +303,8 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution await SendMessageIfExecutingMultipleTimes(SR.EE_ExecutionInfo_InitializingLoop, false); + executionStartTime = DateTime.Now; + while (canContinue && timesLoop > 0) { try @@ -353,7 +355,6 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution dbCommand.CommandText = BatchText; dbCommand.CommandType = CommandType.Text; dbCommand.CommandTimeout = 0; - executionStartTime = DateTime.Now; List columnSchemas = null; if (getFullColumnSchema) diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/QueryExecution/ExecuteTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/QueryExecution/ExecuteTests.cs index d29bf5ec..6a2a69cc 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/QueryExecution/ExecuteTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/QueryExecution/ExecuteTests.cs @@ -1,4 +1,9 @@ - +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; using System.Collections.Concurrent; using System.Data.Common; using Microsoft.SqlTools.ServiceLayer.Connection; @@ -98,6 +103,18 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.QueryExecution Assert.Equal(master, connInfo.ConnectionDetails.DatabaseName); } + [Fact] + public void TestBatchExecutionTime() { + var result = LiveConnectionHelper.InitLiveConnectionInfo(); + ConnectionInfo connInfo = result.ConnectionInfo; + var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory(); + Query query = CreateAndExecuteQuery("select * from sys.databases", connInfo, fileStreamFactory); + DateTime elapsedTime = Convert.ToDateTime(query.Batches[0].ExecutionElapsedTime); + Query mutipleQuery = CreateAndExecuteQuery("select * from sys.databases\r\nGO 15", connInfo, fileStreamFactory); + DateTime multipleElapsedTime = Convert.ToDateTime(mutipleQuery.Batches[0].ExecutionElapsedTime); + Assert.True(multipleElapsedTime > elapsedTime); + } + public Query CreateAndExecuteQuery(string queryText, ConnectionInfo connectionInfo, IFileStreamFactory fileStreamFactory) { Query query = new Query(queryText, connectionInfo, new QueryExecutionSettings(), fileStreamFactory); @@ -105,5 +122,6 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.QueryExecution query.ExecutionTask.Wait(); return query; } + } } diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Utility/LiveConnectionHelper.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Utility/LiveConnectionHelper.cs index 254523e8..dc737e7f 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Utility/LiveConnectionHelper.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Utility/LiveConnectionHelper.cs @@ -1,10 +1,10 @@ -using System; +using System; using System.Data.SqlClient; using System.Globalization; using System.IO; using System.Reflection; -using System.Threading; -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; using Microsoft.SqlServer.Management.Common; using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; @@ -19,13 +19,13 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility public static string GetTestSqlFile(string fileName = null) { string filePath = null; - if (string.IsNullOrEmpty(fileName)) - { - filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "sqltest.sql"); - } - else - { - filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), fileName + ".sql"); + if (string.IsNullOrEmpty(fileName)) + { + filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "sqltest.sql"); + } + else + { + filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), fileName + ".sql"); } if (File.Exists(filePath)) @@ -39,12 +39,12 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility public static TestConnectionResult InitLiveConnectionInfo(string databaseName = null, string ownerUri = null) { ScriptFile scriptFile = null; - ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName); - if (string.IsNullOrEmpty(ownerUri)) - { - ownerUri = GetTestSqlFile(); - scriptFile = TestServiceProvider.Instance.WorkspaceService.Workspace.GetFile(ownerUri); - ownerUri = scriptFile.ClientFilePath; + ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName); + if (string.IsNullOrEmpty(ownerUri)) + { + ownerUri = GetTestSqlFile(); + scriptFile = TestServiceProvider.Instance.WorkspaceService.Workspace.GetFile(ownerUri); + ownerUri = scriptFile.ClientFilePath; } var connectionService = GetLiveTestConnectionService(); var connectionResult = @@ -64,15 +64,15 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility public static async Task InitLiveConnectionInfoAsync(string databaseName = null, string ownerUri = null, string connectionType = ServiceLayer.Connection.ConnectionType.Default) - { + { ScriptFile scriptFile = null; - if (string.IsNullOrEmpty(ownerUri)) - { - ownerUri = GetTestSqlFile(); - scriptFile = TestServiceProvider.Instance.WorkspaceService.Workspace.GetFile(ownerUri); - ownerUri = scriptFile.ClientFilePath; - } - ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName); + if (string.IsNullOrEmpty(ownerUri)) + { + ownerUri = GetTestSqlFile(); + scriptFile = TestServiceProvider.Instance.WorkspaceService.Workspace.GetFile(ownerUri); + ownerUri = scriptFile.ClientFilePath; + } + ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName); var connectionService = GetLiveTestConnectionService(); var connectionResult = @@ -83,9 +83,9 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility Connection = connectParams.Connection, Type = connectionType }); - if (!string.IsNullOrEmpty(connectionResult.ErrorMessage)) - { - Console.WriteLine(connectionResult.ErrorMessage); + if (!string.IsNullOrEmpty(connectionResult.ErrorMessage)) + { + Console.WriteLine(connectionResult.ErrorMessage); } ConnectionInfo connInfo = null; @@ -95,26 +95,26 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility public static ConnectionInfo InitLiveConnectionInfoForDefinition(string databaseName = null) { - using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) - { - ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName); - string ownerUri = queryTempFile.FilePath; - var connectionService = GetLiveTestConnectionService(); - var connectionResult = - connectionService - .Connect(new ConnectParams - { - OwnerUri = ownerUri, - Connection = connectParams.Connection - }); - - connectionResult.Wait(); - - ConnectionInfo connInfo = null; - connectionService.TryFindConnection(ownerUri, out connInfo); - - Assert.NotNull(connInfo); - return connInfo; + using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) + { + ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName); + string ownerUri = queryTempFile.FilePath; + var connectionService = GetLiveTestConnectionService(); + var connectionResult = + connectionService + .Connect(new ConnectParams + { + OwnerUri = ownerUri, + Connection = connectParams.Connection + }); + + connectionResult.Wait(); + + ConnectionInfo connInfo = null; + connectionService.TryFindConnection(ownerUri, out connInfo); + + Assert.NotNull(connInfo); + return connInfo; } }