Beispiel #1
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);
        }
        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);
            }
        }
        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 void MeasureSerializerSimpleThroughput()
        {
            var cacheClient = GetCacheClient();

            if (cacheClient == null)
            {
                return;
            }

            cacheClient.FlushAll();

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

            for (int i = 0; i < itemCount; i++)
            {
                cacheClient.Set("test", new SimpleModel {
                    Data1 = "Hello",
                    Data2 = 12
                });
                var model = cacheClient.Get <SimpleModel>("test");
                Assert.NotNull(model);
                Assert.Equal("Hello", model.Data1);
                Assert.Equal(12, model.Data2);
                metrics.Counter("work");
            }
            metrics.DisplayStats();
        }
        public void MeasureThroughput()
        {
            var cacheClient = GetCacheClient();

            if (cacheClient == null)
            {
                return;
            }

            cacheClient.FlushAll();

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

            for (int i = 0; i < itemCount; i++)
            {
                cacheClient.Set("test", 13422);
                cacheClient.Set("flag", true);
                Assert.Equal(13422, cacheClient.Get <int>("test"));
                Assert.Null(cacheClient.Get <int?>("test2"));
                Assert.True(cacheClient.Get <bool>("flag"));
                metrics.Counter("work");
            }
            metrics.DisplayStats(_writer);
        }
        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);
            }
        }
Beispiel #7
0
        public void CanDisplayStatsMultithreaded()
        {
            var metrics = new InMemoryMetricsClient();

            metrics.StartDisplayingStats(TimeSpan.FromMilliseconds(10), _writer);
            Parallel.For(0, 100, i => {
                metrics.Counter("Test");
                Thread.Sleep(50);
            });
        }
Beispiel #8
0
        public async Task MeasureThroughputWithRandomFailures()
        {
            var queue = GetQueue(retries: 3, workItemTimeout: TimeSpan.FromSeconds(2), retryDelay: TimeSpan.Zero);

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

                    metrics.Counter("work");
                    workItem = await queue.DequeueAsync(TimeSpan.FromMilliseconds(100));
                }
                _logger.LogTrace((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);

                var muxer = SharedConnection.GetMuxer();
                _logger.LogTrace("# Keys: {0}", muxer.CountAllKeysAsync());
            }
        }
        public async Task CanSendMultiple()
        {
            const int iterations = 100000;

            StartListening(iterations);

            var metrics = new InMemoryMetricsClient(new InMemoryMetricsClientOptions());

            var sw = Stopwatch.StartNew();

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

                _client.Counter("counter");
                metrics.Counter("counter");

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

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

            sw.Stop();
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation((await metrics.GetCounterStatsAsync("counter")).ToString());
            }

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

            SystemClock.Sleep(250);
            var messages = GetMessages();
            int expected = iterations - (iterations / (iterations / 10));

            Assert.InRange(messages.Count, expected - 90, expected + 10);
            foreach (string message in messages)
            {
                Assert.Equal("test.counter:1|c", message);
            }
        }
Beispiel #10
0
        public void MeasureThroughputWithRandomFailures()
        {
            var queue = GetQueue(retries: 3, workItemTimeout: TimeSpan.FromSeconds(2), retryDelay: TimeSpan.Zero);

            if (queue == null)
            {
                return;
            }

            FlushAll();

            using (queue) {
                queue.DeleteQueue();

                const int workItemCount = 10000;
                for (int i = 0; i < workItemCount; i++)
                {
                    queue.Enqueue(new SimpleWorkItem {
                        Data = "Hello"
                    });
                }
                Assert.Equal(workItemCount, queue.GetQueueCount());

                var metrics  = new InMemoryMetricsClient();
                var workItem = queue.Dequeue(TimeSpan.Zero);
                while (workItem != null)
                {
                    Assert.Equal("Hello", workItem.Value.Data);
                    if (RandomData.GetBool(10))
                    {
                        workItem.Abandon();
                    }
                    else
                    {
                        workItem.Complete();
                    }
                    metrics.Counter("work");

                    workItem = queue.Dequeue(TimeSpan.FromMilliseconds(100));
                }
                metrics.DisplayStats();

                Assert.True(queue.DequeuedCount >= workItemCount);
                Assert.Equal(workItemCount, queue.CompletedCount + queue.GetDeadletterCount());
                Assert.Equal(0, queue.GetQueueCount());

                Trace.WriteLine(CountAllKeys());
            }
        }
Beispiel #11
0
        public void CanIncrementCounter()
        {
            var metrics = new InMemoryMetricsClient();

            metrics.Counter("c1");
            Assert.Equal(1, metrics.GetCount("c1"));

            metrics.Counter("c1", 5);
            Assert.Equal(6, metrics.GetCount("c1"));

            var counter = metrics.Counters["c1"];

            Assert.True(counter.Rate > 400);

            metrics.Gauge("g1", 2.534);
            Assert.Equal(2.534, metrics.GetGaugeValue("g1"));

            metrics.Timer("t1", 50788);
            var stats = metrics.GetMetricStats();

            Assert.Equal(1, stats.Timings.Count);

            metrics.DisplayStats(_writer);
        }
Beispiel #12
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());
            }
        }
        public void MeasureSerializerComplexThroughput()
        {
            var cacheClient = GetCacheClient();

            if (cacheClient == null)
            {
                return;
            }

            cacheClient.FlushAll();

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

            for (int i = 0; i < itemCount; i++)
            {
                cacheClient.Set("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"
                          } }
                    }
                });
                var model = cacheClient.Get <SimpleModel>("test");
                Assert.NotNull(model);
                Assert.Equal("Hello", model.Data1);
                Assert.Equal(12, model.Data2);
                metrics.Counter("work");
            }
            metrics.DisplayStats();
        }
Beispiel #14
0
        public void MeasureWorkerThroughput()
        {
            var queue = GetQueue(retries: 3, workItemTimeout: TimeSpan.FromSeconds(2), retryDelay: TimeSpan.FromSeconds(1));

            if (queue == null)
            {
                return;
            }

            FlushAll();

            using (queue) {
                queue.DeleteQueue();

                const int workItemCount = 10000;
                for (int i = 0; i < workItemCount; i++)
                {
                    queue.Enqueue(new SimpleWorkItem {
                        Data = "Hello"
                    });
                }
                Assert.Equal(workItemCount, queue.GetQueueCount());

                var countdown = new CountDownLatch(workItemCount);
                var metrics   = new InMemoryMetricsClient();
                queue.StartWorking(workItem => {
                    Assert.Equal("Hello", workItem.Value.Data);
                    workItem.Complete();
                    metrics.Counter("work");
                    countdown.Signal();
                });
                countdown.Wait(60 * 1000);
                metrics.DisplayStats();

                Assert.Equal(workItemCount, queue.DequeuedCount);
                Assert.Equal(workItemCount, queue.CompletedCount);
                Assert.Equal(0, queue.GetQueueCount());

                Trace.WriteLine(CountAllKeys());
            }
        }