diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js new file mode 100644 index 0000000..f66a1d7 --- /dev/null +++ b/benchmarks/commonUtilities.js @@ -0,0 +1,73 @@ +/** + * Functions that are used in several benchmark tests + */ + +var customUtils = require('../lib/customUtils') + , fs = require('fs') + , path = require('path') + ; + + +/** + * Ensure the workspace exists and the db is empty + */ +module.exports.prepareDb = function (filename, cb) { + customUtils.ensureDirectoryExists(path.dirname(filename), function () { + fs.exists(filename, function (exists) { + if (exists) { + fs.unlink(filename, cb); + } else { return cb(); } + }); + }); +}; + + +/** + * Return an array with the numbers from 0 to n-1, in a random order + * Useful to get fair tests + */ +module.exports.getRandomArray = function (n) { + var res, next; + + if (n === 0) { return []; } + if (n === 1) { return [0]; } + + res = getRandomArray(n - 1); + next = Math.floor(Math.random() * n); + res.splice(next, 0, n - 1); // Add n-1 at a random position in the array + + return res; +}; + + +/** + * 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) { + if (i === n) { // Finished + console.log("Average time for one insert: " + (profiler.elapsedSinceLastStep() / n) + "ms"); + profiler.step('Finished inserting ' + n + ' docs'); + return cb(); + } + + d.insert({ docNumber: i }, function (err) { + process.nextTick(function () { + insertOne(i + 1); + }); + }); + } + insertOne(0); +}; + + + + diff --git a/benchmarks/insert.js b/benchmarks/insert.js index cc9273a..effe1d5 100644 --- a/benchmarks/insert.js +++ b/benchmarks/insert.js @@ -1,57 +1,24 @@ var Datastore = require('../lib/datastore') , benchDb = 'workspace/insert.bench.db' - , fs = require('fs') - , path = require('path') , async = require('async') - , customUtils = require('../lib/customUtils') + , commonUtilities = require('./commonUtilities') + , execTime = require('exec-time') + , profiler = new execTime('INSERT BENCH') , n = 10000 - , d + , d = new Datastore(benchDb) ; if (process.argv[2]) { n = parseInt(process.argv[2], 10); } -console.log("Benchmarking insert"); - async.waterfall([ - function (cb) { - console.log("Preparing database"); - - customUtils.ensureDirectoryExists(path.dirname(benchDb), function () { - fs.exists(benchDb, function (exists) { - if (exists) { - fs.unlink(benchDb, cb); - } else { return cb(); } - }); - }); - } + async.apply(commonUtilities.prepareDb, benchDb) , function (cb) { - d = new Datastore(benchDb); d.loadDatabase(cb); } -, function (cb) { - var beg = new Date() - , i = 0; - - console.log("Inserting " + n + " documents"); - - function insertOne(i) { - if (i === n) { // Finished - var timeTaken = (new Date()).getTime() - beg.getTime(); // In ms - console.log("Time taken: " + (timeTaken / 1000) + "s"); - console.log("Average time for one insert: " + (timeTaken / n) + "ms"); - return cb(); - } - - d.insert({ docNumber: i }, function (err) { - process.nextTick(function () { - insertOne(i + 1); - }); - }); - } - insertOne(0); - } +, function (cb) { profiler.beginProfiling(); return cb(); } +, async.apply(commonUtilities.insertDocs, d, n, profiler) ], function (err) { - console.log("Benchmark finished"); + profiler.step("Benchmark finished"); if (err) { return console.log("An error was encountered: ", err); } }); diff --git a/lib/customUtils.js b/lib/customUtils.js index 1dfa06e..c6aa455 100644 --- a/lib/customUtils.js +++ b/lib/customUtils.js @@ -57,26 +57,7 @@ function deepCopy (obj) { } -/** - * Return an array with the numbers from 0 to n-1, in a random order - * Used in the benchmarks - */ -function getRandomArray (n) { - var res, next; - - if (n === 0) { return []; } - if (n === 1) { return [0]; } - - res = getRandomArray(n - 1); - next = Math.floor(Math.random() * n); - res.splice(next, 0, n - 1); // Add n-1 at a random position in the array - - return res; -} - - module.exports.ensureDirectoryExists = ensureDirectoryExists; module.exports.uid = uid; module.exports.deepCopy = deepCopy; -module.exports.getRandomArray = getRandomArray; diff --git a/package.json b/package.json index 5aa83a5..f5b7ca2 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "chai": "1.0.x", "mocha": "1.4.x", "request": "2.9.x", - "sinon": "1.3.x" + "sinon": "1.3.x", + "exec-time": "0.0.2" }, "scripts": { "test": "make test"