<commit_before>var Datastore = require('../lib/datastore')
  , benchDb = 'workspace/find.bench.db'
  , fs = require('fs')
  , path = require('path')
  , async = require('async')
  , customUtils = require('../lib/customUtils')
  , d
  , 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(); }
      });
    });
  }
, 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");
        return cb();
      }

      d.find({ docNumber: order[i] }, function (err, docs) {
        if (docs.length !== 1) { return cb('One find didnt work'); }
        process.nextTick(function () {
          find(i + 1);
        });
      });
    }
    find(0);
  }
//, function (cb) {
    //var beg = new Date()
      //, i = 0;

    //console.log("Finding " + n + " documents");

    //async.whilst( function () { return i < n; }
    //, function (_cb) {
      //d.find({ docNumber: order[i] }, function (err, docs) {
        //i += 1;
        //if (docs.length !== 1) { return _cb(docs); }
        //return _cb(err);
      //});
    //}, function (err) {
      //var timeTaken = (new Date()).getTime() - beg.getTime();   // In ms
      //if (err) { return cb(err); }
      //console.log("Time taken: " + (timeTaken / 1000) + "s");
      //return cb();
    //});
  //}
], function (err) {
  console.log("Benchmark finished");

  if (err) { return console.log("An error was encountered: ", err); }
});<commit_msg>Benchmark for findone runs in half the time of find as expected<commit_after>var Datastore = require('../lib/datastore')
  , benchDb = 'workspace/find.bench.db'
  , fs = require('fs')
  , path = require('path')
  , async = require('async')
  , customUtils = require('../lib/customUtils')
  , d
  , 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(); }
      });
    });
  }
, 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");
        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) {
  console.log("Benchmark finished");

  if (err) { return console.log("An error was encountered: ", err); }
});
{'fraction_non_alphanumeric': 0.18956336528221512, 'fraction_numerical': 0.011927582534611288, 'mean_word_length': 2.1601615074024227, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7416508', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1569, 'n_words': 366}