public virtual async Task CanRunMultipleQueueJobs() { const int jobCount = 5; const int workItemCount = 100; Log.SetLogLevel<SampleQueueJob>(LogLevel.Information); Log.SetLogLevel<InMemoryMetricsClient>(LogLevel.None); using (var metrics = new InMemoryMetricsClient(true, loggerFactory: Log)) { var queues = new List<IQueue<SampleQueueWorkItem>>(); try { for (int i = 0; i < jobCount; i++) { var q = GetSampleWorkItemQueue(retries: 1, retryDelay: TimeSpan.Zero); await q.DeleteQueueAsync(); q.AttachBehavior(new MetricsQueueBehavior<SampleQueueWorkItem>(metrics, "test", Log)); queues.Add(q); } _logger.Info("Done setting up queues"); var enqueueTask = Run.InParallel(workItemCount, async index => { var queue = queues[RandomData.GetInt(0, jobCount - 1)]; await queue.EnqueueAsync(new SampleQueueWorkItem { Created = SystemClock.UtcNow, Path = RandomData.GetString() }); }); _logger.Info("Done enqueueing"); var cancellationTokenSource = new CancellationTokenSource(); await Run.InParallel(jobCount, async index => { var queue = queues[index - 1]; var job = new SampleQueueJob(queue, metrics, Log); await job.RunUntilEmptyAsync(cancellationTokenSource.Token); cancellationTokenSource.Cancel(); }); _logger.Info("Done running jobs until empty"); await enqueueTask; var queueStats = new List<QueueStats>(); for (int i = 0; i < queues.Count; i++) { var stats = await queues[i].GetQueueStatsAsync(); _logger.Info("Queue#{i}: Working: {working} Completed: {completed} Abandoned: {abandoned} Error: {errors} Deadletter: {deadletter}", i, stats.Working, stats.Completed, stats.Abandoned, stats.Errors, stats.Deadletter); queueStats.Add(stats); } _logger.Info("Done getting queue stats"); await metrics.FlushAsync(); _logger.Info("Done flushing metrics"); var queueSummary = await metrics.GetQueueStatsAsync("test.samplequeueworkitem"); Assert.Equal(queueStats.Sum(s => s.Completed), queueSummary.Completed.Count); Assert.InRange(queueStats.Sum(s => s.Completed), 0, workItemCount); } finally { foreach (var q in queues) { await q.DeleteQueueAsync(); q.Dispose(); } } } }
public async Task JobLoopPerf() { const int iterations = 10000; var metrics = new InMemoryMetricsClient(); var job = new SampleJob(metrics, Log); var sw = Stopwatch.StartNew(); await job.RunContinuousAsync(null, iterations); sw.Stop(); await metrics.FlushAsync(); _logger.Trace((await metrics.GetCounterStatsAsync("runs")).ToString()); _logger.Trace((await metrics.GetCounterStatsAsync("errors")).ToString()); _logger.Trace((await metrics.GetCounterStatsAsync("failed")).ToString()); _logger.Trace((await metrics.GetCounterStatsAsync("completed")).ToString()); }