在RabbitMQ上运行时,Masstransit的开销是多少?

时间:2019-04-24 21:15:16

标签: performance rabbitmq masstransit

将Masstransit与RabbitMQ一起使用时,我看到的效果令人失望。传送/获取速度降至0.20 /秒。我开始使用一个简单的测试应用程序来研究此问题。此应用程序并行运行一个线程,该线程使用RabbitMQ客户端库将消息发送到RabbitMQ,而一个线程使用Masstransit库将相同的消息发送到RabbitMQ。 RabbitMQ客户端库发送的消息比Masstransit库多10倍。

RabbitMQ在HyperV计算机的Docker容器中运行。 Masstransit的PublishConfirm标志具有一定作用,但作用不大。 为了进行公平的比较,为RabbitMQ案例定义了与Masstransit案例相同的拓扑。

公交代码:

        public MasstransitMessageSender(string user, string password, string rabbitMqHost)
        {
            this.user = user;
            this.password = password;
            this.rabbitMqHost = rabbitMqHost;
        }

        public RabbitMqMessageSender(string user, string password, string rabbitMqHost)
        {
            var myUri = new Uri(rabbitMqHost);

            this.factory = new ConnectionFactory
            {
                HostName = myUri.Host,
                UserName = user,
                Password = password,
                VirtualHost = myUri.LocalPath,
                Port = (myUri.Port > 0 ? myUri.Port : -1),
                AutomaticRecoveryEnabled = true
            };
        }

        public Task SendCommands(int numberOfMessages)
        {
            return Task.Run(() =>
            {
                var busControl = global::MassTransit.Bus.Factory.CreateUsingRabbitMq(
                    sbc =>
                    {
                        // Host control
                        var host =
                            sbc.Host(
                                new Uri(this.rabbitMqHost),
                                h =>
                                {
                                    h.Username(this.user);
                                    h.Password(this.password);
                                    h.Heartbeat(60);
                                    h.PublisherConfirmation = false;
                                });
                    });

                busControl.StartAsync().Wait();
                var task = busControl.GetSendEndpoint(new Uri(this.rabbitMqHost + "/MasstransitService"));
                var tasks = new List<Task>();

                task.Wait();
                var endpoint = task.Result;
                for (var i = 0; i < numberOfMessages; i++)
                {
                    tasks.Add(endpoint.Send<IMyMessage>(new
                    {
                        Number = i,
                        Description = "MyMessage"
                    }));
                }

                Task.WaitAll(tasks.ToArray());
            });
        }

RabbitMQ代码:

        public RabbitMqMessageSender(string user, string password, string rabbitMqHost)
        {
            var myUri = new Uri(rabbitMqHost);

            this.factory = new ConnectionFactory
            {
                HostName = myUri.Host,
                UserName = user,
                Password = password,
                VirtualHost = myUri.LocalPath,
                Port = (myUri.Port > 0 ? myUri.Port : -1),
                AutomaticRecoveryEnabled = true
            };
        }

        public Task SendCommands(int numberOfMessages)
        {
            return Task.Run(() =>
            {
                using (var connection = this.factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        var messageProperties = channel.CreateBasicProperties();
                        channel.ExchangeDeclare("PerformanceConsole:ShowRabbitMqMessage", "fanout", true, false, null);
                        channel.ExchangeDeclare("RabbitMqService", "fanout", true, false, null);
                        channel.QueueDeclare("RabbitMqService", true, false, false, null);
                        channel.ExchangeBind("RabbitMqService", "PerformanceConsole:ShowRabbitMqMessage", "", null);
                        channel.QueueBind("RabbitMqService", "RabbitMqService", "", null);
                        for (var i = 0; i < numberOfMessages; i++)
                        {
                            var bericht = new
                            {
                                Volgnummer = 1,
                                Tekst = "Bericht"
                            };
                            var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bericht));
                                channel.BasicPublish("RabbitMqService", "", messageProperties, body);
                        }
                    }
                }
            });
        }

RabbitMQ仪表板显示以下费率:

公交输入664 msg / s RabbitMQ传入6764 msg / s

我希望传入的速率在相同范围内。

也许我在配置上犯了一个错误,欢迎提出建议。

1 个答案:

答案 0 :(得分:0)

使用MassTransit-Benchmark,在OS X上使用netcoreapp2.2在MacBook Pro 2015上获得以下性能。

PhatBoyG-Pro15:MassTransit-Benchmark Chris$ dotnet run -f netcoreapp2.2 -- --clients=50 --count=50000 --prefetch=100
MassTransit Benchmark

Transport: RabbitMQ
Host: localhost
Virtual Host: /
Username: guest
Password: *****
Heartbeat: 0
Publisher Confirmation: False
Running Message Latency Benchmark
Message Count: 50000
Clients: 50
Durable: False
Payload Length: 0
Prefetch Count: 100
Concurrency Limit: 0
Total send duration: 0:00:05.2250045
Send message rate: 9569.37 (msg/s)
Total consume duration: 0:00:07.2385114
Consume message rate: 6907.50 (msg/s)
Concurrent Consumer Count: 8
Avg Ack Time: 4ms
Min Ack Time: 0ms
Max Ack Time: 251ms
Med Ack Time: 4ms
95t Ack Time: 6ms
Avg Consume Time: 1431ms
Min Consume Time: 268ms
Max Consume Time: 2075ms
Med Consume Time: 1639ms
95t Consume Time: 2070ms

您可以下载基准测试并自己运行: https://github.com/MassTransit/MassTransit-Benchmark