From af9192881b31a1d4038494b1dd3b1938b28809c8 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 3 May 2013 00:38:04 +0200 Subject: [PATCH] Able to make the find bench pass through use of process.nexttick --- benchmarks/find.js | 66 ++++++++++++++++++++++++++++++-------------- benchmarks/insert.js | 22 ++++++++------- lib/datastore.js | 18 ++++++++---- 3 files changed, 70 insertions(+), 36 deletions(-) diff --git a/benchmarks/find.js b/benchmarks/find.js index 74028a5..6ecf3e4 100644 --- a/benchmarks/find.js +++ b/benchmarks/find.js @@ -36,18 +36,20 @@ async.waterfall([ console.log("Inserting " + n + " documents"); - async.whilst( function () { return i < n; } - , function (_cb) { + function insertOne(i) { + if (i === n) { // Finished + var timeTaken = (new Date()).getTime() - beg.getTime(); // In ms + console.log("Time taken: " + (timeTaken / 1000) + "s"); + return cb(); + } + d.insert({ docNumber: i }, function (err) { - i += 1; - return _cb(err); + process.nextTick(function () { + insertOne(i + 1); + }); }); - }, function (err) { - var timeTaken = (new Date()).getTime() - beg.getTime(); // In ms - if (err) { return cb(err); } - console.log("Time taken: " + (timeTaken / 1000) + "s"); - return cb(); - }); + } + insertOne(0); } , function (cb) { var beg = new Date() @@ -55,20 +57,42 @@ async.waterfall([ console.log("Finding " + n + " documents"); - async.whilst( function () { return i < n; } - , function (_cb) { + function find(i) { + if (i === n) { // Finished + var timeTaken = (new Date()).getTime() - beg.getTime(); // In ms + console.log("Time taken: " + (timeTaken / 1000) + "s"); + return cb(); + } + d.find({ docNumber: order[i] }, function (err, docs) { - i += 1; - if (docs.length !== 1) { return _cb(docs); } - return _cb(err); + if (docs.length !== 1) { return cb('One find didnt work'); } + process.nextTick(function () { + find(i + 1); + }); }); - }, function (err) { - var timeTaken = (new Date()).getTime() - beg.getTime(); // In ms - if (err) { return cb(err); } - console.log("Time taken: " + (timeTaken / 1000) + "s"); - return cb(); - }); + } + find(0); } +//, function (cb) { + //var beg = new Date() + //, i = 0; + + //console.log("Finding " + n + " documents"); + + //async.whilst( function () { return i < n; } + //, function (_cb) { + //d.find({ docNumber: order[i] }, function (err, docs) { + //i += 1; + //if (docs.length !== 1) { return _cb(docs); } + //return _cb(err); + //}); + //}, function (err) { + //var timeTaken = (new Date()).getTime() - beg.getTime(); // In ms + //if (err) { return cb(err); } + //console.log("Time taken: " + (timeTaken / 1000) + "s"); + //return cb(); + //}); + //} ], function (err) { console.log("Benchmark finished"); diff --git a/benchmarks/insert.js b/benchmarks/insert.js index 11abc00..cb222ee 100644 --- a/benchmarks/insert.js +++ b/benchmarks/insert.js @@ -34,18 +34,20 @@ async.waterfall([ console.log("Inserting " + n + " documents"); - async.whilst( function () { return i < n; } - , function (_cb) { + function insertOne(i) { + if (i === n) { // Finished + var timeTaken = (new Date()).getTime() - beg.getTime(); // In ms + console.log("Time taken: " + (timeTaken / 1000) + "s"); + return cb(); + } + d.insert({ docNumber: i }, function (err) { - i += 1; - return _cb(err); + process.nextTick(function () { + insertOne(i + 1); + }); }); - }, function (err) { - var timeTaken = (new Date()).getTime() - beg.getTime(); // In ms - if (err) { return cb(err); } - console.log("Time taken: " + (timeTaken / 1000) + "s"); - return cb(); - }); + } + insertOne(0); } ], function (err) { console.log("Benchmark finished"); diff --git a/lib/datastore.js b/lib/datastore.js index f6b1aa0..316c5c1 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -103,7 +103,14 @@ Datastore.prototype.insert = function (newDoc, cb) { * Check whether object is matched by the given query */ Datastore.match = function (obj, query) { - var match = true; + var match = true + //, queryKeys = Object.keys(query) + , i, k; + + //for (i = 0; i < queryKeys.length; i += 1) { + //k = queryKeys[i] + //if (obj[k] !== query[k]) { match = false; } + //} Object.keys(query).forEach(function (k) { if (obj[k] !== query[k]) { match = false; } @@ -120,13 +127,14 @@ Datastore.match = function (obj, query) { Datastore.prototype.find = function (query, callback) { var res = [] , self = this + , i ; - self.data.forEach(function (d) { - if (Datastore.match(d, query)) { - res.push(d); + for (i = 0; i < self.data.length; i += 1) { + if (Datastore.match(self.data[i], query)) { + res.push(self.data[i]); } - }); + } return callback(null, res); };