Exemple #1
0
        public async Task TestEventStreamCleanup()
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw new SkipTestException("Test debugee sigfaults for OSX/Linux");
            }

            Stream eventStream = null;

            using var cancellationTokenSource = new CancellationTokenSource();
            await using (var testExecution = StartTraceeProcess("TestEventStreamCleanup"))
            {
                //TestRunner should account for start delay to make sure that the diagnostic pipe is available.

                var client   = new DiagnosticsClient(testExecution.TestRunner.Pid);
                var settings = new EventTracePipelineSettings()
                {
                    Duration      = Timeout.InfiniteTimeSpan,
                    Configuration = new CpuProfileConfiguration()
                };

                await using var pipeline = new EventTracePipeline(client, settings, (s, token) =>
                {
                    eventStream = s; //Clients should not do this.
                    cancellationTokenSource.Cancel();
                    token.ThrowIfCancellationRequested();
                    return(Task.CompletedTask);
                });

                await Assert.ThrowsAsync <OperationCanceledException>(async() => await PipelineTestUtilities.ExecutePipelineWithDebugee(pipeline, testExecution, cancellationTokenSource.Token));
            }

            //Validate that the stream is only valid for the lifetime of the callback in the trace pipeline.
            Assert.Throws <ObjectDisposedException>(() => eventStream.Read(new byte[4], 0, 4));
        }
        private async Task <ActionResult> StartTrace(
            ProcessFilter?processFilter,
            MonitoringSourceConfiguration configuration,
            TimeSpan duration)
        {
            IProcessInfo processInfo = await _diagnosticServices.GetProcessAsync(processFilter, HttpContext.RequestAborted);

            return(new OutputStreamResult(async(outputStream, token) =>
            {
                Func <Stream, CancellationToken, Task> streamAvailable = async(Stream eventStream, CancellationToken token) =>
                {
                    //Buffer size matches FileStreamResult
                    //CONSIDER Should we allow client to change the buffer size?
                    await eventStream.CopyToAsync(outputStream, 0x10000, token);
                };

                await using EventTracePipeline pipeProcessor = new EventTracePipeline(processInfo.Client, new EventTracePipelineSettings
                {
                    Configuration = configuration,
                    Duration = duration,
                }, streamAvailable);

                await pipeProcessor.RunAsync(token);
            }, "application/octet-stream", FormattableString.Invariant($"{GetFileNameTimeStampUtcNow()}_{processInfo.ProcessId}.nettrace")));
        }
Exemple #3
0
        public async Task TestTraceStopAsync()
        {
            using var buffer = new MemoryStream();
            Stream eventStream = null;

            await using (var testExecution = StartTraceeProcess("TraceStopTest"))
            {
                //TestRunner should account for start delay to make sure that the diagnostic pipe is available.

                var client   = new DiagnosticsClient(testExecution.TestRunner.Pid);
                var settings = new EventTracePipelineSettings()
                {
                    Duration      = Timeout.InfiniteTimeSpan,
                    Configuration = new CpuProfileConfiguration()
                };

                await using var pipeline = new EventTracePipeline(client, settings, async(s, token) =>
                {
                    await s.CopyToAsync(buffer);
                    eventStream = s;
                });

                await PipelineTestUtilities.ExecutePipelineWithDebugee(pipeline, testExecution);
            }

            //Validate that the stream is only valid for the lifetime of the callback in the trace pipeline.
            Assert.Throws <ObjectDisposedException>(() => eventStream.Read(new byte[4], 0, 4));

            Assert.True(buffer.Length > 0);

            var  eventSource      = new EventPipeEventSource(buffer);
            bool foundCpuProvider = false;

            eventSource.Dynamic.All += (TraceEvent obj) =>
            {
                if (string.Equals(obj.ProviderName, MonitoringSourceConfiguration.SampleProfilerProviderName, StringComparison.OrdinalIgnoreCase))
                {
                    foundCpuProvider = true;
                }
            };
            Assert.True(eventSource.Process());
            Assert.True(foundCpuProvider);
        }
