diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js index 9612006..ae384b4 100644 --- a/benchmarks/commonUtilities.js +++ b/benchmarks/commonUtilities.js @@ -124,7 +124,7 @@ module.exports.findDocs = function (d, n, profiler, cb) { function runFrom(i) { if (i === n) { // Finished - console.log("===== RESULT (find) ===== " + Math.floor(1000* n / profiler.elapsedSinceLastStep()) + " ops/s"); + console.log("===== RESULT (find with in selector) ===== " + Math.floor(1000* n / profiler.elapsedSinceLastStep()) + " ops/s"); profiler.step('Finished finding ' + n + ' docs'); return cb(); } @@ -140,6 +140,45 @@ module.exports.findDocs = function (d, n, profiler, cb) { }; +/** + * Find documents with find and the $in operator + */ +module.exports.findDocsWithIn = function (d, n, profiler, cb) { + var beg = new Date() + , order = getRandomArray(n) + , ins = [], i, j + , arraySize = Math.min(10, n) // The array for $in needs to be smaller than n (inclusive) + ; + + // Preparing all the $in arrays, will take some time + for (i = 0; i < n; i += 1) { + ins[i] = []; + + for (j = 0; j < arraySize; j += 1) { + ins[i].push((i + j) % n); + } + } + + profiler.step("Finding " + n + " documents WITH $IN OPERATOR"); + + function runFrom(i) { + if (i === n) { // Finished + console.log("===== RESULT (find with in selector) ===== " + Math.floor(1000* n / profiler.elapsedSinceLastStep()) + " ops/s"); + profiler.step('Finished finding ' + n + ' docs'); + return cb(); + } + + d.find({ docNumber: { $in: ins[i] } }, function (err, docs) { + if (docs.length !== arraySize) { return cb('One find didnt work'); } + executeAsap(function () { + runFrom(i + 1); + }); + }); + } + runFrom(0); +}; + + /** * Find documents with findOne */ diff --git a/benchmarks/findWithIn.js b/benchmarks/findWithIn.js new file mode 100644 index 0000000..9777ab7 --- /dev/null +++ b/benchmarks/findWithIn.js @@ -0,0 +1,30 @@ +var Datastore = require('../lib/datastore') + , benchDb = 'workspace/find.bench.db' + , fs = require('fs') + , path = require('path') + , async = require('async') + , execTime = require('exec-time') + , profiler = new execTime('FIND BENCH') + , commonUtilities = require('./commonUtilities') + , config = commonUtilities.getConfiguration(benchDb) + , d = config.d + , n = config.n + ; + +async.waterfall([ + async.apply(commonUtilities.prepareDb, benchDb) +, function (cb) { + d.loadDatabase(function (err) { + if (err) { return cb(err); } + if (config.program.withIndex) { d.ensureIndex({ fieldName: 'docNumber' }); } + cb(); + }); + } +, function (cb) { profiler.beginProfiling(); return cb(); } +, async.apply(commonUtilities.insertDocs, d, n, profiler) +, async.apply(commonUtilities.findDocsWithIn, d, n, profiler) +], function (err) { + profiler.step("Benchmark finished"); + + if (err) { return console.log("An error was encountered: ", err); } +});