Skip to content

Commit

Permalink
Use the profiler to time the experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed May 3, 2013
1 parent 2c59251 commit b494958
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 61 deletions.
73 changes: 73 additions & 0 deletions benchmarks/commonUtilities.js
Original file line number Diff line number Diff line change
@@ -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);
};




49 changes: 8 additions & 41 deletions benchmarks/insert.js
Original file line number Diff line number Diff line change
@@ -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); }
});
19 changes: 0 additions & 19 deletions lib/customUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b494958

Please sign in to comment.