0

我正在尝试使用 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

每个生产者的第一条消息具有低延迟,但其他消息没有。

我可以做些什么来改善这一点吗?

4

1 回答 1

2

试试看amqp.connect('amqp://localhost', { noDelay: true }),也许需要 rabbitmq-server 配置。rabbitmq 的 Nagle 算法的详细信息。

于 2019-06-03T05:55:55.250 回答