Exemple #4
0
        public async Task TestTraceStopAsync()
        {
            using var buffer = new MemoryStream();

            await using (var testExecution = StartTraceeProcess("TraceStopTest"))
            {
                //TestRunner should account for start delay to make sure that the diagnostic pipe is available.

                var client   = new DiagnosticsClient(testExecution.TestRunner.Pid);
                var settings = new EventTracePipelineSettings()
                {
                    Duration      = Timeout.InfiniteTimeSpan,
                    Configuration = new CpuProfileConfiguration()
                };

                await using var pipeline = new EventTracePipeline(client, settings, async(s, token) =>
                {
                    //The buffer must be read in order to not hang. The Stop message will not be processed otherwise.
                    await s.CopyToAsync(buffer);
                });

                await PipelineTestUtilities.ExecutePipelineWithDebugee(pipeline, testExecution);
            }

            Assert.True(buffer.Length > 0);

            var  eventSource      = new EventPipeEventSource(buffer);
            bool foundCpuProvider = false;

            eventSource.Dynamic.All += (TraceEvent obj) =>
            {
                if (string.Equals(obj.ProviderName, MonitoringSourceConfiguration.SampleProfilerProviderName, StringComparison.OrdinalIgnoreCase))
                {
                    foundCpuProvider = true;
                }
            };
            Assert.True(eventSource.Process());
            Assert.True(foundCpuProvider);
        }
Exemple #5
0
        public static async Task CaptureTraceAsync(TaskCompletionSource <object> startCompletionSource, IEndpointInfo endpointInfo, MonitoringSourceConfiguration configuration, TimeSpan duration, Stream outputStream, CancellationToken token)
        {
            Func <Stream, CancellationToken, Task> streamAvailable = async(Stream eventStream, CancellationToken token) =>
            {
                if (null != startCompletionSource)
                {
                    startCompletionSource.TrySetResult(null);
                }
                //Buffer size matches FileStreamResult
                //CONSIDER Should we allow client to change the buffer size?
                await eventStream.CopyToAsync(outputStream, 0x10000, token);
            };

            var client = new DiagnosticsClient(endpointInfo.Endpoint);

            await using EventTracePipeline pipeProcessor = new EventTracePipeline(client, new EventTracePipelineSettings
            {
                Configuration = configuration,
                Duration      = duration,
            }, streamAvailable);

            await pipeProcessor.RunAsync(token);
        }
Exemple #6
0
        private async Task <ActionResult> StartTrace(
            ProcessFilter?processFilter,
            MonitoringSourceConfiguration configuration,
            TimeSpan duration,
            string egressProvider)
        {
            IProcessInfo processInfo = await _diagnosticServices.GetProcessAsync(processFilter, HttpContext.RequestAborted);

            string fileName = FormattableString.Invariant($"{GetFileNameTimeStampUtcNow()}_{processInfo.EndpointInfo.ProcessId}.nettrace");

            Func <Stream, CancellationToken, Task> action = async(outputStream, token) =>
            {
                Func <Stream, CancellationToken, Task> streamAvailable = async(Stream eventStream, CancellationToken token) =>
                {
                    //Buffer size matches FileStreamResult
                    //CONSIDER Should we allow client to change the buffer size?
                    await eventStream.CopyToAsync(outputStream, 0x10000, token);
                };

                var client = new DiagnosticsClient(processInfo.EndpointInfo.Endpoint);

                await using EventTracePipeline pipeProcessor = new EventTracePipeline(client, new EventTracePipelineSettings
                {
                    Configuration = configuration,
                    Duration      = duration,
                }, streamAvailable);

                await pipeProcessor.RunAsync(token);
            };

            return(Result(
                       egressProvider,
                       action,
                       fileName,
                       ContentTypes.ApplicationOctectStream,
                       processInfo.EndpointInfo));
        }