Example #1
0
        public virtual async Task MeasureSerializerSimpleThroughputAsync()
        {
            var cache = GetCacheClient();

            if (cache == null)
            {
                return;
            }

            using (cache) {
                await cache.RemoveAllAsync();

                var       start     = SystemClock.UtcNow;
                const int itemCount = 10000;
                var       metrics   = new InMemoryMetricsClient(new InMemoryMetricsClientOptions());
                for (int i = 0; i < itemCount; i++)
                {
                    await cache.SetAsync("test", new SimpleModel {
                        Data1 = "Hello",
                        Data2 = 12
                    });

                    var model = await cache.GetAsync <SimpleModel>("test");

                    Assert.True(model.HasValue);
                    Assert.Equal("Hello", model.Value.Data1);
                    Assert.Equal(12, model.Value.Data2);
                    metrics.Counter("work");
                }

                var workCounter = metrics.GetCounterStatsAsync("work", start, SystemClock.UtcNow);
            }
        }
Example #2
0
        public virtual async Task MeasureThroughputAsync()
        {
            var cache = GetCacheClient();

            if (cache == null)
            {
                return;
            }

            using (cache) {
                await cache.RemoveAllAsync();

                var       start     = SystemClock.UtcNow;
                const int itemCount = 10000;
                var       metrics   = new InMemoryMetricsClient(new InMemoryMetricsClientOptions());
                for (int i = 0; i < itemCount; i++)
                {
                    await cache.SetAsync("test", 13422);

                    await cache.SetAsync("flag", true);

                    Assert.Equal(13422, (await cache.GetAsync <int>("test")).Value);
                    Assert.Null(await cache.GetAsync <int>("test2"));
                    Assert.True((await cache.GetAsync <bool>("flag")).Value);
                    metrics.Counter("work");
                }

                var workCounter = metrics.GetCounterStatsAsync("work", start, SystemClock.UtcNow);
            }
        }
        public virtual async Task MeasureSerializerComplexThroughputAsync()
        {
            var cache = GetCacheClient();

            if (cache == null)
            {
                return;
            }

            using (cache) {
                await cache.RemoveAllAsync();

                var       start     = SystemClock.UtcNow;
                const int itemCount = 10000;
                var       metrics   = new InMemoryMetricsClient(new InMemoryMetricsClientOptions());
                for (int i = 0; i < itemCount; i++)
                {
                    await cache.SetAsync("test", new ComplexModel {
                        Data1  = "Hello",
                        Data2  = 12,
                        Data3  = true,
                        Simple = new SimpleModel {
                            Data1 = "hi",
                            Data2 = 13
                        },
                        Simples = new List <SimpleModel> {
                            new SimpleModel {
                                Data1 = "hey",
                                Data2 = 45
                            },
                            new SimpleModel {
                                Data1 = "next",
                                Data2 = 3423
                            }
                        },
                        DictionarySimples = new Dictionary <string, SimpleModel> {
                            { "sdf", new SimpleModel {
                                  Data1 = "Sachin"
                              } }
                        },

                        DerivedDictionarySimples = new SampleDictionary <string, SimpleModel> {
                            { "sdf", new SimpleModel {
                                  Data1 = "Sachin"
                              } }
                        }
                    });

                    var model = await cache.GetAsync <ComplexModel>("test");

                    Assert.True(model.HasValue);
                    Assert.Equal("Hello", model.Value.Data1);
                    Assert.Equal(12, model.Value.Data2);
                    metrics.Counter("work");
                }

                var workCounter = metrics.GetCounterStatsAsync("work", start, SystemClock.UtcNow);
            }
        }
Example #4
0
        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());
        }
Example #5
0
        public virtual async Task CanRunMultipleQueueJobs()
        {
            const int jobCount      = 5;
            const int workItemCount = 100;

            Log.SetLogLevel <SampleQueueJob>(LogLevel.Error);
            Log.SetLogLevel <InMemoryMetricsClient>(LogLevel.Trace);

            var metrics = new InMemoryMetricsClient(true, loggerFactory: Log);

            var queues = new List <IQueue <SampleQueueWorkItem> >();

            for (int i = 0; i < jobCount; i++)
            {
                var q = GetSampleWorkItemQueue(retries: 3, retryDelay: TimeSpan.FromSeconds(1));
                await q.DeleteQueueAsync();

                q.AttachBehavior(new MetricsQueueBehavior <SampleQueueWorkItem>(metrics, "test", Log));
                queues.Add(q);
            }

            var enqueueTask = Run.InParallel(workItemCount, async index => {
                var queue = queues[RandomData.GetInt(0, jobCount - 1)];
                await queue.EnqueueAsync(new SampleQueueWorkItem {
                    Created = DateTime.Now,
                    Path    = RandomData.GetString()
                });
            });

            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();
            });

            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);
            }

            await metrics.FlushAsync();

            var counter = await metrics.GetCounterStatsAsync("completed");

            Assert.Equal(queueStats.Sum(s => s.Completed), counter.Count);
            Assert.InRange(queueStats.Sum(s => s.Completed), 0, workItemCount);
        }
