Вопрос

I'm running the node.js amqp-node client on a RabbitMQ server. I've set up a simple publish/consume test in two different ways - each produces a different latency and I can't work out why. The latency is measured from the time the message was sent to the time it was delivered.

The first method uses two separate processes - one for the publishing app - one for the consuming app. It produces a latency of about at 4ms. The second method uses just one app to produce and consume. This produces a latency of about 40ms. Can anyone explain this behaviour to me why? Thank you for your time.

Method #1 Producer:

var amqp = require('amqp'),
    connection = amqp.createConnection({host:'localhost'}),
    testExchange = {};

connection.on('ready', function(){
    testExchange = connection.exchange('testExchange', {type:'topic', autoDelete:true});
    testMessage();
});

function testMessage(){
    console.log('message sent');
    testExchange.publish('test.message', { msg:'testMessage', time: new Date().getTime() });
    setTimeout(testMessage, 500);
}

Method #1 Consumer

var amqp = require('amqp'),
    connection = amqp.createConnection({host:'localhost'}),

connection.on('ready', function(){
    var testExchange = connection.exchange('testExchange', {type:'topic', autoDelete:true});
    var testQ = connection.queue('testQ', function(queue){
        queue.bind('testExchange', 'test.#');
        queue.subscribe( function(message){
            console.log('message received');
            var now = new Date().getTime();
            console.log(now-message.time);
        });
    });
});

Method #2 Producer and Consumer

var amqp = require('amqp'),
    connection = amqp.createConnection({host:'localhost'}),
    testExchange = {};

connection.on('ready', function(){
    testExchange = connection.exchange('testExchange', {type:'topic', autoDelete:true});
    var testQ = connection.queue('testQ', function(queue){
        queue.bind('testExchange', 'test.#');
        queue.subscribe( function(message){
            console.log('message received');
            var now = new Date().getTime();
            console.log(now-message.time);
        });
        testMessage();
    });
});

function testMessage(){
    console.log('message sent');
    testExchange.publish('test.message', { msg:'testMessage', time: new Date().getTime() });
    setTimeout(testMessage, 500);
}
Это было полезно?

Решение

In the first method you have two Independent connections to the RabbitMQ server, where as in the second method you are sharing the same connection for the producer (sender) and consumer (receiver). Admittedly your message rate of 2 per second is pretty low given the localhost RabbitMQ server so we can discount any networking delays, but I suspect RabbitMQ or the node.js amqp implementation slows down things when senders and receivers are multiplexed on the same connection.

Cheers!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top