Skip to content

Commit

Permalink
Benchmark for update done
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed May 29, 2013
1 parent 77b95ce commit 4d7a382
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
3 changes: 2 additions & 1 deletion benchmarks/commonUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ module.exports.updateDocs = function (options, d, n, profiler, cb) {
return cb();
}

d.update({ docNumber: order[i] }, { newDocNumber: i }, options, function (err, nr) {
// Will not actually modify the document but will take the same time
d.update({ docNumber: order[i] }, { docNumber: order[i] }, options, function (err, nr) {
if (nr !== 1) { return cb('One update didnt work'); }
executeAsap(function () {
runFrom(i + 1);
Expand Down
1 change: 0 additions & 1 deletion benchmarks/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ console.log("Test with " + n + " documents");
console.log(program.withIndex ? "Use an index" : "Don't use an index");
console.log("----------------------------");


async.waterfall([
async.apply(commonUtilities.prepareDb, benchDb)
, function (cb) {
Expand Down
25 changes: 22 additions & 3 deletions benchmarks/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,33 @@ var Datastore = require('../lib/datastore')
, execTime = require('exec-time')
, profiler = new execTime('REMOVE BENCH')
, d = new Datastore(benchDb)
, n = 10000
, program = require('commander')
, n
;

if (process.argv[2]) { n = parseInt(process.argv[2], 10); }
program
.option('-n --number [number]', 'Size of the collection to test on', parseInt)
.option('-i --with-index', 'Test with an index')
.parse(process.argv);

n = program.number || 10000;

console.log("----------------------------");
console.log("Test with " + n + " documents");
console.log(program.withIndex ? "Use an index" : "Don't use an index");
console.log("----------------------------");

async.waterfall([
async.apply(commonUtilities.prepareDb, benchDb)
, function (cb) { d.loadDatabase(cb); }
, function (cb) {
d.loadDatabase(function (err) {
if (err) { return cb(err); }
if (program.withIndex) {
d.ensureIndex({ fieldName: 'docNumber' });
}
cb();
});
}
, function (cb) { profiler.beginProfiling(); return cb(); }
, async.apply(commonUtilities.insertDocs, d, n, profiler)

Expand Down
25 changes: 22 additions & 3 deletions benchmarks/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,33 @@ var Datastore = require('../lib/datastore')
, execTime = require('exec-time')
, profiler = new execTime('UPDATE BENCH')
, d = new Datastore(benchDb)
, n = 10000
, program = require('commander')
, n
;

if (process.argv[2]) { n = parseInt(process.argv[2], 10); }
program
.option('-n --number [number]', 'Size of the collection to test on', parseInt)
.option('-i --with-index', 'Test with an index')
.parse(process.argv);

n = program.number || 10000;

console.log("----------------------------");
console.log("Test with " + n + " documents");
console.log(program.withIndex ? "Use an index" : "Don't use an index");
console.log("----------------------------");

async.waterfall([
async.apply(commonUtilities.prepareDb, benchDb)
, function (cb) { d.loadDatabase(cb); }
, function (cb) {
d.loadDatabase(function (err) {
if (err) { return cb(err); }
if (program.withIndex) {
d.ensureIndex({ fieldName: 'docNumber' });
}
cb();
});
}
, function (cb) { profiler.beginProfiling(); return cb(); }
, async.apply(commonUtilities.insertDocs, d, n, profiler)

Expand Down
12 changes: 8 additions & 4 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
, numReplaced = 0
, multi, upsert
, updatedDocs = []
, candidates
, i
;

Expand Down Expand Up @@ -367,12 +368,14 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
});
}
, function () { // Perform the update
candidates = self.getCandidates(query)

try {
for (i = 0; i < self.data.length; i += 1) {
if (model.match(self.data[i], query) && (multi || numReplaced === 0)) {
for (i = 0; i < candidates.length; i += 1) {
if (model.match(candidates[i], query) && (multi || numReplaced === 0)) {
numReplaced += 1;
self.data[i] = model.modify(self.data[i], updateQuery);
updatedDocs.push(self.data[i]);
candidates[i] = model.modify(candidates[i], updateQuery);
updatedDocs.push(candidates[i]);
}
}
} catch (err) {
Expand Down Expand Up @@ -404,6 +407,7 @@ Datastore.prototype.update = function () {
Datastore.prototype._remove = function (query, options, cb) {
var callback
, self = this
, candidates = this.getCandidates(query)
, numRemoved = 0
, multi
, newData = []
Expand Down

0 comments on commit 4d7a382

Please sign in to comment.