From f2f4acc29b8c2bcba76a60e04a8bb36ea4ee5587 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 3 May 2013 15:15:12 +0200 Subject: [PATCH] Factorized benchmarks code --- benchmarks/commonUtilities.js | 32 +++++++++++++++- benchmarks/find.js | 72 +++++------------------------------ 2 files changed, 40 insertions(+), 64 deletions(-) diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js index f66a1d7..e459dc7 100644 --- a/benchmarks/commonUtilities.js +++ b/benchmarks/commonUtilities.js @@ -26,7 +26,7 @@ module.exports.prepareDb = function (filename, 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) { +function getRandomArray (n) { var res, next; if (n === 0) { return []; } @@ -38,6 +38,7 @@ module.exports.getRandomArray = function (n) { return res; }; +module.exports.getRandomArray = getRandomArray; /** @@ -69,5 +70,34 @@ module.exports.insertDocs = function (d, n, profiler, cb) { }; +/** + * Insert a certain number of documents for testing + * @param {Datastore} d + * @param {Number} n + * @param {Profiler} profiler + */ +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) { + 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'); + return 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); + }); + }); + } + find(0); +}; diff --git a/benchmarks/find.js b/benchmarks/find.js index 7583cbd..bd1be2c 100644 --- a/benchmarks/find.js +++ b/benchmarks/find.js @@ -3,79 +3,25 @@ var Datastore = require('../lib/datastore') , fs = require('fs') , path = require('path') , async = require('async') - , customUtils = require('../lib/customUtils') - , d + , commonUtilities = require('./commonUtilities') + , execTime = require('exec-time') + , profiler = new execTime('FIND BENCH') + , d = new Datastore(benchDb) , n = 10000 - , order ; if (process.argv[2]) { n = parseInt(process.argv[2], 10); } -order = customUtils.getRandomArray(n) - -console.log("Benchmarking find"); 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"); - return cb(); - } - - d.insert({ docNumber: i }, function (err) { - process.nextTick(function () { - insertOne(i + 1); - }); - }); - } - insertOne(0); - } -, function (cb) { - var beg = new Date() - , i = 0; - - console.log("Finding " + n + " documents"); - - function find(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 to find docs in a collection of " + n + " documents: " + (timeTaken / n) + "ms"); - return 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); - }); - }); - } - find(0); - } +, function (cb) { profiler.beginProfiling(); return cb(); } +, async.apply(commonUtilities.insertDocs, d, n, profiler) +, async.apply(commonUtilities.findDocs, d, n, profiler) ], function (err) { - console.log("Benchmark finished"); + profiler.step("Benchmark finished"); if (err) { return console.log("An error was encountered: ", err); } });