marcello3d / node-mongolian

[project inactive] Mongolian DeadBeef is an awesome Mongo DB driver for node.js
https://groups.google.com/group/node-mongolian
zlib License
349 stars 50 forks source link

Performance issues on linux systems #79

Open Cowboy-coder opened 12 years ago

Cowboy-coder commented 12 years ago

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!

marcello3d commented 12 years ago

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?

Cowboy-coder commented 12 years ago

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

Cowboy-coder commented 12 years ago

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

marcello3d commented 12 years ago

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)