奇怪的低性能发送消息与amqplib排队?

时间:2019-06-02 11:01:01

标签: node.js rabbitmq node-amqp

我正在尝试使用amqp.node(https://github.com/squaremo/amqp.node)测试Rabbitmq延迟。

这是消费者:

import amqp from 'amqplib';

amqp.connect('amqp://localhost').then((connection) => {
  connection.createChannel().then((channel) => {
    const queue = 'test1';
    channel.assertQueue(queue, { durable: false }).then(()=>{
      var consumedMessages = 0;
      channel.consume(queue, (msg) => {
        const timestamp = new Date().getTime();
        consumedMessages++;
        if (msg != null) console.log(`consumed ${consumedMessages}: ${timestamp - Number(msg.content.toString())}`);
      }, { noAck: true });
    });
  }).catch((error) => { throw error; });
}).catch((error) => { throw error; });

这是制作人:

import amqp from 'amqplib';

amqp.connect('amqp://localhost').then((connection) => {
  connection.createChannel().then((channel) => {
    const queue = 'test1';
    const totalMessages = 3;

    channel.assertQueue(queue, { durable: false }).then(()=>{    
      for(var n = 0; n < totalMessages; n++){
        channel.sendToQueue(queue, Buffer.from(new Date().getTime().toString()));
      }
      setTimeout(()=>{
        connection.close();
        process.exit(0);
      }, 1000);
    });
  }).catch((error) => { throw error; });
}).catch((error) => { throw error; });

然后我用以下方法削弱了3个消费者实例:

for ((i=1;i<=3;i+=1)); do node dist/consumer.js & done

还有3个制作人:

for ((i=1;i<=3;i+=1)); do node dist/producer.js & done

我得到:

consumed 1: 8
consumed 1: 6
consumed 1: 10
consumed 2: 42
consumed 2: 43
consumed 3: 42
consumed 2: 42
consumed 3: 42
consumed 3: 42

每个生产者的第一个消息都具有较低的延迟,而其他生产者则没有。

有什么我可以做些改善的事情吗?

1 个答案:

答案 0 :(得分:1)

尝试amqp.connect('amqp://localhost', { noDelay: true }),可能需要Rabbitmq-server配置。详细信息,rabbitmq's Nagle Algorithm