Search code examples
node.jsrabbitmqnode-amqp

strange low performance sending messages to queue with amqplib?


I'm trying to test Rabbitmq latency with amqp.node (https://github.com/squaremo/amqp.node).

This is the consumer:

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; });

And this is the producer:

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; });

Then I weak-up 3 instances of the consumer with:

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

And 3 producers:

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

and I get:

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

The first message of every producer has a low latency but the others no.

Is there something that I can do to improve this?


Solution

  • Try amqp.connect('amqp://localhost', { noDelay: true }), maybe need rabbitmq-server config. Detail at rabbitmq's Nagle Algorithm.