From f16b7a9ab9db45d1070f6cf1801e40614c98bb6d Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 3 May 2013 16:20:39 +0200 Subject: [PATCH] Benchmark removes too --- benchmarks/commonUtilities.js | 32 ++++++++++++++++++++++++++++++++ benchmarks/remove.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 benchmarks/remove.js diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js index 72f2d07..494acad 100644 --- a/benchmarks/commonUtilities.js +++ b/benchmarks/commonUtilities.js @@ -152,6 +152,38 @@ module.exports.updateDocs = function (options, d, n, profiler, cb) { }; +/** + * Remove documents + * options is the same as the options object for update + */ +module.exports.removeDocs = function (options, d, n, profiler, cb) { + var beg = new Date() + , order = getRandomArray(n) + ; + + profiler.step("Removing " + n + " documents"); + + function runFrom(i) { + if (i === n) { // Finished + console.log("Average time for one remove in a collection of " + n + " docs: " + (profiler.elapsedSinceLastStep() / n) + "ms"); + profiler.step('Finished removing ' + n + ' docs'); + return cb(); + } + + d.remove({ docNumber: order[i] }, options, function (err, nr) { + if (nr !== 1) { return cb('One remove didnt work'); } + d.insert({ docNumber: order[i] }, function (err) { // Reinserting just removed document so that the collection size doesn't change + // Time is about 70x smaller for an insert so the impact on the results is minimal + process.nextTick(function () { + runFrom(i + 1); + }); + }); + }); + } + runFrom(0); +}; + + diff --git a/benchmarks/remove.js b/benchmarks/remove.js new file mode 100644 index 0000000..b07eda0 --- /dev/null +++ b/benchmarks/remove.js @@ -0,0 +1,35 @@ +var Datastore = require('../lib/datastore') + , benchDb = 'workspace/remove.bench.db' + , fs = require('fs') + , path = require('path') + , async = require('async') + , commonUtilities = require('./commonUtilities') + , execTime = require('exec-time') + , profiler = new execTime('REMOVE BENCH') + , d = new Datastore(benchDb) + , n = 10000 + ; + +if (process.argv[2]) { n = parseInt(process.argv[2], 10); } + +async.waterfall([ + async.apply(commonUtilities.prepareDb, benchDb) +, function (cb) { d.loadDatabase(cb); } +, function (cb) { profiler.beginProfiling(); return cb(); } +, async.apply(commonUtilities.insertDocs, d, n, profiler) + +// Test with remove only one document +, function (cb) { profiler.step('MULTI: FALSE'); return cb(); } +, async.apply(commonUtilities.removeDocs, { multi: false }, d, n, profiler) + +// Test with multiple documents +//, async.apply(commonUtilities.prepareDb, benchDb) +//, function (cb) { d.loadDatabase(cb); } +//, async.apply(commonUtilities.insertDocs, d, n, profiler) +//, function (cb) { profiler.step('MULTI: TRUE'); return cb(); } +//, async.apply(commonUtilities.updateDocs, { multi: true }, d, n, profiler) +], function (err) { + profiler.step("Benchmark finished"); + + if (err) { return console.log("An error was encountered: ", err); } +});