Able to make the find bench pass through use of process.nexttick

pull/2/head
Louis Chatriot 12 years ago
parent 063c2d04cb
commit af9192881b
  1. 66
      benchmarks/find.js
  2. 22
      benchmarks/insert.js
  3. 18
      lib/datastore.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");

@ -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");

@ -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);
};

Loading…
Cancel
Save