/// <summary> /// Handle request to start a profiling session /// </summary> internal async Task HandleStartProfilingRequest(StartProfilingParams parameters, RequestContext <StartProfilingResult> requestContext) { try { var result = new StartProfilingResult(); ConnectionInfo connInfo; ConnectionServiceInstance.TryFindConnection( parameters.OwnerUri, out connInfo); if (connInfo != null) { ProfilerSession session = StartSession(parameters.OwnerUri, connInfo); result.SessionId = session.SessionId; result.Succeeded = true; } else { result.Succeeded = false; result.ErrorMessage = SR.ProfilerConnectionNotFound; } await requestContext.SendResult(result); } catch (Exception e) { await requestContext.SendError(e); } }
/// <summary> /// Handle request to start a profiling session /// </summary> internal async Task HandleStartProfilingRequest(StartProfilingParams parameters, RequestContext <StartProfilingResult> requestContext) { await Task.Run(async() => { try { ConnectionInfo connInfo; ConnectionServiceInstance.TryFindConnection( parameters.OwnerUri, out connInfo); if (connInfo != null) { // create a new XEvent session and Profiler session var xeSession = this.XEventSessionFactory.GetXEventSession(parameters.SessionName, connInfo); // start monitoring the profiler session monitor.StartMonitoringSession(parameters.OwnerUri, xeSession); var result = new StartProfilingResult(); await requestContext.SendResult(result); } else { throw new Exception(SR.ProfilerConnectionNotFound); } } catch (Exception e) { await requestContext.SendError(new Exception(SR.StartSessionFailed(e.Message))); } }); }
/// <summary> /// Handle request to start a profiling session /// </summary> internal async Task HandleStartProfilingRequest(StartProfilingParams parameters, RequestContext <StartProfilingResult> requestContext) { try { var result = new StartProfilingResult(); ConnectionInfo connInfo; ConnectionServiceInstance.TryFindConnection( parameters.OwnerUri, out connInfo); if (connInfo != null) { int xEventSessionId = StartSession(parameters.OwnerUri, parameters.TemplateName, connInfo); result.SessionId = xEventSessionId.ToString(); result.Succeeded = true; } else { result.Succeeded = false; result.ErrorMessage = SR.ProfilerConnectionNotFound; } await requestContext.SendResult(result); } catch (Exception e) { await requestContext.SendError(new Exception(SR.StartSessionFailed(e.Message))); } }
/// <summary> /// Verify that a start profiling request starts a profiling session /// </summary> //[Fact] public async Task TestHandleStartAndStopProfilingRequests() { using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) { var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath); ProfilerService profilerService = new ProfilerService(); // start a new session var startParams = new StartProfilingParams(); startParams.OwnerUri = connectionResult.ConnectionInfo.OwnerUri; startParams.TemplateName = "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 sessionId = result.SessionId; 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() { OwnerUri = sessionId }; var stopContext = new Mock <RequestContext <StopProfilingResult> >(); stopContext.Setup(rc => rc.SendResult(It.IsAny <StopProfilingResult>())) .Returns(Task.FromResult(0)); await profilerService.HandleStopProfilingRequest(stopParams, stopContext.Object); stopContext.VerifyAll(); } }
/// <summary> /// Test starting a profiling session and receiving event callback /// </summary> /// <returns></returns> // TODO: Fix flaky test. See https://github.com/Microsoft/sqltoolsservice/issues/459 //[Fact] public async Task TestStartProfilingRequest() { string sessionId = null; 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 sessionId = result.SessionId; return(Task.FromResult(0)); }); var sessionListener = new TestSessionListener(); var profilerService = new ProfilerService(); profilerService.SessionMonitor.AddSessionListener(sessionListener); 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.TemplateName = "Standard"; await profilerService.HandleStartProfilingRequest(requestParams, requestContext.Object); // wait a bit for profile sessions to be polled Thread.Sleep(500); requestContext.VerifyAll(); Assert.Equal(sessionListener.PreviousSessionId, sessionId); Assert.Equal(sessionListener.PreviousEvents.Count, 1); }
/// <summary> /// Test starting a profiling session and receiving event callback /// </summary> /// <returns></returns> // TODO: Fix flaky test. See https://github.com/Microsoft/sqltoolsservice/issues/459 //[Fact] public async Task TestStartProfilingRequest() { string sessionId = null; bool recievedEvents = false; 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 sessionId = result.SessionId; 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.TemplateName = "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.Equal(sessionId, "1"); // check that the proper owner Uri was used Assert.True(recievedEvents); }