Ejemplo n.º 1
0
        public Task Run(IWorkContext context)
        {
            context.Verify(nameof(context)).IsNotNull();
            context = context
                      .WithCreateLogger(nameof(ReceiveMessages))
                      .With(_tag);

            context.Telemetry.Info(context, "Receiving events...");
            _messageCount = 0;

            try
            {
                _sampler.Start();
                using Timer timer = new Timer(x => MetricsOutput(context), null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));

                Task.WaitAll(new MessageReceiverHost().Run(context, _option.TaskCount, async x => await ReceiveMessage(context, x)));
            }
            catch (Exception ex)
            {
                context.Telemetry.Error(context, "Send failed", ex);
                throw;
            }
            finally
            {
                _sampler.Stop();
                MetricsOutput(context);
            }

            MetricsOutput(context);
            context.Telemetry.Info(context, $"Received {_messageCount} messages");
            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        public async Task Run(IWorkContext context)
        {
            context.Verify(nameof(context)).IsNotNull();
            context = context
                      .WithCreateLogger(nameof(ReceiveEvents))
                      .With(_tag);

            context.Telemetry.Info(context, "Receiving events...");
            _messageCount = 0;

            try
            {
                _sampler.Start();
                using Timer timer = new Timer(x => MetricsOutput(context), null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));

                // Registers the Event Processor Host and starts receiving messages
                EventProcessorOptions eventOption = EventProcessorOptions.DefaultOptions;
                eventOption.ReceiveTimeout = TimeSpan.FromSeconds(5);

                await _eventReceiverHost.RegisterEventProcessorFactoryAsync(new EventProcessFactory(context, _sampler), eventOption);

                while (!context.CancellationToken.IsCancellationRequested)
                {
                    await Task.Delay(500);
                }
            }
            catch (Exception ex)
            {
                context.Telemetry.Error(context, "Send failed", ex);
                throw;
            }
            finally
            {
                context.Telemetry.Info(context, "Unregister event processing...");

                // Disposes of the Event Processor Host
                await _eventReceiverHost.UnregisterEventProcessorAsync();

                _sampler.Stop();

                MetricsOutput(context);
            }

            MetricsOutput(context);
            context.Telemetry.Info(context, $"Received {_messageCount} messages");
        }
Ejemplo n.º 3
0
        public void GivenSampler_WhenTestingFast_ShouldHaveSingleSummaryRecord()
        {
            const int count   = 99;
            var       metrics = new MetricSampler(TimeSpan.FromSeconds(1));

            metrics.Start();

            Enumerable.Range(0, count)
            .ForEach(x => metrics.Add(x));

            metrics.Stop();

            IReadOnlyList <MetricSample> metricsList = metrics.GetMetrics();

            metricsList.Count().Should().Be(1);

            int   n            = count - 1;
            float sumOfNumbers = (n * (n + 1)) / 2;

            metricsList.Max(x => x.Value).Should().Be(sumOfNumbers);
            metricsList.Max(x => x.Count).Should().Be(count);
        }