From b74cffc25a82ee349745f1bf2dee6edb177530b6 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 3 May 2013 15:32:50 +0200 Subject: [PATCH] Bench for findone --- benchmarks/commonUtilities.js | 51 ++++++++++++++++++++++++----------- benchmarks/findOne.js | 27 +++++++++++++++++++ 2 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 benchmarks/findOne.js diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js index e459dc7..b7b2c6d 100644 --- a/benchmarks/commonUtilities.js +++ b/benchmarks/commonUtilities.js @@ -43,17 +43,14 @@ module.exports.getRandomArray = getRandomArray; /** * Insert a certain number of documents for testing - * @param {Datastore} d - * @param {Number} n - * @param {Profiler} profiler */ module.exports.insertDocs = function (d, n, profiler, cb) { var beg = new Date() - , i = 0; + ; profiler.step('Begin inserting ' + n + ' docs'); - function insertOne(i) { + function runFrom(i) { if (i === n) { // Finished console.log("Average time for one insert: " + (profiler.elapsedSinceLastStep() / n) + "ms"); profiler.step('Finished inserting ' + n + ' docs'); @@ -62,28 +59,25 @@ module.exports.insertDocs = function (d, n, profiler, cb) { d.insert({ docNumber: i }, function (err) { process.nextTick(function () { - insertOne(i + 1); + runFrom(i + 1); }); }); } - insertOne(0); + runFrom(0); }; /** - * Insert a certain number of documents for testing - * @param {Datastore} d - * @param {Number} n - * @param {Profiler} profiler + * Find documents with find */ module.exports.findDocs = function (d, n, profiler, cb) { var beg = new Date() , order = getRandomArray(n) - , i = 0; + ; profiler.step("Finding " + n + " documents"); - function find(i) { + function runFrom(i) { if (i === n) { // Finished console.log("Average time for one find in a collection of " + n + " docs: " + (profiler.elapsedSinceLastStep() / n) + "ms"); profiler.step('Finished finding ' + n + ' docs'); @@ -93,11 +87,38 @@ module.exports.findDocs = function (d, n, profiler, cb) { d.find({ docNumber: order[i] }, function (err, docs) { if (docs.length !== 1 || docs[0].docNumber !== order[i]) { return cb('One find didnt work'); } process.nextTick(function () { - find(i + 1); + runFrom(i + 1); }); }); } - find(0); + runFrom(0); }; +/** + * Find documents with findOne + */ +module.exports.findOneDocs = function (d, n, profiler, cb) { + var beg = new Date() + , order = getRandomArray(n) + ; + + profiler.step("FindingOne " + n + " documents"); + + function runFrom(i) { + if (i === n) { // Finished + console.log("Average time for one findOne in a collection of " + n + " docs: " + (profiler.elapsedSinceLastStep() / n) + "ms"); + profiler.step('Finished finding ' + n + ' docs'); + return cb(); + } + + d.findOne({ docNumber: order[i] }, function (err, doc) { + if (!doc || doc.docNumber !== order[i]) { return cb('One find didnt work'); } + process.nextTick(function () { + runFrom(i + 1); + }); + }); + } + runFrom(0); +}; + diff --git a/benchmarks/findOne.js b/benchmarks/findOne.js new file mode 100644 index 0000000..b126593 --- /dev/null +++ b/benchmarks/findOne.js @@ -0,0 +1,27 @@ +var Datastore = require('../lib/datastore') + , benchDb = 'workspace/find.bench.db' + , 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) + , 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) +, async.apply(commonUtilities.findOneDocs, d, n, profiler) +], function (err) { + profiler.step("Benchmark finished"); + + if (err) { return console.log("An error was encountered: ", err); } +});