|
|
@ -3,79 +3,25 @@ var Datastore = require('../lib/datastore') |
|
|
|
, fs = require('fs') |
|
|
|
, fs = require('fs') |
|
|
|
, path = require('path') |
|
|
|
, path = require('path') |
|
|
|
, async = require('async') |
|
|
|
, async = require('async') |
|
|
|
, customUtils = require('../lib/customUtils') |
|
|
|
, commonUtilities = require('./commonUtilities') |
|
|
|
, d |
|
|
|
, execTime = require('exec-time') |
|
|
|
|
|
|
|
, profiler = new execTime('FIND BENCH') |
|
|
|
|
|
|
|
, d = new Datastore(benchDb) |
|
|
|
, n = 10000 |
|
|
|
, n = 10000 |
|
|
|
, order |
|
|
|
|
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
if (process.argv[2]) { n = parseInt(process.argv[2], 10); } |
|
|
|
if (process.argv[2]) { n = parseInt(process.argv[2], 10); } |
|
|
|
order = customUtils.getRandomArray(n) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Benchmarking find"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([ |
|
|
|
async.waterfall([ |
|
|
|
function (cb) { |
|
|
|
async.apply(commonUtilities.prepareDb, benchDb) |
|
|
|
console.log("Preparing database"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
customUtils.ensureDirectoryExists(path.dirname(benchDb), function () { |
|
|
|
|
|
|
|
fs.exists(benchDb, function (exists) { |
|
|
|
|
|
|
|
if (exists) { |
|
|
|
|
|
|
|
fs.unlink(benchDb, cb); |
|
|
|
|
|
|
|
} else { return cb(); } |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
, function (cb) { |
|
|
|
, function (cb) { |
|
|
|
d = new Datastore(benchDb); |
|
|
|
|
|
|
|
d.loadDatabase(cb); |
|
|
|
d.loadDatabase(cb); |
|
|
|
} |
|
|
|
} |
|
|
|
, function (cb) { |
|
|
|
, function (cb) { profiler.beginProfiling(); return cb(); } |
|
|
|
var beg = new Date() |
|
|
|
, async.apply(commonUtilities.insertDocs, d, n, profiler) |
|
|
|
, i = 0; |
|
|
|
, async.apply(commonUtilities.findDocs, d, n, profiler) |
|
|
|
|
|
|
|
|
|
|
|
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 (err) { |
|
|
|
], function (err) { |
|
|
|
console.log("Benchmark finished"); |
|
|
|
profiler.step("Benchmark finished"); |
|
|
|
|
|
|
|
|
|
|
|
if (err) { return console.log("An error was encountered: ", err); } |
|
|
|
if (err) { return console.log("An error was encountered: ", err); } |
|
|
|
}); |
|
|
|
}); |
|
|
|