Example #1
0
        public virtual async Task MeasureThroughput()
        {
            var cacheClient = GetCacheClient();

            if (cacheClient == null)
            {
                return;
            }

            await cacheClient.RemoveAllAsync();

            var       start     = DateTime.UtcNow;
            const int itemCount = 10000;
            var       metrics   = new InMemoryMetricsClient();

            for (int i = 0; i < itemCount; i++)
            {
                await cacheClient.SetAsync("test", 13422);

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

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

            var workCounter = metrics.GetCounterStatsAsync("work", start, DateTime.UtcNow);
        }
Example #2
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();
                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);
                    await metrics.CounterAsync("work");
                }

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

            if (cacheClient == null)
            {
                return;
            }

            await cacheClient.RemoveAllAsync();

            const int itemCount = 10000;
            var       metrics   = new InMemoryMetricsClient();

            for (int i = 0; i < itemCount; i++)
            {
                await cacheClient.SetAsync("test", new SimpleModel {
                    Data1 = "Hello",
                    Data2 = 12
                });

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

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

            metrics.DisplayStats(_writer);
        }
Example #4
0
        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();
                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);
                    await metrics.CounterAsync("work");
                }

                var workCounter = metrics.GetCounterStatsAsync("work", start, SystemClock.UtcNow);
            }
        }
        public async Task CanDisplayStatsMultithreaded()
        {
            var metrics = new InMemoryMetricsClient();

            metrics.StartDisplayingStats(TimeSpan.FromMilliseconds(10), _writer);

            await Run.InParallel(100, async i => {
                await metrics.CounterAsync("Test");
                await Task.Delay(50);
            });
        }
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());
            }
        }
        public async Task CanSendMultiple()
        {
            const int iterations = 100000;

            await StartListeningAsync(iterations);

            var metrics = new InMemoryMetricsClient();

            var sw = Stopwatch.StartNew();

            for (int index = 0; index < iterations; index++)
            {
                if (index % (iterations / 10) == 0)
                {
                    StopListening();
                }

                await _client.CounterAsync("counter");

                await metrics.CounterAsync("counter");

                if (index % (iterations / 10) == 0)
                {
                    await StartListeningAsync(iterations - index);
                }

                if (index % (iterations / 20) == 0)
                {
                    _logger.Trace((await metrics.GetCounterStatsAsync("counter")).ToString());
                }
            }

            sw.Stop();
            _logger.Info((await metrics.GetCounterStatsAsync("counter")).ToString());

            // Require at least 10,000 operations/s
            Assert.InRange(sw.ElapsedMilliseconds, 0, (iterations / 10000.0) * 1000);

            await Task.Delay(250);

            var messages = GetMessages();
            int expected = iterations - (iterations / (iterations / 10));

            Assert.InRange(messages.Count, expected - 10, expected + 10);
            foreach (string message in messages)
            {
                Assert.Equal("test.counter:1|c", message);
            }
        }
Example #8
0
        public async void CanWaitForCounter()
        {
            var metrics = new InMemoryMetricsClient();

            metrics.StartDisplayingStats(TimeSpan.FromMilliseconds(50), _writer);
            Task.Run(() => {
                Thread.Sleep(50);
                metrics.Counter("Test");
                metrics.Counter("Test");
            });
            var success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(500), 2);

            Assert.True(success);

            Task.Run(() => {
                Thread.Sleep(50);
                metrics.Counter("Test");
            });
            success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(100));

            Assert.False(success);

            Task.Run(() => {
                Thread.Sleep(50);
                metrics.Counter("Test", 2);
            });
            success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(500), 2);

            Assert.True(success);

            success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(500), 1,
                                                        async() => await metrics.CounterAsync("Test"));

            Assert.True(success);

            Task.Run(() => {
                Thread.Sleep(50);
                metrics.Counter("Test");
            });
            success = metrics.WaitForCounter("Test", TimeSpan.FromMilliseconds(500));
            Assert.True(success);

            metrics.DisplayStats(_writer);
        }
Example #9
0
        public async Task CanSendMultiple()
        {
            const int iterations = 100000;

            StartListening(iterations);

            var metrics = new InMemoryMetricsClient();
            var sw      = new Stopwatch();

            sw.Start();
            for (int index = 0; index < iterations; index++)
            {
                if (index % (iterations / 10) == 0)
                {
                    StopListening();
                }

                await _client.CounterAsync("counter");

                await metrics.CounterAsync("counter");

                if (index % (iterations / 10) == 0)
                {
                    StartListening(iterations - index);
                }

                if (index % (iterations / 20) == 0)
                {
                    metrics.DisplayStats(_writer);
                }
            }

            sw.Stop();
            metrics.DisplayStats(_writer);

            // Require at least 10,000 operations/s
            Assert.InRange(sw.ElapsedMilliseconds, 0, (iterations / 10000.0) * 1000);

            Thread.Sleep(100);
            var messages = GetMessages();

            Assert.Equal(iterations - (iterations / (iterations / 10)), messages.Count);
            foreach (string message in messages)
            {
                Assert.Equal("test.counter:1|c", message);
            }
        }
Example #10
0
        public async Task MeasureThroughput()
        {
            var queue = GetQueue(retries: 3, workItemTimeout: TimeSpan.FromSeconds(2), retryDelay: TimeSpan.FromSeconds(1));

            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);
                    await workItem.CompleteAsync();

                    await metrics.CounterAsync("work");

                    workItem = await queue.DequeueAsync(TimeSpan.Zero);
                }
                metrics.DisplayStats(_writer);

                var stats = await queue.GetQueueStatsAsync();

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

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

            if (queue == null)
            {
                return;
            }

            FlushAll();

            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();
                queue.StartWorking(async workItem => {
                    Assert.Equal("Hello", workItem.Value.Data);
                    await workItem.CompleteAsync();
                    await metrics.CounterAsync("work");
                    countdown.Signal();
                });

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

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

                Trace.WriteLine(CountAllKeys());
            }
        }
Example #12
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());
            }
        }
Example #13
0
        public async Task CanWaitForCounter()
        {
            var metrics = new InMemoryMetricsClient();

            metrics.StartDisplayingStats(TimeSpan.FromMilliseconds(50), _writer);
            Task.Run(async() => {
                await Task.Delay(50);
                await metrics.CounterAsync("Test");
                await metrics.CounterAsync("Test");
            });

            var success = await metrics.WaitForCounterAsync("Test", 2, TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            Task.Run(async() => {
                await Task.Delay(50);
                await metrics.CounterAsync("Test");
            });

            success = await metrics.WaitForCounterAsync("Test", timeout : TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            success = await metrics.WaitForCounterAsync("Test", timeout : TimeSpan.FromMilliseconds(100));

            Assert.False(success);

            Task.Run(async() => {
                await Task.Delay(50);
                await metrics.CounterAsync("Test", 2);
            });

            success = await metrics.WaitForCounterAsync("Test", 2, TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            success = await metrics.WaitForCounterAsync("Test", async() => await metrics.CounterAsync("Test"), cancellationToken : TimeSpan.FromMilliseconds(500).ToCancellationToken());

            Assert.True(success);

            Task.Run(async() => {
                await Task.Delay(50);
                await metrics.CounterAsync("Test");
            });

            success = await metrics.WaitForCounterAsync("Test", timeout : TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            metrics.DisplayStats(_writer);
        }