diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js index 9658826..f47479b 100644 --- a/benchmarks/commonUtilities.js +++ b/benchmarks/commonUtilities.js @@ -5,6 +5,7 @@ var customUtils = require('../lib/customUtils') , fs = require('fs') , path = require('path') + , Datastore = require('../lib/datastore') , executeAsap // process.nextTick or setImmediate depending on your Node version ; @@ -16,7 +17,40 @@ try { /** - * Ensure the workspace exists and the db is empty + * Configure the benchmark + */ +module.exports.getConfiguration = function (benchDb) { + var d, n + , program = require('commander') + ; + + program + .option('-n --number [number]', 'Size of the collection to test on', parseInt) + .option('-i --with-index', 'Use an index') + .option('-p --with-pipeline', 'Use pipelining') + .option('-m --in-memory', 'Test with an in-memory only store') + .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(program.withPipeline ? "Use an pipelining" : "Don't use pipelining"); + console.log(program.inMemory ? "Use an in-memory datastore" : "Use a persistent datastore"); + console.log("----------------------------"); + + d = new Datastore({ filename: benchDb + , pipeline: program.withPipeline + , inMemoryOnly: program.inMemory + }); + + return { n: n, d: d, program: program }; +} + + +/** + * Ensure the workspace exists and the db datafile is empty */ module.exports.prepareDb = function (filename, cb) { customUtils.ensureDirectoryExists(path.dirname(filename), function () { diff --git a/benchmarks/find.js b/benchmarks/find.js index e8f574e..21a57bd 100644 --- a/benchmarks/find.js +++ b/benchmarks/find.js @@ -3,32 +3,20 @@ var Datastore = require('../lib/datastore') , fs = require('fs') , path = require('path') , async = require('async') - , commonUtilities = require('./commonUtilities') , execTime = require('exec-time') , profiler = new execTime('FIND BENCH') - , d = new Datastore(benchDb) - , program = require('commander') - , n + , commonUtilities = require('./commonUtilities') + , config = commonUtilities.getConfiguration(benchDb) + , d = config.d + , n = config.n ; -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(function (err) { if (err) { return cb(err); } - if (program.withIndex) { d.ensureIndex({ fieldName: 'docNumber' }); } + if (config.program.withIndex) { d.ensureIndex({ fieldName: 'docNumber' }); } cb(); }); } diff --git a/benchmarks/findOne.js b/benchmarks/findOne.js index 64ec3d9..d70c636 100644 --- a/benchmarks/findOne.js +++ b/benchmarks/findOne.js @@ -3,37 +3,26 @@ var Datastore = require('../lib/datastore') , fs = require('fs') , path = require('path') , async = require('async') - , commonUtilities = require('./commonUtilities') , execTime = require('exec-time') , profiler = new execTime('FINDONE BENCH') - , d = new Datastore(benchDb) - , program = require('commander') - , n + , commonUtilities = require('./commonUtilities') + , config = commonUtilities.getConfiguration(benchDb) + , d = config.d + , n = config.n ; -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(function (err) { if (err) { return cb(err); } - if (program.withIndex) { d.ensureIndex({ fieldName: 'docNumber' }); } + if (config.program.withIndex) { d.ensureIndex({ fieldName: 'docNumber' }); } cb(); }); } , function (cb) { profiler.beginProfiling(); return cb(); } , async.apply(commonUtilities.insertDocs, d, n, profiler) +, function (cb) { setTimeout(function () {cb();}, 500); } , async.apply(commonUtilities.findOneDocs, d, n, profiler) ], function (err) { profiler.step("Benchmark finished"); diff --git a/benchmarks/insert.js b/benchmarks/insert.js index faac99b..44875ea 100644 --- a/benchmarks/insert.js +++ b/benchmarks/insert.js @@ -1,36 +1,20 @@ var Datastore = require('../lib/datastore') , benchDb = 'workspace/insert.bench.db' , async = require('async') - , commonUtilities = require('./commonUtilities') , execTime = require('exec-time') , profiler = new execTime('INSERT BENCH') - , d - , program = require('commander') - , n + , commonUtilities = require('./commonUtilities') + , config = commonUtilities.getConfiguration(benchDb) + , d = config.d + , n = config.n ; -program - .option('-n --number [number]', 'Size of the collection to test on', parseInt) - .option('-i --with-index', 'Use an index') - .option('-p --with-pipeline', 'Use pipelining') - .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(program.withPipeline ? "Use an pipelining" : "Don't use pipelining"); -console.log("----------------------------"); - -d = new Datastore({ filename: benchDb, pipeline: program.withPipeline }); - async.waterfall([ async.apply(commonUtilities.prepareDb, benchDb) , function (cb) { d.loadDatabase(function (err) { if (err) { return cb(err); } - if (program.withIndex) { + if (config.program.withIndex) { d.ensureIndex({ fieldName: 'docNumber' }); n = 2 * n; // We will actually insert twice as many documents // because the index is slower when the collection is already diff --git a/benchmarks/remove.js b/benchmarks/remove.js index 754c3b2..1a471d6 100644 --- a/benchmarks/remove.js +++ b/benchmarks/remove.js @@ -3,34 +3,20 @@ var Datastore = require('../lib/datastore') , 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) - , program = require('commander') - , n + , commonUtilities = require('./commonUtilities') + , config = commonUtilities.getConfiguration(benchDb) + , d = config.d + , n = config.n ; -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(function (err) { if (err) { return cb(err); } - if (program.withIndex) { - d.ensureIndex({ fieldName: 'docNumber' }); - } + if (config.program.withIndex) { d.ensureIndex({ fieldName: 'docNumber' }); } cb(); }); } diff --git a/benchmarks/update.js b/benchmarks/update.js index b9a3c27..f016f80 100644 --- a/benchmarks/update.js +++ b/benchmarks/update.js @@ -3,42 +3,26 @@ var Datastore = require('../lib/datastore') , fs = require('fs') , path = require('path') , async = require('async') - , commonUtilities = require('./commonUtilities') , execTime = require('exec-time') , profiler = new execTime('UPDATE BENCH') - , d = new Datastore(benchDb) - , program = require('commander') - , n + , commonUtilities = require('./commonUtilities') + , config = commonUtilities.getConfiguration(benchDb) + , d = config.d + , n = config.n ; -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(function (err) { if (err) { return cb(err); } - if (program.withIndex) { - d.ensureIndex({ fieldName: 'docNumber' }); - } + if (config.program.withIndex) { d.ensureIndex({ fieldName: 'docNumber' }); } cb(); }); } , function (cb) { profiler.beginProfiling(); return cb(); } , async.apply(commonUtilities.insertDocs, d, n, profiler) -// CHECK THAT MULTIPLE LOAD DATABASE DONT SCREW INDEXES - // Test with update only one document , function (cb) { profiler.step('MULTI: FALSE'); return cb(); } , async.apply(commonUtilities.updateDocs, { multi: false }, d, n, profiler) diff --git a/lib/datastore.js b/lib/datastore.js index 26a58ac..2cf4f0e 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -250,7 +250,11 @@ Datastore.prototype._loadDatabase = function (cb) { }; Datastore.prototype.loadDatabase = function () { - this.executor.push({ this: this, fn: this._loadDatabase, arguments: arguments }); + if (this.pipeline) { + this.persistenceExecutor.push({ this: this, fn: this._loadDatabase, arguments: arguments }); + } else { + this.executor.push({ this: this, fn: this._loadDatabase, arguments: arguments }); + } }; @@ -337,12 +341,19 @@ Datastore.prototype._persistNewState = function (newDocs, cb) { }); }; Datastore.prototype.persistNewState = function (newDocs, cb) { + if (this.inMemoryOnly) { + cb(); // No persistence + return; + } + if (this.pipeline) { this.persistenceExecutor.push({ this: this, fn: this._persistNewState, arguments: [newDocs] }); cb (); // Return right away with no error - } else { - this._persistNewState(newDocs, cb); + return; } + + // Default behaviour + this._persistNewState(newDocs, cb); };