Skip to content

Instantly share code, notes, and snippets.

@StarpTech
Last active January 18, 2017 19:03
Show Gist options
  • Save StarpTech/cd649380f5aebc4f9410b41fb04b2de8 to your computer and use it in GitHub Desktop.
Save StarpTech/cd649380f5aebc4f9410b41fb04b2de8 to your computer and use it in GitHub Desktop.
Memory leak Node-Nats
var nats = require('../lib/nats').connect();
///////////////////////////////////////
// Publish Performance
//
// Setup:
// NodeJs: 6.9.2, Windows 10
///////////////////////////////////////
var loop = 2000000;
var hash = 2500;
console.log('Publish Performance Test');
nats.on('connect', function() {
var start = new Date();
var invalid2octet = new Buffer('\xc3\x28', 'binary');
for (var i=0; i<loop; i++) {
//nats.publish('test', invalid2octet); //old version
nats.publish('test', invalid2octet, function () {}); //with callback
//nats.publish('test', 'ok');
if (i % hash === 0) {
process.stdout.write('+');
}
}
nats.flush(function() {
var stop = new Date();
var mps = parseInt(loop/((stop-start)/1000));
console.log('\nPublished at ' + mps + ' msgs/sec');
process.exit();
});
});
@derekcollison
Copy link

So you are queueing up a stack of functions to be processed when the publish completes. The way this works is that the PUB is sent and followed by a PING. The server eventually sends a PONG which will pull the head from the queue of functions. No messages or PONG responses can be received until the publishing for loop finishes, hence the memory buildup.

You could do a simple flush at the end as is coded which will guarantee that when that callback runs all messages have been received and processed by the server. Otherwise I would recommend the for loop be bound by a small number a controlled with a repeating timer to allow the inbound socket to be processed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment