private ProfilerResult(ProcedureContext procedureContext, CollectorsSynchronizer synchronizer) { _procedureContext = procedureContext; _synchronizer = synchronizer; _bulkCopy = new ProfilerBulkCopyDataReader(_procedureContext); }
private static AnalyzerTaskResult InternalAnalyzeAsync(ProcedureContext procedureContext, AdomdClient.AdomdConnection connection) { var queryResult = default(DataTable); var profilerResult = default(ProfilerResult); var performanceResult = default(PerformanceResult); using (var collectorsSynchronizer = CollectorsSynchronizer.Create(procedureContext.CancellationToken)) using (var performanceTask = PerformanceCollector.StartAsync(procedureContext, collectorsSynchronizer)) { try { if (!performanceTask.IsFaulted && !procedureContext.IsCancellationRequested) { #region profiler collector using (var profilerCancellation = CancellationTokenSource.CreateLinkedTokenSource(procedureContext.CancellationToken)) using (var profilerTask = ProfilerCollector.StartAsync(procedureContext, collectorsSynchronizer, profilerCancellation.Token)) { try { if (!profilerTask.IsFaulted && !profilerCancellation.IsCancellationRequested) { try { queryResult = AdomdClientHelper.ExecuteDataTable( connection, procedureContext.CancellationToken, procedureContext.Statement, activityID: procedureContext.ClientActivityID, rowsLimit: procedureContext.QueryResultRowLimit, beforeStart: () => EventsNotifier.Instance.Notify(ProcedureEvents.ProcedureAnalyzeStepExecuteStatement), beforeWait: () => EventsNotifier.Instance.Notify(ProcedureEvents.ProcedureAnalyzeStepPendingOutcome) ); } finally { profilerCancellation.Cancel(); } } } finally { profilerResult = profilerTask.Result; } } #endregion } } finally { performanceResult = performanceTask.Result; } } return(AnalyzerTaskResult.Create(queryResult, profilerResult, performanceResult)); }
public static ProfilerResult Create(ProcedureContext procedureContext, CollectorsSynchronizer synchronizer) { #region Argument exceptions if (procedureContext == null) { throw new ArgumentNullException("procedureContext"); } if (synchronizer == null) { throw new ArgumentNullException("synchronizer"); } #endregion return(new ProfilerResult(procedureContext, synchronizer)); }
public static Task <PerformanceResult> StartAsync(ProcedureContext procedureContext, CollectorsSynchronizer collectorsSynchronizer) { #region Argument exceptions if (collectorsSynchronizer == null) { throw new ArgumentNullException("collectorsSynchronizer"); } if (procedureContext == null) { throw new ArgumentNullException("procedureContext"); } #endregion var started = new ManualResetEvent(false); var faulted = new ManualResetEvent(false); var performanceTask = new Task <PerformanceResult>(() => { var performanceResult = PerformanceResult.Create(procedureContext); started.Set(); while (!collectorsSynchronizer.IsCompleted) { try { DateTime eventTime; if (collectorsSynchronizer.TryTake(out eventTime, Timeout.Infinite, procedureContext.CancellationToken)) { performanceResult.Collect(eventTime); } } catch (OperationCanceledException) { break; } } if (procedureContext.ExecutionMode == ProcedureExecutionMode.Batch) { performanceResult.FlushBatch(ThreadingTimeout.Infinite).Wait(); } return(performanceResult); }, procedureContext.CancellationToken, TaskCreationOptions.AttachedToParent | TaskCreationOptions.LongRunning); performanceTask.ContinueWith((previousTask) => faulted.Set(), TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.OnlyOnFaulted); performanceTask.Start(); WaitHandle.WaitAny(new[] { started, faulted }); return(performanceTask); }
public static Task <ProfilerResult> StartAsync(ProcedureContext procedureContext, CollectorsSynchronizer collectorsSynchronizer, CancellationToken cancellationToken) { #region Argument exceptions if (procedureContext == null) { throw new ArgumentNullException("procedureContext"); } if (collectorsSynchronizer == null) { throw new ArgumentNullException("collectorsSynchronizer"); } if (cancellationToken == null) { throw new ArgumentNullException("cancellationToken"); } #endregion var started = new ManualResetEvent(false); var faulted = new ManualResetEvent(false); var traceTask = new Task <ProfilerResult>(() => { #region task var profilerResult = ProfilerResult.Create(procedureContext, collectorsSynchronizer); using (var server = new Server()) { server.Connect(procedureContext.ConnectionString); var trace = server.Traces.Find(ProfilerCollector.TraceId); if (trace == null) { throw new ApplicationException("Trace not found [{0}]".FormatWith(ProfilerCollector.TraceName)); } var onTraceEventException = default(Exception); #if ASQASSAS13 || ASQASSAS14 var clientActivityID = procedureContext.ClientActivityID.ToString("D"); #endif trace.OnEvent += (s, e) => { #region OnEvent try { #if ASQASSAS11 || ASQASSAS12 var found = e.ConnectionID == procedureContext.CurrentConnectionID; #else var found = clientActivityID.Equals(e[TraceColumn.ActivityID], StringComparison.OrdinalIgnoreCase); #endif if (found) { profilerResult.Add(e); } } catch (Exception ex) { if (onTraceEventException == null) { onTraceEventException = ex; } profilerResult.CollectCompleted.Set(); } #endregion }; trace.Start(); while (!trace.IsStarted) { Thread.Sleep(50); } started.Set(); cancellationToken.WaitHandle.WaitOne(Timeout.Infinite); WaitHandle.WaitAny(new [] { profilerResult.CollectCompleted, procedureContext.CancellationToken.WaitHandle }); if (onTraceEventException != null) { throw onTraceEventException; } if (procedureContext.ExecutionMode == ProcedureExecutionMode.Batch && !procedureContext.IsCancellationRequested) { profilerResult.FlushBatch(completion: true); } trace.Stop(); } return(profilerResult); #endregion }, cancellationToken, TaskCreationOptions.AttachedToParent | TaskCreationOptions.LongRunning); traceTask.ContinueWith((previousTask) => faulted.Set(), TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.OnlyOnFaulted); traceTask.ContinueWith((previousTask) => collectorsSynchronizer.CompleteAdding(), TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.ExecuteSynchronously); traceTask.Start(); WaitHandle.WaitAny(new [] { started, faulted }); return(traceTask); }