public void DeliverySpeedMultithreadTest(int messageSize)
        {
            // Given

            const int threadCount = 5;
            var       stopwatch   = new Stopwatch();
            var       message     = CreateMessage(messageSize);

            var factory       = new RabbitMqMessageQueueFactory(RabbitMqSettings.Default, new Mock <ILog>().Object);
            var publisher     = factory.CreateMessageQueuePublisher();
            var listener      = factory.CreateMessageQueueListener();
            var subscriptions = factory.CreateMessageQueueManager();

            var publishEvent   = new ManualResetEvent(false);
            var startedEvent   = new CountdownEvent(threadCount);
            var completedEvent = new CountdownEvent(threadCount * IterationCount);
            var consumer       = new CountdownConsumer(completedEvent);

            subscriptions.CreateExchangeFanout(ExchangeName).Subscribe(QueueName, () => consumer);
            listener.StartListenAll();

            // When

            for (var t = 0; t < threadCount; ++t)
            {
                ThreadPool.QueueUserWorkItem(state =>
                {
                    startedEvent.Signal();
                    publishEvent.WaitOne();

                    for (var i = 0; i < IterationCount; ++i)
                    {
                        publisher.Publish(ExchangeName, null, null, message);
                    }
                });
            }

            startedEvent.Wait();

            stopwatch.Start();
            publishEvent.Set();
            completedEvent.Wait(WaitTimeout);
            stopwatch.Stop();

            listener.StopListenAll();

            // Then

            const int totalMessages = threadCount * IterationCount;
            var       totalSeconds  = stopwatch.Elapsed.TotalSeconds;

            Console.WriteLine(@"Message size: {0} Kb", messageSize / 1024);
            Console.WriteLine(@"Total time: {0:N4} sec", totalSeconds);
            Console.WriteLine(@"Publish time: {0:N4} ms/message", 1000 * totalSeconds / totalMessages);
            Console.WriteLine(@"Publish speed: {0:N4} message/sec", totalMessages / totalSeconds);
            Console.WriteLine(@"Publish bitrate: {0:N4} kbps", 8.0 * messageSize * totalMessages / (1024 * totalSeconds));
        }
        private static IMessageQueuePublisher GetMessageQueuePublisher()
        {
            var factory       = new RabbitMqMessageQueueFactory(RabbitMqSettings.Default, new Mock <ILog>().Object);
            var publisher     = factory.CreateMessageQueuePublisher();
            var subscriptions = factory.CreateMessageQueueManager();

            subscriptions.CreateExchangeFanout(ExchangeName).Subscribe(QueueName, () => null);

            return(publisher);
        }
示例#3
0
        private static EventWaitHandle ListenerThread(ICollection <string[]> result)
        {
            var subscribeCompleted = new ManualResetEvent(false);
            var consumeCompleted   = new ManualResetEvent(false);

            ThreadPool.QueueUserWorkItem(state =>
            {
                var factory       = new RabbitMqMessageQueueFactory(RabbitMqSettings.Default, new Mock <ILog>().Object);
                var listener      = factory.CreateMessageQueueListener();
                var subscribtions = factory.CreateMessageQueueManager();

                // Создание подписок

                var exchange = subscribtions.CreateExchangeDirect(ExchangeName);

                var completeConsumer1 = new CountdownEvent(Messages1.Length);
                var consumer1         = new TestConsumer(completeConsumer1);
                exchange.Subscribe(QueueName1, () => consumer1, QueueKey1);

                var completeConsumer2 = new CountdownEvent(Messages2.Length);
                var consumer2         = new TestConsumer(completeConsumer2);
                exchange.Subscribe(QueueName2, () => consumer2, QueueKey2);

                subscribeCompleted.Set();

                // Запуск прослушивания

                listener.StartListenAll();

                // Ожидание окончания приема

                WaitHandle.WaitAll(new[] { completeConsumer1.WaitHandle, completeConsumer2.WaitHandle }, WaitTimeout);
                result.Add(consumer1.Messages);
                result.Add(consumer2.Messages);

                consumeCompleted.Set();

                // Остановка прослушивания

                listener.StopListenAll();
            });

            subscribeCompleted.WaitOne(WaitTimeout);

            return(consumeCompleted);
        }
        public void PublishSpeedTest(int messageSize)
        {
            // Given

            var factory       = new RabbitMqMessageQueueFactory(RabbitMqSettings.Default, new Mock <ILog>().Object);
            var publisher     = factory.CreateMessageQueuePublisher();
            var listener      = factory.CreateMessageQueueListener();
            var subscriptions = factory.CreateMessageQueueManager();

            var completedEvent = new CountdownEvent(IterationCount);
            var consumer       = new CountdownConsumer(completedEvent);

            subscriptions.CreateExchangeFanout(ExchangeName).Subscribe(QueueName, () => consumer);

            // When

            var memoryBefore = GC.GetTotalMemory(true);

            listener.StartListenAll();

            for (var i = 0; i < IterationCount; ++i)
            {
                var message = CreateMessage(messageSize);
                publisher.Publish(ExchangeName, null, null, message);
            }

            listener.StopListenAll();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            var memoryAfter = GC.GetTotalMemory(true);

            // Then

            var transferSize      = IterationCount * messageSize / 1024.0;
            var memoryLeak        = (memoryAfter - memoryBefore) / 1024.0;
            var memeryLeakPercent = 100 * memoryLeak / transferSize;

            Console.WriteLine(@"Message size: {0} Kb", messageSize / 1024);
            Console.WriteLine(@"Transfer messages: {0} messages ({1:N0} Kb)", IterationCount, transferSize);
            Console.WriteLine(@"Memory leak: {0:N0} Kb ({1:N0}%)", memoryLeak, memeryLeakPercent);

            Assert.LessOrEqual(memeryLeakPercent, 10);
        }