node-mongolian icon indicating copy to clipboard operation
node-mongolian copied to clipboard

Performance issues on linux systems

Open Cowboy-coder opened this issue 13 years ago • 4 comments

For some time we have noticed that our test suite we are running takes like 19x longer to run on linux systems and the bottleneck seems to be in Mongolian when doing inserts. To test this we wrote a single test-file that we run on different systems

On OS X it takes 5-7 ms On Linux systems it takes 42-43ms

First run will not give a reliable result (because it needs to create the collection and the database and such). In our result posted above the first run is ignored

var Mongolian = require("mongolian")

// Create a server instance with default host and port
var server = new Mongolian

// Get database
var db = server.db("blog")

// Get some collections
var posts = db.collection("posts")
var comments = db.collection("comments")

startTime = new Date()
// Insert some data
posts.insert({
    pageId: "hallo",
    title: "Hallo",
    created: new Date,
    body: "Welcome to my new blog!"
}, function(err, post){
    console.log("time to insert (ms): ", (new Date()) - startTime);
    process.exit();
})

Since this seems to be very similar to this issue for node-mongodb-native https://github.com/christkv/node-mongodb-native/issues/445 I tried to apply the exact same patch (switch new Socket() to net.createConnection() ) and that actually seems to solve the issue.

We got the same result for the test file above (5-7ms) in both OS X and Linux and our test suite went from ~19 seconds down to ~660ms on Linux systems. The performance on OS X was unaffected.

I could probably write a pull request for this but I prefer if this is fixed by you (@marcello3d) because It seems like lib/connection.js will need some refactorings in order to use net.createConnection().

Anyway... let me know what you think!

Cowboy-coder avatar Jan 20 '12 13:01 Cowboy-coder

This is very bizarre. I wonder why there is a performance difference, because if you look at the source for net.createConnection:

exports.connect = exports.createConnection = function(port /* [host], [cb] */) {
  var s;

  if (isPipeName(port)) {
    s = new Socket({ handle: createPipe() });
  } else {
    s = new Socket();
  }

  return s.connect(port, arguments[1], arguments[2]);
};

And on 0.4.12:

exports.createConnection = function(port, host) {
  var s = new Socket();
  s.connect(port, host);
  return s;
};

Can you submit your pull request anyway?

marcello3d avatar Jan 21 '12 19:01 marcello3d

I've sent a pull request. Let me know what you think.

Cowboy-coder avatar Jan 23 '12 11:01 Cowboy-coder

I've also created a ticket on node.js issue tracker. https://github.com/joyent/node/issues/2597

Cowboy-coder avatar Jan 23 '12 14:01 Cowboy-coder

For kicks, is it possible that the order in which the listeners and commands are added is affecting the speed? Did you try using your new patch and literally inlining the net.createConnection function body? (e.g. reverse patch on your patch)

marcello3d avatar Jan 28 '12 18:01 marcello3d