postwait/node-amqp — Issue #72: Publish performance is very slow
Author: prystupa
Created: 2012-03-03 22:17:13+00:00
State: open

We have severe performance problems with node-amqp when under load in production. To reproduce I created this little publisher program:

```JavaScript

var amqp = require('amqp');

var conn = amqp.createConnection({url: "amqp://guest:guest@localhost:5672"});
conn.on('ready', function(){

for(var i = 0; i < 50000; i++){
conn.publish('labs', {my: "paylaod" + i});
}

console.log('published ' + i + ' messages');
});
```

On the other end of RabbitMQ this little program reads messages as they are published and display some timing statistics:

```JavaScript
var amqp = require('amqp');

var conn = amqp.createConnection({url: "amqp://guest:guest@localhost:5672"});

conn.on('ready', function(){
var n = 0;
conn.queue('labs', function(queue){
var prev = new Date().getTime();
queue.subscribe(function (message, headers, deliveryInfo) {
n++;
if(n % 1000 === 0){
var next = new Date().getTime();
console.log('read ' + n + ' messages in ' + (next - prev) + " ms");
prev = next;
}
});
});
});
```

As you can see the test run results show that performance is significantly deteriorated as more and more messages are published:

```
read 1000 messages in 352 ms
read 2000 messages in 395 ms
read 3000 messages in 418 ms
read 4000 messages in 500 ms
read 5000 messages in 459 ms
read 6000 messages in 479 ms
read 7000 messages in 469 ms
...
read 46000 messages in 2778 ms
read 47000 messages in 2767 ms
read 48000 messages in 2818 ms
read 49000 messages in 2814 ms
read 50000 messages in 2847 ms
```

I re-wrote the publisher program in Python using pika, for comparison:

```Python

import pika

def main():
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

for i in range(50000):
channel.basic_publish(
exchange='',
routing_key='labs',
body='{"my": "payload"}')

print " [x] Sent " + str(i) + " messages"

connection.close()

if __name__ == '__main__':
main()
```

As you can see the test run with pika shows much better performance that stays constant for the whole run:

```
read 1000 messages in 232 ms
read 2000 messages in 235 ms
read 3000 messages in 221 ms
read 4000 messages in 223 ms
read 5000 messages in 227 ms
read 6000 messages in 224 ms
read 7000 messages in 225 ms
...
read 46000 messages in 237 ms
read 47000 messages in 218 ms
read 48000 messages in 247 ms
read 49000 messages in 225 ms
read 50000 messages in 229 ms
```

Would you be able to look into why the difference in performance between two technologies is so significant and if this can be addressed? Thanks!