forked from louischatriot/nedb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the profiler to time the experiment
- Loading branch information
1 parent
2c59251
commit b494958
Showing
4 changed files
with
83 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters