使用 amqplib 将消息发送到队列的奇怪低性能?
strange low performance sending messages to queue with amqplib?
我正在尝试使用 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
每个生产者的第一条消息都有低延迟,但其他的没有。
我可以做些什么来改进它吗?
尝试 amqp.connect('amqp://localhost', { noDelay: true })
,可能需要 rabbitmq-server 配置。详情见 rabbitmq's Nagle Algorithm.
我正在尝试使用 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
每个生产者的第一条消息都有低延迟,但其他的没有。
我可以做些什么来改进它吗?
尝试 amqp.connect('amqp://localhost', { noDelay: true })
,可能需要 rabbitmq-server 配置。详情见 rabbitmq's Nagle Algorithm.