Example #6
0
        public async Task MeasureThroughputWithRandomFailures()
        {
            var queue = GetQueue(retries: 3, workItemTimeout: TimeSpan.FromSeconds(2), retryDelay: TimeSpan.Zero);

            if (queue == null)
            {
                return;
            }

            FlushAll();

            using (queue) {
                await queue.DeleteQueueAsync();

                const int workItemCount = 1000;
                for (int i = 0; i < workItemCount; i++)
                {
                    await queue.EnqueueAsync(new SimpleWorkItem {
                        Data = "Hello"
                    });
                }
                Assert.Equal(workItemCount, (await queue.GetQueueStatsAsync()).Queued);

                var metrics  = new InMemoryMetricsClient();
                var workItem = await queue.DequeueAsync(TimeSpan.Zero);

                while (workItem != null)
                {
                    Assert.Equal("Hello", workItem.Value.Data);
                    if (RandomData.GetBool(10))
                    {
                        await workItem.AbandonAsync();
                    }
                    else
                    {
                        await workItem.CompleteAsync();
                    }

                    await metrics.CounterAsync("work");

                    workItem = await queue.DequeueAsync(TimeSpan.FromMilliseconds(100));
                }
                _logger.Trace((await metrics.GetCounterStatsAsync("work")).ToString());

                var stats = await queue.GetQueueStatsAsync();

                Assert.True(stats.Dequeued >= workItemCount);
                Assert.Equal(workItemCount, stats.Completed + stats.Deadletter);
                Assert.Equal(0, stats.Queued);

                Trace.WriteLine(CountAllKeys());
            }
        }
Example #7
0
        public async Task MeasureWorkerThroughput()
        {
            var queue = GetQueue(retries: 3, workItemTimeout: TimeSpan.FromSeconds(2), retryDelay: TimeSpan.FromSeconds(1));

            if (queue == null)
            {
                return;
            }

            using (queue) {
                await queue.DeleteQueueAsync();

                const int workItemCount = 1;
                for (int i = 0; i < workItemCount; i++)
                {
                    await queue.EnqueueAsync(new SimpleWorkItem {
                        Data = "Hello"
                    });
                }
                Assert.Equal(workItemCount, (await queue.GetQueueStatsAsync()).Queued);

                var countdown = new AsyncCountdownEvent(workItemCount);
                var metrics   = new InMemoryMetricsClient(new InMemoryMetricsClientOptions());
                await queue.StartWorkingAsync(async workItem => {
                    Assert.Equal("Hello", workItem.Value.Data);
                    await workItem.CompleteAsync();
                    metrics.Counter("work");
                    countdown.Signal();
                });

                await countdown.WaitAsync(TimeSpan.FromMinutes(1));

                Assert.Equal(0, countdown.CurrentCount);
                _logger.LogTrace((await metrics.GetCounterStatsAsync("work")).ToString());

                var stats = await queue.GetQueueStatsAsync();

                Assert.Equal(workItemCount, stats.Dequeued);
                Assert.Equal(workItemCount, stats.Completed);
                Assert.Equal(0, stats.Queued);

                var muxer = SharedConnection.GetMuxer();
                _logger.LogTrace("# Keys: {0}", muxer.CountAllKeysAsync());
            }
        }
Example #8
0
        public async Task MeasureThroughput()
        {
            var queue = GetQueue(retries: 3, workItemTimeout: TimeSpan.FromSeconds(2), retryDelay: TimeSpan.FromSeconds(1));

            if (queue == null)
            {
                return;
            }

            using (queue) {
                await queue.DeleteQueueAsync();

                const int workItemCount = 1000;
                for (int i = 0; i < workItemCount; i++)
                {
                    await queue.EnqueueAsync(new SimpleWorkItem {
                        Data = "Hello"
                    });
                }
                Assert.Equal(workItemCount, (await queue.GetQueueStatsAsync()).Queued);

                var metrics  = new InMemoryMetricsClient();
                var workItem = await queue.DequeueAsync(TimeSpan.Zero);

                while (workItem != null)
                {
                    Assert.Equal("Hello", workItem.Value.Data);
                    await workItem.CompleteAsync();

                    await metrics.CounterAsync("work");

                    workItem = await queue.DequeueAsync(TimeSpan.Zero);
                }
                _logger.Trace((await metrics.GetCounterStatsAsync("work")).ToString());

                var stats = await queue.GetQueueStatsAsync();

                Assert.Equal(workItemCount, stats.Dequeued);
                Assert.Equal(workItemCount, stats.Completed);
                Assert.Equal(0, stats.Queued);

                var muxer = SharedConnection.GetMuxer();
                _logger.Trace("# Keys: {0}", muxer.CountAllKeysAsync());
            }
        }