mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Add "Open XEL file" support to profiler in sqltoolsservice (#2091)
* Open XEL file changes * placeholders for openxel * add observable xe reader * md format tweaks * implement localfile as a new session type * add ErrorMessage to session stopped notice * fix flaky test * handle already running session * fix stopped session event send on file completion * fix flaky unit test * Update XElite and dependent versions * Fix errors after merge and remove failing tests for now * Fix main merge mess-up. Address comments. Add one more relevant test. * Remove extra namespace. * Remove unnecessary import * Fix build error * Address comments. * Remove disabiling JSON002 compiler warning * Address comments and update json handling * Fix build error * Fix integration test (emerged due to Main merge mess up) * Clean up code (no functional changes) --------- Co-authored-by: Karl Burtram <karlb@microsoft.com> Co-authored-by: shueybubbles <david.shiflet@microsoft.com>
This commit is contained in:
@@ -53,7 +53,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Connection
|
||||
query.Execute();
|
||||
query.ExecutionTask.Wait();
|
||||
|
||||
// We should still have 2 DbConnections
|
||||
// We should see 1 DbConnections
|
||||
Assert.AreEqual(1, connectionInfo.CountConnections);
|
||||
|
||||
// If we disconnect, we should remain in a consistent state to do it over again
|
||||
|
||||
@@ -15,71 +15,124 @@ using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Microsoft.SqlServer.Management.Sdk.Sfc;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlServer.Management.XEvent;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Profiler
|
||||
{
|
||||
public class ProfilerServiceTests
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Verify that a start profiling request starts a profiling session
|
||||
/// </summary>
|
||||
//[Test]
|
||||
[Test]
|
||||
public async Task TestHandleStartAndStopProfilingRequests()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
|
||||
var sqlConnection = ConnectionService.OpenSqlConnection(connectionResult.ConnectionInfo);
|
||||
SqlStoreConnection connection = new SqlStoreConnection(sqlConnection);
|
||||
var xeStore = new XEStore(connection);
|
||||
ProfilerService profilerService = new ProfilerService();
|
||||
|
||||
// start a new session
|
||||
var startParams = new StartProfilingParams();
|
||||
startParams.OwnerUri = connectionResult.ConnectionInfo.OwnerUri;
|
||||
startParams.SessionName = "Standard";
|
||||
|
||||
string sessionId = null;
|
||||
var startContext = new Mock<RequestContext<StartProfilingResult>>();
|
||||
startContext.Setup(rc => rc.SendResult(It.IsAny<StartProfilingResult>()))
|
||||
.Returns<StartProfilingResult>((result) =>
|
||||
{
|
||||
// capture the session id for sending the stop message
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
|
||||
await profilerService.HandleStartProfilingRequest(startParams, startContext.Object);
|
||||
|
||||
startContext.VerifyAll();
|
||||
|
||||
// wait a bit for the session monitoring to initialize
|
||||
Thread.Sleep(TimeSpan.FromHours(1));
|
||||
|
||||
// stop the session
|
||||
var stopParams = new StopProfilingParams()
|
||||
var sessionName = await StartStandardSession(profilerService, connectionResult.ConnectionInfo.OwnerUri);
|
||||
xeStore.Sessions.Refresh();
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
OwnerUri = sessionId
|
||||
};
|
||||
Assert.That(xeStore.Sessions.Cast<Session>().Select(s => s.Name), Has.Member(sessionName), "ProfilerService should have created the session");
|
||||
Assert.That(xeStore.Sessions[sessionName].IsRunning, Is.True, "Session should be running when created by ProfilerService");
|
||||
});
|
||||
|
||||
var stopContext = new Mock<RequestContext<StopProfilingResult>>();
|
||||
stopContext.Setup(rc => rc.SendResult(It.IsAny<StopProfilingResult>()))
|
||||
.Returns(Task.FromResult(0));
|
||||
|
||||
await profilerService.HandleStopProfilingRequest(stopParams, stopContext.Object);
|
||||
try
|
||||
{
|
||||
var xeSession = xeStore.Sessions[sessionName];
|
||||
xeSession.Stop();
|
||||
// start a new session
|
||||
var startParams = new StartProfilingParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
SessionName = sessionName
|
||||
};
|
||||
|
||||
stopContext.VerifyAll();
|
||||
string sessionId = null;
|
||||
var startContext = new Mock<RequestContext<StartProfilingResult>>();
|
||||
startContext.Setup(rc => rc.SendResult(It.IsAny<StartProfilingResult>()))
|
||||
.Returns<StartProfilingResult>((result) =>
|
||||
{
|
||||
// capture the session id for sending the stop message
|
||||
sessionId = result.UniqueSessionId;
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
|
||||
|
||||
await profilerService.HandleStartProfilingRequest(startParams, startContext.Object);
|
||||
Assert.That(sessionId, Does.Contain(connectionResult.ConnectionInfo.ConnectionDetails.ServerName), "UniqueSessionId");
|
||||
startContext.VerifyAll();
|
||||
|
||||
// wait a bit for the session monitoring to initialize
|
||||
Thread.Sleep(TimeSpan.FromSeconds(30));
|
||||
|
||||
xeSession.Refresh();
|
||||
Assert.That(xeSession.IsRunning, Is.True, "Session should be running due to HandleStartProfilingRequest");
|
||||
|
||||
// stop the session
|
||||
var stopParams = new StopProfilingParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri
|
||||
};
|
||||
|
||||
var stopContext = new Mock<RequestContext<StopProfilingResult>>();
|
||||
stopContext.Setup(rc => rc.SendResult(It.IsAny<StopProfilingResult>()))
|
||||
.Returns(Task.FromResult(0));
|
||||
|
||||
await profilerService.HandleStopProfilingRequest(stopParams, stopContext.Object);
|
||||
|
||||
xeSession.Refresh();
|
||||
Assert.That(xeSession.IsRunning, Is.False, "Session should be stopped due to HandleStopProfilingRequest");
|
||||
stopContext.VerifyAll();
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
xeStore.Sessions.Refresh();
|
||||
if (xeStore.Sessions.Contains(sessionName))
|
||||
{
|
||||
try
|
||||
{
|
||||
xeStore.Sessions[sessionName].Stop();
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
xeStore.Sessions[sessionName].Drop();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the profiler service XEvent session factory
|
||||
/// </summary>
|
||||
//[Test]
|
||||
public void TestCreateXEventSession()
|
||||
private async Task<string> StartStandardSession(ProfilerService profilerService, string ownerUri)
|
||||
{
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo("master");
|
||||
ProfilerService profilerService = new ProfilerService();
|
||||
IXEventSession xeSession = profilerService.GetXEventSession("Profiler", liveConnection.ConnectionInfo);
|
||||
Assert.NotNull(xeSession);
|
||||
Assert.NotNull(xeSession.GetTargetXml());
|
||||
const string sessionName = "ADS_Standard_Test";
|
||||
var template = Newtonsoft.Json.JsonConvert.DeserializeObject<ProfilerSessionTemplate>(standardSessionJson.Substring(1, standardSessionJson.Length - 2));
|
||||
|
||||
var createParams = new CreateXEventSessionParams() { OwnerUri = ownerUri, SessionName = sessionName, Template = template };
|
||||
var requestContext = new Mock<RequestContext<CreateXEventSessionResult>>();
|
||||
requestContext.Setup(c => c.SendResult(It.IsAny<CreateXEventSessionResult>()))
|
||||
.Returns<CreateXEventSessionResult>((result) => { return Task.FromResult(0); });
|
||||
var serviceHostMock = new Mock<IProtocolEndpoint>();
|
||||
profilerService.ServiceHost = serviceHostMock.Object;
|
||||
await profilerService.HandleCreateXEventSessionRequest(createParams, requestContext.Object);
|
||||
return sessionName;
|
||||
}
|
||||
|
||||
const string standardSessionJson = /*lang=json,strict*/ "[{\"name\": \"Standard_OnPrem\", \"defaultView\": \"Standard View\", \"engineTypes\": [\"Standalone\"], \"createStatement\": \"CREATE EVENT SESSION [{sessionName}] ON SERVER ADD EVENT sqlserver.attention(ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_pid,sqlserver.database_id,sqlserver.nt_username,sqlserver.query_hash,sqlserver.server_principal_name,sqlserver.session_id) WHERE ([package0].[equal_boolean]([sqlserver].[is_system],(0)))), ADD EVENT sqlserver.existing_connection(SET collect_options_text=(1) ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_pid,sqlserver.nt_username,sqlserver.server_principal_name,sqlserver.session_id)), ADD EVENT sqlserver.login(SET collect_options_text=(1) ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_pid,sqlserver.nt_username,sqlserver.server_principal_name,sqlserver.session_id)), ADD EVENT sqlserver.logout( ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_pid,sqlserver.nt_username,sqlserver.server_principal_name,sqlserver.session_id)), ADD EVENT sqlserver.rpc_completed( ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_pid,sqlserver.database_id,sqlserver.database_name,sqlserver.nt_username,sqlserver.query_hash,sqlserver.server_principal_name,sqlserver.session_id) WHERE ([package0].[equal_boolean]([sqlserver].[is_system],(0)))), ADD EVENT sqlserver.sql_batch_completed( ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_pid,sqlserver.database_id,sqlserver.database_name,sqlserver.nt_username,sqlserver.query_hash,sqlserver.server_principal_name,sqlserver.session_id) WHERE ([package0].[equal_boolean]([sqlserver].[is_system],(0)))), ADD EVENT sqlserver.sql_batch_starting( ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_pid,sqlserver.database_id,sqlserver.database_name,sqlserver.nt_username,sqlserver.query_hash,sqlserver.server_principal_name,sqlserver.session_id) WHERE ([package0].[equal_boolean]([sqlserver].[is_system],(0)))) ADD TARGET package0.ring_buffer(SET max_events_limit=(1000),max_memory=(51200)) WITH (MAX_MEMORY=8192 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=5 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=PER_CPU,TRACK_CAUSALITY=ON,STARTUP_STATE=OFF)\"}]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user