From 77b95ce7dc4eb6adca7e15f9690c2b8904edde7d Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Wed, 29 May 2013 18:28:06 +0200 Subject: [PATCH] More honest benchmark for insert --- benchmarks/commonUtilities.js | 17 +++++++++++------ benchmarks/insert.js | 9 ++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js index 05a4a73..7224ef5 100644 --- a/benchmarks/commonUtilities.js +++ b/benchmarks/commonUtilities.js @@ -31,17 +31,22 @@ module.exports.prepareDb = function (filename, cb) { /** * Return an array with the numbers from 0 to n-1, in a random order + * Uses Fisher Yates algorithm * Useful to get fair tests */ function getRandomArray (n) { - var res, next; + var res = [] + , i, j, temp + ; - if (n === 0) { return []; } - if (n === 1) { return [0]; } + for (i = 0; i < n; i += 1) { res[i] = i; } - 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 + for (i = n - 1; i >= 1; i -= 1) { + j = Math.floor((i + 1) * Math.random()); + temp = res[i]; + res[i] = res[j]; + res[j] = temp; + } return res; }; diff --git a/benchmarks/insert.js b/benchmarks/insert.js index bcf2de5..e10797c 100644 --- a/benchmarks/insert.js +++ b/benchmarks/insert.js @@ -21,12 +21,19 @@ 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 (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 + // big. So the result given by the algorithm will be a bit worse than + // actual performance + } cb(); }); }