public virtual async Task CanDequeueEfficientlyAsync() { const int iterations = 100; var queue = GetQueue(runQueueMaintenance: false); if (queue == null) { return; } try { await queue.DeleteQueueAsync(); await AssertEmptyQueueAsync(queue); await queue.EnqueueAsync(new SimpleWorkItem { Data = "Initialize queue to create more accurate metrics" }); Assert.NotNull(await queue.DequeueAsync(TimeSpan.FromSeconds(1))); using (var metrics = new InMemoryMetricsClient(new InMemoryMetricsClientOptions())) { queue.AttachBehavior(new MetricsQueueBehavior <SimpleWorkItem>(metrics, reportCountsInterval: TimeSpan.FromMilliseconds(100), loggerFactory: Log)); Task.Run(async() => { _logger.Trace("Starting enqueue loop."); for (int index = 0; index < iterations; index++) { await SystemClock.SleepAsync(RandomData.GetInt(10, 30)); await queue.EnqueueAsync(new SimpleWorkItem { Data = "Hello" }); } _logger.Trace("Finished enqueuing."); }); _logger.Trace("Starting dequeue loop."); for (int index = 0; index < iterations; index++) { var item = await queue.DequeueAsync(TimeSpan.FromSeconds(3)); Assert.NotNull(item); await item.CompleteAsync(); } _logger.Trace("Finished dequeuing."); await metrics.FlushAsync(); var timing = await metrics.GetTimerStatsAsync("simpleworkitem.queuetime"); _logger.Trace(() => $"AverageDuration: {timing.AverageDuration}"); Assert.InRange(timing.AverageDuration, 0, 75); } } finally { await CleanupQueueAsync(queue); } }
public virtual async Task CanRunWorkItemWithMetrics() { var eventRaised = new ManualResetEvent(false); var metricsClient = new InMemoryMetricsClient(false); var behavior = new MetricsQueueBehavior <WorkItemData>(metricsClient, "metric"); var queue = new InMemoryQueue <WorkItemData>(behaviors: new[] { behavior }); queue.Completed.AddHandler((sender, e) => { eventRaised.Set(); return(TaskHelper.Completed()); }); var work = new SimpleWorkItem { Id = 1, Data = "Testing" }; await queue.EnqueueAsync(work); var item = await queue.DequeueAsync(); await item.CompleteAsync(); Assert.True(eventRaised.WaitOne(TimeSpan.FromMinutes(1))); Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.enqueued")); Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.dequeued")); Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.completed")); var queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.queuetime"); Assert.True(0 < queueTiming.Count); var processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.processtime"); Assert.True(0 < processTiming.Count); }
public virtual async Task CanDequeueEfficiently() { const int iterations = 100; var queue = GetQueue(runQueueMaintenance: false); if (queue == null) { return; } using (queue) { await queue.DeleteQueueAsync(); await AssertEmptyQueueAsync(queue); using (var metrics = new InMemoryMetricsClient()) { queue.AttachBehavior(new MetricsQueueBehavior <SimpleWorkItem>(metrics)); Task.Run(async() => { for (int index = 0; index < iterations; index++) { await SystemClock.SleepAsync(RandomData.GetInt(10, 30)); await queue.EnqueueAsync(new SimpleWorkItem { Data = "Hello" }); } _logger.Trace("Done enqueuing."); }); _logger.Trace("Starting dequeue loop."); var sw = Stopwatch.StartNew(); for (int index = 0; index < iterations; index++) { var item = await queue.DequeueAsync(TimeSpan.FromSeconds(3)); Assert.NotNull(item); await item.CompleteAsync(); } sw.Stop(); Assert.InRange(sw.ElapsedMilliseconds, iterations * 10, iterations * 50); var timing = await metrics.GetTimerStatsAsync("simpleworkitem.queuetime"); Assert.InRange(timing.AverageDuration, 0, 25); } } }
public virtual async Task CanResumeDequeueEfficientlyAsync() { const int iterations = 10; var queue = GetQueue(runQueueMaintenance: false); if (queue == null) { return; } try { await queue.DeleteQueueAsync(); await AssertEmptyQueueAsync(queue); using (var metrics = new InMemoryMetricsClient(new InMemoryMetricsClientOptions())) { for (int index = 0; index < iterations; index++) { await queue.EnqueueAsync(new SimpleWorkItem { Data = "Hello" }); } using (var secondQueue = GetQueue(runQueueMaintenance: false)) { secondQueue.AttachBehavior(new MetricsQueueBehavior <SimpleWorkItem>(metrics, reportCountsInterval: TimeSpan.FromMilliseconds(100), loggerFactory: Log)); _logger.Trace("Starting dequeue loop."); for (int index = 0; index < iterations; index++) { _logger.Trace(() => $"[{index}] Calling Dequeue"); var item = await secondQueue.DequeueAsync(TimeSpan.FromSeconds(3)); Assert.NotNull(item); await item.CompleteAsync(); } await metrics.FlushAsync(); // This won't flush metrics queue behaviors var timing = await metrics.GetTimerStatsAsync("simpleworkitem.queuetime"); _logger.Trace(() => $"TotalDuration: {timing.TotalDuration} AverageDuration: {timing.AverageDuration}"); Assert.InRange(timing.AverageDuration, 0, 75); } } } finally { await CleanupQueueAsync(queue); } }
public virtual async Task CanRunWorkItemWithMetricsAsync() { int completedCount = 0; using (var metrics = new InMemoryMetricsClient(new InMemoryMetricsClientOptions { Buffered = false, LoggerFactory = Log })) { var behavior = new MetricsQueueBehavior <WorkItemData>(metrics, "metric", TimeSpan.FromMilliseconds(100), loggerFactory: Log); var options = new InMemoryQueueOptions <WorkItemData> { Behaviors = new[] { behavior }, LoggerFactory = Log }; using (var queue = new InMemoryQueue <WorkItemData>(options)) { Func <object, CompletedEventArgs <WorkItemData>, Task> handler = (sender, e) => { completedCount++; return(Task.CompletedTask); }; using (queue.Completed.AddHandler(handler)) { _logger.Trace("Before enqueue"); await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "Testing" }); await queue.EnqueueAsync(new SimpleWorkItem { Id = 2, Data = "Testing" }); await queue.EnqueueAsync(new SimpleWorkItem { Id = 3, Data = "Testing" }); await SystemClock.SleepAsync(100); _logger.Trace("Before dequeue"); var item = await queue.DequeueAsync(); await item.CompleteAsync(); item = await queue.DequeueAsync(); await item.CompleteAsync(); item = await queue.DequeueAsync(); await item.AbandonAsync(); _logger.Trace("Before asserts"); Assert.Equal(2, completedCount); await SystemClock.SleepAsync(100); // flush metrics queue behaviors await metrics.FlushAsync(); Assert.InRange((await metrics.GetGaugeStatsAsync("metric.workitemdata.count")).Max, 1, 3); Assert.InRange((await metrics.GetGaugeStatsAsync("metric.workitemdata.working")).Max, 0, 1); Assert.Equal(3, await metrics.GetCounterCountAsync("metric.workitemdata.simple.enqueued")); Assert.Equal(3, await metrics.GetCounterCountAsync("metric.workitemdata.enqueued")); Assert.Equal(3, await metrics.GetCounterCountAsync("metric.workitemdata.simple.dequeued")); Assert.Equal(3, await metrics.GetCounterCountAsync("metric.workitemdata.dequeued")); Assert.Equal(2, await metrics.GetCounterCountAsync("metric.workitemdata.simple.completed")); Assert.Equal(2, await metrics.GetCounterCountAsync("metric.workitemdata.completed")); Assert.Equal(1, await metrics.GetCounterCountAsync("metric.workitemdata.simple.abandoned")); Assert.Equal(1, await metrics.GetCounterCountAsync("metric.workitemdata.abandoned")); var queueTiming = await metrics.GetTimerStatsAsync("metric.workitemdata.simple.queuetime"); Assert.Equal(3, queueTiming.Count); queueTiming = await metrics.GetTimerStatsAsync("metric.workitemdata.queuetime"); Assert.Equal(3, queueTiming.Count); var processTiming = await metrics.GetTimerStatsAsync("metric.workitemdata.simple.processtime"); Assert.Equal(3, processTiming.Count); processTiming = await metrics.GetTimerStatsAsync("metric.workitemdata.processtime"); Assert.Equal(3, processTiming.Count); var queueStats = await metrics.GetQueueStatsAsync("metric.workitemdata"); Assert.Equal(3, queueStats.Enqueued.Count); Assert.Equal(3, queueStats.Dequeued.Count); Assert.Equal(2, queueStats.Completed.Count); Assert.Equal(1, queueStats.Abandoned.Count); Assert.InRange(queueStats.Count.Max, 1, 3); Assert.InRange(queueStats.Working.Max, 0, 1); var subQueueStats = await metrics.GetQueueStatsAsync("metric.workitemdata", "simple"); Assert.Equal(3, subQueueStats.Enqueued.Count); Assert.Equal(3, subQueueStats.Dequeued.Count); Assert.Equal(2, subQueueStats.Completed.Count); Assert.Equal(1, subQueueStats.Abandoned.Count); } } } }
public virtual async Task CanRunWorkItemWithMetrics() { int completedCount = 0; //Log.MinimumLevel = LogLevel.Trace; //Log.SetLogLevel<ScheduledTimer>(LogLevel.Information); //Log.SetLogLevel<InMemoryCacheClient>(LogLevel.Information); using (var metricsClient = new InMemoryMetricsClient(false, loggerFactory: Log)) { var behavior = new MetricsQueueBehavior <WorkItemData>(metricsClient, "metric", loggerFactory: Log, reportCountsInterval: TimeSpan.Zero); using (var queue = new InMemoryQueue <WorkItemData>(behaviors: new[] { behavior }, loggerFactory: Log)) { Func <object, CompletedEventArgs <WorkItemData>, Task> handler = (sender, e) => { completedCount++; return(Task.CompletedTask); }; using (queue.Completed.AddHandler(handler)) { _logger.Trace("Before enqueue"); await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "Testing" }); await queue.EnqueueAsync(new SimpleWorkItem { Id = 2, Data = "Testing" }); await queue.EnqueueAsync(new SimpleWorkItem { Id = 3, Data = "Testing" }); await SystemClock.SleepAsync(100); _logger.Trace("Before dequeue"); var item = await queue.DequeueAsync(); await item.CompleteAsync(); item = await queue.DequeueAsync(); await item.CompleteAsync(); item = await queue.DequeueAsync(); await item.AbandonAsync(); _logger.Trace("Before asserts"); Assert.Equal(2, completedCount); await SystemClock.SleepAsync(100); Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.count")).Max, 1, 3); Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.working")).Max, 0, 1); Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.enqueued")); Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.enqueued")); Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.dequeued")); Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.dequeued")); Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.completed")); Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.completed")); Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.abandoned")); Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.abandoned")); var queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.queuetime"); Assert.Equal(3, queueTiming.Count); queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.queuetime"); Assert.Equal(3, queueTiming.Count); var processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.processtime"); Assert.Equal(3, processTiming.Count); processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.processtime"); Assert.Equal(3, processTiming.Count); var queueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata"); Assert.Equal(3, queueStats.Enqueued.Count); Assert.Equal(3, queueStats.Dequeued.Count); Assert.Equal(2, queueStats.Completed.Count); Assert.Equal(1, queueStats.Abandoned.Count); Assert.InRange(queueStats.Count.Max, 1, 3); Assert.InRange(queueStats.Working.Max, 0, 1); var subQueueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata", "simple"); Assert.Equal(3, subQueueStats.Enqueued.Count); Assert.Equal(3, subQueueStats.Dequeued.Count); Assert.Equal(2, subQueueStats.Completed.Count); Assert.Equal(1, subQueueStats.Abandoned.Count); } } } }
public virtual async Task CanRunWorkItemWithMetrics() { int completedCount = 0; Log.MinimumLevel = LogLevel.Trace; var metricsClient = new InMemoryMetricsClient(false, loggerFactory: Log); var behavior = new MetricsQueueBehavior <WorkItemData>(metricsClient, "metric", loggerFactory: Log, reportCountsInterval: TimeSpan.Zero); var queue = new InMemoryQueue <WorkItemData>(behaviors: new[] { behavior }, loggerFactory: Log); queue.Completed.AddHandler((sender, e) => { completedCount++; return(TaskHelper.Completed); }); _logger.Trace("Before enqueue"); await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "Testing" }); await queue.EnqueueAsync(new SimpleWorkItem { Id = 2, Data = "Testing" }); await queue.EnqueueAsync(new SimpleWorkItem { Id = 3, Data = "Testing" }); await Task.Delay(100); _logger.Trace("Before dequeue"); var item = await queue.DequeueAsync(); await item.CompleteAsync(); item = await queue.DequeueAsync(); await item.CompleteAsync(); item = await queue.DequeueAsync(); await item.AbandonAsync(); _logger.Trace("Before asserts"); Assert.Equal(2, completedCount); Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.count")).Max, 2, 3); Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.working")).Max, 0, 1); Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.enqueued")); Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.enqueued")); Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.dequeued")); Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.dequeued")); Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.completed")); Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.completed")); Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.abandoned")); Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.abandoned")); var queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.queuetime"); Assert.Equal(3, queueTiming.Count); queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.queuetime"); Assert.Equal(3, queueTiming.Count); var processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.processtime"); Assert.Equal(3, processTiming.Count); processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.processtime"); Assert.Equal(3, processTiming.Count); var queueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata"); Assert.Equal(3, queueStats.Enqueued.Count); Assert.Equal(3, queueStats.Dequeued.Count); Assert.Equal(2, queueStats.Completed.Count); Assert.Equal(1, queueStats.Abandoned.Count); Assert.Equal(3, queueStats.Count.Max); Assert.InRange(queueStats.Working.Max, 0, 1); var subQueueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata", "simple"); Assert.Equal(3, subQueueStats.Enqueued.Count); Assert.Equal(3, subQueueStats.Dequeued.Count); Assert.Equal(2, subQueueStats.Completed.Count); Assert.Equal(1, subQueueStats.Abandoned.Count); }