Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Working on adding some benchmarking code

  • Loading branch information...
commit 3bc0d02ec57f3a792a4258a9fe2dbee6d4ea256b 1 parent 97382cb
@christkv christkv authored
View
1  .npmignore
@@ -15,6 +15,7 @@ data/
dev/
examples/
test/
+performance/
.coverage_data/
external-libs/bson/build/
View
41 performance/benchmark.js
@@ -0,0 +1,41 @@
+var read_all_tests = require('./util').read_all_tests
+ , run_test = require('./util').run_test;
+
+// Load all the tests
+var tests = read_all_tests(__dirname + "/test");
+
+// Get a test
+var run_number_of_times = 100;
+var concurrent = false;
+var results = [];
+var default_url = "mongodb://localhost:27017/db";
+
+console.log("=======================================================");
+console.log("= running benchmarks =")
+console.log("=======================================================");
+
+var start = new Date();
+run_test(default_url, tests[0], run_number_of_times, concurrent, function(err, results) {
+ var end = new Date();
+ for(var key in results) {
+ // Calculate the averages
+ var result = results[key];
+ var total_time = 0;
+ for(var i = 0; i < result.results.length; i++) {
+ total_time = total_time + result.results[i].time;
+ }
+ // Calculate the average
+ var average = total_time / result.results.length;
+ console.log("= test: " + key);
+ console.log(" tot :: " + total_time);
+ console.log(" avg :: " + average);
+ }
+
+
+ // console.dir(err)
+ // console.dir(results["simple_100_document_toArray"])
+ console.log("total time :: " + (end.getTime() - start.getTime()));
+});
+
+
+// console.dir(tests)
View
39 performance/test/cursor_small_set_.js
@@ -0,0 +1,39 @@
+var mongodb = require('../../lib/mongodb')
+ , Db = mongodb.Db
+ , Server = mongodb.Server
+ , MongoClient = mongodb.MongoClient;
+
+var simple_100_document_toArray = function(connection_string) {
+ return function() {
+ return {
+ db: null,
+
+ // Setup function, called once before tests are run
+ setup: function(callback) {
+ var self = this;
+
+ MongoClient.connect(connection_string, function(err, db) {
+ if(err) return callback(err);
+ self.db = db;
+ // Create 100 documents
+ var docs = [];
+ for(var i = 0; i < 100; i++) docs.push({a:1, b:'hello world', c:1});
+ // Setup the 100 documents
+ db.collection('simple_100_document_toArray').insert(docs, {w:1}, callback);
+ });
+ },
+
+ // Setup function, called once after test are run
+ teardown: function(callback) {
+ if(this.db != null) this.db.close(callback);
+ },
+
+ // Actual operation we are measuring
+ test: function(callback) {
+ this.db.collection('simple_100_document_toArray').find().toArray(callback);
+ }
+ }
+ }
+}
+
+exports.simple_100_document_toArray = simple_100_document_toArray;
View
107 performance/util.js
@@ -0,0 +1,107 @@
+var fs = require('fs');
+
+var read_all_tests = function(directory) {
+ return fs.readdirSync(directory).filter(function(element, index, array) {
+ return element.indexOf(".js") != -1;
+ }).map(function(element) {
+ return directory + "/" + element;
+ })
+}
+
+var run_test = function(url, file, number_of_times, concurrent, test_name, callback) {
+ var final_results = {};
+ var number_of_test_to_run = 0;
+ // If we have not test_name set up
+ if(typeof test_name == 'function') {
+ callback = test_name;
+ test_name = null;
+ }
+
+ // Load the file
+ var _module = require(file);
+ // No test set run all the available tests and store the results
+ if(test_name == null) {
+ // Execute the setup
+ var keys = Object.keys(_module);
+ var number_of_tests = keys.length;
+ // Go over all the tests
+ for(var i = 0; i < keys.length; i++) {
+ // Wrap the scope so we can execute it by itself
+ new function(_key, _module_func) {
+ run_single_test(url, _key, _module_func, number_of_times, concurrent, function(err, results) {
+ // Final results
+ final_results[_key] = { results: results};
+ if(err) { final_results[_key].err = err; }
+ // Adjust number of tests left to run
+ number_of_tests = number_of_tests - 1;
+ if(number_of_tests == 0) callback(null, final_results)
+ });
+ }(keys[i], _module[keys[i]])
+ }
+ } else {
+
+ }
+}
+
+var run_single_test = function(url, func_name, func, number_of_times, concurrent, callback) {
+ var results = [];
+ var test = func(url)();
+
+ // Run the setup part first
+ test.setup(function(err, setup_result) {
+ if(err) return callback(err, null);
+
+ if(!concurrent) {
+ exceute_test_serially(url, func_name, test, number_of_times, results, function(err, test_results) {
+ test.teardown(function(_t_err, _t_result) {
+ if(_t_err) return callback(_t_err, null);
+ callback(err, test_results)
+ })
+ });
+ } else {
+ var number_left_to_run = number_of_times;
+ // Execute all the tests
+ for(var i = 0; i < number_of_times; i++) {
+ new function() {
+ // Set start function
+ var start = new Date();
+ // Execute function
+ test.test(function(err, result) {
+ var end = new Date();
+ var time = end.getTime() - start.getTime();
+ results.push({start: start, end: end, time: time});
+ // Adjust the number of tests to run
+ number_left_to_run = number_left_to_run - 1;
+ if(number_left_to_run == 0) {
+ test.teardown(function(_t_err, _t_result) {
+ if(_t_err) return callback(_t_err, null);
+ callback(err, results)
+ })
+ }
+ });
+ }();
+ }
+ }
+ })
+}
+
+var exceute_test_serially = function(url, func_name, test, number_of_times, results, callback) {
+ if(number_of_times == 0) return callback(null, results);
+ number_of_times = number_of_times - 1;
+ // Set start function
+ var start = new Date();
+ // Execute function
+ test.test(function(err, result) {
+ var end = new Date();
+ var time = end.getTime() - start.getTime();
+ results.push({start: start, end: end, time: time});
+ // Execute the next tick
+ process.nextTick(function() {
+ exceute_test_serially(url, func_name, test, number_of_times - 1, results, callback);
+ })
+ });
+}
+
+
+exports.read_all_tests = read_all_tests;
+exports.run_test = run_test;
Please sign in to comment.
Something went wrong with that request. Please try again.