The JavaScript Database, for Node.js, nw.js, electron and the browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nedb/benchmarks/insert.js

58 lines
1.4 KiB

var Datastore = require('../lib/datastore')
, benchDb = 'workspace/insert.bench.db'
, fs = require('fs')
, path = require('path')
, async = require('async')
, customUtils = require('../lib/customUtils')
, n = 10000
, d
;
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(); }
});
});
}
, 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 (err) {
console.log("Benchmark finished");
if (err) { return console.log("An error was encountered: ", err); }
});