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:
Sakshi Sharma
2023-06-27 14:25:18 -07:00
committed by GitHub
parent dbcb156816
commit 4334d79d76
27 changed files with 952 additions and 259 deletions

View File

@@ -45,4 +45,9 @@
<ItemGroup>
<EmbeddedResource Include="ShowPlan\TestShowPlanRecommendations.xml" />
</ItemGroup>
<ItemGroup>
<None Update="Profiler\TestXel_0.xel">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -16,10 +16,12 @@ using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
using Moq;
using NUnit.Framework;
using System.IO;
using System.Reflection;
using System.Linq;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
{
[TestFixture]
/// <summary>
/// Unit tests for ProfilerService
/// </summary>
@@ -29,66 +31,39 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
/// Test starting a profiling session and receiving event callback
/// </summary>
/// <returns></returns>
// TODO: Fix flaky test. See https://github.com/Microsoft/sqltoolsservice/issues/459
//[Test]
public async Task TestStartProfilingRequest()
[Test]
public async Task StartProfilingRequest_creates_pausable_remote_session()
{
string sessionId = null;
bool recievedEvents = false;
var sessionId = new SessionId("testsession_1", 1);
string testUri = "profiler_uri";
var requestContext = new Mock<RequestContext<StartProfilingResult>>();
requestContext.Setup(rc => rc.SendResult(It.IsAny<StartProfilingResult>()))
.Returns<StartProfilingResult>((result) =>
{
// capture the session id for sending the stop message
Assert.Multiple(() =>
{
Assert.That(result.CanPause, Is.True, "Result.CanPause for RingBuffer sessions");
Assert.That(result.UniqueSessionId, Is.EqualTo(sessionId.ToString()), "Result.UniqueSessionId");
});
return Task.FromResult(0);
});
// capture Listener event notifications
var mockListener = new Mock<IProfilerSessionListener>();
mockListener.Setup(p => p.EventsAvailable(It.IsAny<string>(), It.IsAny<List<ProfilerEvent>>(), It.IsAny<bool>())).Callback(() =>
{
recievedEvents = true;
});
var profilerService = new ProfilerService();
profilerService.SessionMonitor.AddSessionListener(mockListener.Object);
profilerService.ConnectionServiceInstance = TestObjects.GetTestConnectionService();
ConnectionInfo connectionInfo = TestObjects.GetTestConnectionInfo();
profilerService.ConnectionServiceInstance.OwnerToConnectionMap.Add(testUri, connectionInfo);
profilerService.XEventSessionFactory = new TestXEventSessionFactory();
var requestParams = new StartProfilingParams();
requestParams.OwnerUri = testUri;
requestParams.SessionName = "Standard";
var requestParams = new StartProfilingParams
{
OwnerUri = testUri,
SessionName = "Standard"
};
// start profiling session
await profilerService.HandleStartProfilingRequest(requestParams, requestContext.Object);
profilerService.SessionMonitor.PollSession(1);
// simulate a short polling delay
Thread.Sleep(200);
profilerService.SessionMonitor.PollSession(1);
// wait for polling to finish, or for timeout
System.Timers.Timer pollingTimer = new System.Timers.Timer();
pollingTimer.Interval = 10000;
pollingTimer.Start();
bool timeout = false;
pollingTimer.Elapsed += new System.Timers.ElapsedEventHandler((s_, e_) => {timeout = true;});
while (sessionId == null && !timeout)
{
Thread.Sleep(250);
}
pollingTimer.Stop();
requestContext.VerifyAll();
// Check that the correct XEvent session was started
Assert.AreEqual("1", sessionId);
// check that the proper owner Uri was used
Assert.True(recievedEvents);
await profilerService.HandleStartProfilingRequest(requestParams, requestContext.Object);
requestContext.VerifyAll();
}
/// <summary>
@@ -118,6 +93,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
stopped = true;
});
mockSession.Setup(p => p.GetTargetXml()).Returns("<RingBufferTarget/>");
mockSession.Setup(p => p.Id).Returns(new SessionId("test_1", 1));
var sessionListener = new TestSessionListener();
var profilerService = new ProfilerService();
profilerService.SessionMonitor.AddSessionListener(sessionListener);
@@ -136,8 +114,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
requestContext.VerifyAll();
// check that session was succesfully stopped and stop was called
Assert.True(success);
Assert.True(stopped);
Assert.True(success, nameof(success));
Assert.True(stopped, nameof(stopped));
// should not be able to remove the session, it should already be gone
ProfilerSession ps;
@@ -185,9 +163,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
profilerService.SessionMonitor.StartMonitoringSession(testUri, new TestXEventSession1());
// poll the session
profilerService.SessionMonitor.PollSession(1);
profilerService.SessionMonitor.PollSession(new SessionId("testsession_1", 1));
Thread.Sleep(500);
profilerService.SessionMonitor.PollSession(1);
profilerService.SessionMonitor.PollSession(new SessionId("testsession_1", 1));
// wait for polling to finish, or for timeout
System.Timers.Timer pollingTimer = new System.Timers.Timer();
@@ -211,7 +189,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
recievedEvents = false;
success = false;
profilerService.SessionMonitor.PollSession(1);
profilerService.SessionMonitor.PollSession(new SessionId("testsession_1", 1));
// confirm that no events were sent to paused Listener
Assert.False(recievedEvents);
@@ -220,7 +198,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
await profilerService.HandlePauseProfilingRequest(requestParams, requestContext.Object);
Assert.True(success);
profilerService.SessionMonitor.PollSession(1);
profilerService.SessionMonitor.PollSession(new SessionId("testsession_1", 1));
// wait for polling to finish, or for timeout
timeout = false;
@@ -240,7 +218,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
/// Test notifications for stopped sessions
/// </summary>
[Test]
public async Task TestStoppedSessionNotification()
public void TestStoppedSessionNotification()
{
bool sessionStopped = false;
string testUri = "profiler_uri";
@@ -251,9 +229,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
{
throw new XEventException();
});
mockSession.Setup(p => p.Id).Returns(new SessionId("test_1", 1));
var mockListener = new Mock<IProfilerSessionListener>();
mockListener.Setup(p => p.SessionStopped(It.IsAny<string>(), It.IsAny<int>())).Callback(() =>
mockListener.Setup(p => p.SessionStopped(It.IsAny<string>(), It.IsAny<SessionId>(), It.IsAny<string>())).Callback(() =>
{
sessionStopped = true;
});
@@ -282,5 +260,114 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
// check that a stopped session notification was sent
Assert.True(sessionStopped);
}
[Test]
public void StartProfilingRequest_defaults_to_remote()
{
var param = new StartProfilingParams();
Assert.That(param.SessionType, Is.EqualTo(ProfilingSessionType.RemoteSession), nameof(param.SessionType));
}
[Test]
public async Task StartProfilingRequest_creates_a_LocalFile_session_on_request()
{
var filePath = @"c:\folder\file.xel";
var param = new StartProfilingParams() { OwnerUri = "someUri", SessionType = ProfilingSessionType.LocalFile, SessionName = filePath};
var mockSession = new Mock<IObservableXEventSession>();
mockSession.Setup(p => p.GetTargetXml()).Callback(() =>
{
throw new XEventException();
});
mockSession.Setup(p => p.Id).Returns(new SessionId("test_1", 1));
var requestContext = new Mock<RequestContext<StartProfilingResult>>();
requestContext.Setup(rc => rc.SendResult(It.IsAny<StartProfilingResult>()))
.Returns<StartProfilingResult>((result) =>
{
return Task.FromResult(0);
});
var sessionFactory = new Mock<IXEventSessionFactory>();
sessionFactory.Setup(s => s.OpenLocalFileSession(filePath))
.Returns (mockSession.Object)
.Verifiable();
var profilerService = new ProfilerService() { XEventSessionFactory = sessionFactory.Object };
await profilerService.HandleStartProfilingRequest(param, requestContext.Object);
sessionFactory.Verify();
requestContext.VerifyAll();
}
[Test]
public async Task ProfilerService_processes_localfile_session()
{
var viewerId = "someUri";
var filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Profiler", "TestXel_0.xel");
var param = new StartProfilingParams() { OwnerUri = viewerId, SessionType = ProfilingSessionType.LocalFile, SessionName = filePath };
var requestContext = new Mock<RequestContext<StartProfilingResult>>();
requestContext.Setup(rc => rc.SendResult(It.IsAny<StartProfilingResult>()))
.Returns<StartProfilingResult>((result) =>
{
Assert.Multiple(() =>
{
Assert.That(result.CanPause, Is.False, "local file session cannot be paused");
Assert.That(result.UniqueSessionId, Is.EqualTo(filePath), "UniqueSessionId should match file path");
});
return Task.FromResult(0);
});
var profilerService = new ProfilerService();
var listener = new TestSessionListener();
profilerService.SessionMonitor.AddSessionListener(listener);
await profilerService.HandleStartProfilingRequest(param, requestContext.Object);
var retries = 100;
while (retries-- > 0 && !listener.StoppedSessions.Contains(viewerId))
{
Thread.Sleep(100);
}
Assert.Multiple(() =>
{
Assert.That(listener.StoppedSessions, Has.Member(viewerId), "session should have been stopped after reading the file");
Assert.That(listener.AllEvents.Keys, Has.Member(viewerId), "session should have events logged for it");
Assert.That(listener.AllEvents[viewerId]?.Count, Is.EqualTo(149), "all events from the xel should be in the buffer");
});
}
[Test]
public async Task ProfilerService_includes_ErrorMessage_in_session_stop_notification()
{
var param = new StartProfilingParams() { OwnerUri = "someUri", SessionName = "someSession" };
var mockSession = new Mock<IXEventSession>();
mockSession.Setup(p => p.GetTargetXml()).Callback(() =>
{
throw new XEventException("test!");
});
mockSession.Setup(p => p.Id).Returns(new SessionId("test_1", 1));
var requestContext = new Mock<RequestContext<StartProfilingResult>>();
requestContext.Setup(rc => rc.SendResult(It.IsAny<StartProfilingResult>()))
.Returns<StartProfilingResult>((result) =>
{
return Task.FromResult(0);
});
var sessionFactory = new Mock<IXEventSessionFactory>();
sessionFactory.Setup(s => s.GetXEventSession(It.IsAny<string>(), It.IsAny<ConnectionInfo>()))
.Returns(mockSession.Object)
.Verifiable();
var profilerService = new ProfilerService() { XEventSessionFactory = sessionFactory.Object };
profilerService.ConnectionServiceInstance = TestObjects.GetTestConnectionService();
var connectionInfo = TestObjects.GetTestConnectionInfo();
profilerService.ConnectionServiceInstance.OwnerToConnectionMap.Add("someUri", connectionInfo);
var listener = new TestSessionListener();
profilerService.SessionMonitor.AddSessionListener(listener);
await profilerService.HandleStartProfilingRequest(param, requestContext.Object);
var retries = 10;
while (retries-- > 0 && !listener.StoppedSessions.Any())
{
Thread.Sleep(100);
}
Assert.Multiple(() =>
{
Assert.That(listener.ErrorMessages, Is.EqualTo(new[] { "test!" }), "listener.ErrorMessages");
Assert.That(listener.StoppedSessions, Has.Member("someUri"), "listener.StoppedSessions");
});
sessionFactory.Verify();
}
}
}

View File

@@ -25,7 +25,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
public void TestFilterOldEvents()
{
// create a profiler session and get some test events
var profilerSession = new ProfilerSession();
var profilerSession = new ProfilerSession(new XEventSession());
var allEvents = ProfilerTestObjects.TestProfilerEvents;
var profilerEvents = ProfilerTestObjects.TestProfilerEvents;
@@ -62,7 +62,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
public void TestFilterProfilerEvents()
{
// create a profiler session and get some test events
var profilerSession = new ProfilerSession();
var profilerSession = new ProfilerSession(new XEventSession());
var profilerEvents = ProfilerTestObjects.TestProfilerEvents;
int expectedEventCount = profilerEvents.Count;
@@ -86,7 +86,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
public void TestEventsLost()
{
// create a profiler session and get some test events
var profilerSession = new ProfilerSession();
var profilerSession = new ProfilerSession(new XEventSession());
var profilerEvents = ProfilerTestObjects.TestProfilerEvents;
// filter all the results from the first poll
@@ -135,7 +135,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
DateTime startTime = DateTime.Now;
// create new profiler session
var profilerSession = new ProfilerSession();
var profilerSession = new ProfilerSession(new XEventSession());
// enter the polling block
Assert.True(profilerSession.TryEnterPolling());

View File

@@ -5,6 +5,7 @@
#nullable disable
using System;
using System.Collections.Generic;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Profiler;
@@ -37,21 +38,24 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
public class TestSessionListener : IProfilerSessionListener
{
public string PreviousSessionId { get; set; }
public readonly Dictionary<string, List<ProfilerEvent>> AllEvents = new Dictionary<string, List<ProfilerEvent>>();
public List<ProfilerEvent> PreviousEvents { get; set; }
public bool Stopped { get; set; }
public readonly List<string> StoppedSessions = new List<string>();
public readonly List<string> ErrorMessages = new List<string>();
public void EventsAvailable(string sessionId, List<ProfilerEvent> events, bool eventsLost)
{
this.PreviousSessionId = sessionId;
this.PreviousEvents = events;
if (!AllEvents.ContainsKey(sessionId))
{
AllEvents[sessionId] = new List<ProfilerEvent>();
}
AllEvents[sessionId].AddRange(events);
}
public void SessionStopped(string viewerId, int sessionId)
public void SessionStopped(string viewerId, SessionId sessionId, string errorMessage)
{
Stopped = true;
StoppedSessions.Add(viewerId);
ErrorMessages.Add(errorMessage);
}
}
@@ -197,7 +201,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
public int Id { get { return 51; } }
public SessionId Id { get { return new SessionId("testsession_51"); } }
public void Start(){}
@@ -282,7 +286,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
" </event>" +
"</RingBufferTarget>";
public int Id { get { return 1; } }
public SessionId Id { get { return new SessionId("testsession_1"); } }
public void Start(){}
@@ -373,7 +377,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
" </event>" +
"</RingBufferTarget>";
public int Id { get { return 2; } }
public SessionId Id { get { return new SessionId("testsession_2"); } }
public void Start(){}
@@ -420,5 +424,10 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
return new TestXEventSession2();
}
}
public IXEventSession OpenLocalFileSession(string filePath)
{
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,113 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#nullable disable
using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using System.Reflection;
using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
using Microsoft.SqlTools.ServiceLayer.Profiler;
using System.Threading;
using System.Linq;
using Moq;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
{
public class XeStreamObservableTests
{
/// <summary>
/// this might technically be an integration test but putting it here because it doesn't require any connectivity.
/// </summary>
[Test]
public void XeStreamObservable_reads_entire_xel_file()
{
var observer = InitializeFileObserver();
observer.Observable.Start();
var retries = 100;
while (!observer.Completed && retries-- > 0)
{
Thread.Sleep(100);
}
Assert.That(observer.Completed, Is.True, $"Reading the file didn't complete in 10 seconds. Events read: {observer.ProfilerEvents.Count}");
Assert.Multiple(() =>
{
Assert.That(observer.ProfilerEvents.Count, Is.EqualTo(149), "Number of events read");
Assert.That(observer.ProfilerEvents[0].Name, Is.EqualTo("rpc_completed"), "First event in the file");
Assert.That(observer.ProfilerEvents.Last().Name, Is.EqualTo("sql_batch_completed"), "Last event in the file");
});
}
[Test]
public void XeStreamObservable_calls_OnError_when_the_fetcher_fails()
{
var observer = InitializeFileObserver("thispathdoesnotexist.xel");
observer.Observable.Start();
var retries = 10;
while (!observer.Completed && retries-- > 0)
{
Thread.Sleep(100);
}
Assert.Multiple(() =>
{
Assert.That(observer.Completed, Is.True, $"Reading the missing file didn't complete in 1 second.");
Assert.That(observer.Error?.GetBaseException(), Is.InstanceOf<FileNotFoundException>(), $"Expected Error from missing file. Error:{observer.Error}");
});
}
private XeStreamObserver InitializeFileObserver(string filePath = null)
{
filePath ??= Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Profiler", "TestXel_0.xel");
var profilerService = SetupProfilerService(filePath);
var xeStreamObservable = new XeStreamObservable(() =>
{
return profilerService.initIXEventFetcher(filePath);
});
var observer = new XeStreamObserver() { Observable = xeStreamObservable };
xeStreamObservable.Subscribe(observer);
return observer;
}
private ProfilerService SetupProfilerService (string filePath = null)
{
filePath ??= Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Profiler", "TestXel_0.xel");
var sessionFactory = new Mock<IXEventSessionFactory>();
var profilerService = new ProfilerService() { XEventSessionFactory = sessionFactory.Object };
return profilerService;
}
}
sealed class XeStreamObserver : IObserver<ProfilerEvent>
{
public XeStreamObservable Observable { get; set; }
public readonly List<ProfilerEvent> ProfilerEvents = new List<ProfilerEvent>();
public bool Completed { get; private set; }
public Exception Error { get; private set; }
public void OnCompleted()
{
Completed = true;
}
public void OnError(Exception error)
{
Error = error;
}
public void OnNext(ProfilerEvent value)
{
ProfilerEvents.Add(value);
OnEventAdded?.Invoke(this, EventArgs.Empty);
}
public event EventHandler OnEventAdded;
}
}