首页 > 解决方案 > 使用相同库 api 的 Rabbitmq 的性能非常不同(python3 与 dotnet)

问题描述

我一直在尝试找出使用 rabbitmq python 库(pika/aiopika)的最佳方法,而我注意到当我使用 dotnet 库(RabbitMQ.客户)。

Python:

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
exchange_name = "test-load"
channel.exchange_declare(exchange_name, exchange_type="topic", durable=True)
msg = 'WhySoSLow?'
while True:
    channel.basic_publish(exchange=exchange_name,routing_key="random", body=msg)

这让我得到了11K msgs/s 和 100% CPU 使用率的结果

对于 dotnet:

using RabbitMQ.Client;

var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
    channel.ExchangeDeclare(exchange: "test-load", durable: true, type: "topic");

    var routingKey = "random";
    var message = "WhySoSLow?";

    var body = Encoding.UTF8.GetBytes(message);
    while(true) 
    {
        channel.BasicPublish(   exchange: "test-load", 
                                routingKey: routingKey,
                                basicProperties: null,
                                body: body);

    }
}

此代码产生100K msgs/s 的结果,CPU 使用率为 30%

我究竟做错了什么?

标签: c#python-3.xperformancerabbitmqbenchmarking

解决方案


性能因语言而异。C++ 会更快。

C# 在编译和运行时都有更多的优化,而 pyton 必须一直处理装箱和拆箱。

也许您可以从上述评论之一中获得一些优化想法,但我怀疑您使用的库。


推荐阅读