Skip to content

Commit

Permalink
added benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Nov 1, 2014
1 parent 0e02e68 commit 6dafd2b
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pids
*.pid
*.seed
*.tmp
*.dat

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.tmp
*.log
*.heapsnapshot
*.dat

HISTORY
Readme.md
Expand Down
116 changes: 116 additions & 0 deletions test/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// console.log(argv._);
var argv = require('optimist')
.usage('Usage: $0 -n [name]')
.argv;

// Get all the functions needed
var read_all_tests = require('./util').read_all_tests
, fs = require('fs')
, run_test = require('./util').run_test
, spawn = require('child_process').spawn
, RunningStats = require('./util').RunningStats;

// Load all the tests
var tests = read_all_tests(__dirname + "/benchmarks");
// Number of times to run the test
var run_number_of_times = 10000;
// Number of iterations to run for JIT warmup
var warm_up_iterations = 1000;
// Run serially or all of them at the same time
var concurrent = false;
// Number of operations in one concurrent batch
var concurrent_batch_size = 10;
// Default connection url
var default_url = "mongodb://localhost:27017/db";
// Additional options
var options = {};
// If we want to run a single benchmark test
if(argv.n != null) {
options.test_name = argv.n;
}

console.log("=======================================================");
console.log("= running benchmarks =")
console.log("=======================================================");

var run_tests = function(_tests) {
if(_tests.length == 0) process.exit(0);

// Get a test file
var testFile = _tests.shift();

// Run the test file
run_test(default_url
, testFile
, run_number_of_times
, warm_up_iterations
, concurrent
, concurrent_batch_size
, options
, function(err, results) {
// Let's run the test and calculate the results
var end = new Date();
// Iterate over all the results
for(var key in results) {
// Calculate the averages
var result = results[key];
var total_time = 0;
var stats = new RunningStats();
var startMemory = process.memoryUsage().rss;

// console.dir(result)
// Result file used for gnuplot
var resultfile = result.results.map(function(x, i) {
return (i + 1) + " " + x.time;
}).join("\n");

// Iterate over all the items
for(var i = warm_up_iterations; i < result.results.length; i++) {
stats.push(result.results[i].time);
}

// Filename
var dataFileName = "./" + key.replace(/ /g, "_") + ".dat";
// Write out the data to a file
fs.writeFileSync(dataFileName, resultfile);

// Execute the gnuplot to create the png file
executeGnuPlot(key.replace(/ /g, "_"), dataFileName);

// console.log("============================== data for key " + key)
// console.dir(result)

var endMemory = process.memoryUsage().rss;
// Calculate the average
var average = total_time / result.results.length;
console.log("= test: " + key);
console.log(" num :: " + stats.numDataValues);
console.log(" avg :: " + stats.mean);
console.log(" variance :: " + stats.variance);
console.log(" std dev :: " + stats.standardDeviation);
console.log(" bytes used :: " + (endMemory - startMemory));
}

// Run next batch of tests
run_tests(_tests);
});

}

var executeGnuPlot = function(key, dataFileName) {
var gnuplot = spawn('gnuplot', ['-p', '-e', "set term png; set output './" + key + ".png'; plot '" + dataFileName + "'"])
gnuplot.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});

gnuplot.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});

gnuplot.on('close', function (code) {
console.log('child process exited with code ' + code);
});
}

// Run all the tests
run_tests(tests);
101 changes: 101 additions & 0 deletions test/benchmarks/cursor_benchmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
var mongodb = require('../../.')
, 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;

// Drop the collection
db.collection('simple_100_document_toArray').drop(function(err, result) {
// 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);
}
}
}
}

// var A = function() {
// this.execute = function() {
// }
// }

// var B = function() {}
// B.prototype.execute = function() {}

// var simple_public_method_bench = function(connection_string) {
// return function() {
// return {
// db: null,

// // Setup function, called once before tests are run
// setup: function(callback) {
// callback()
// },

// // Setup function, called once after test are run
// teardown: function(callback) {
// callback()
// },

// // Actual operation we are measuring
// test: function(callback) {
// new A().execute();
// callback()
// }
// }
// }
// }

// var simple_protected_method_bench = function(connection_string) {
// return function() {
// return {
// db: null,

// // Setup function, called once before tests are run
// setup: function(callback) {
// callback()
// },

// // Setup function, called once after test are run
// teardown: function(callback) {
// callback()
// },

// // Actual operation we are measuring
// test: function(callback) {
// new B().execute();
// callback()
// }
// }
// }
// }

exports.simple_100_document_toArray = simple_100_document_toArray;
// exports.simple_protected_method_bench = simple_protected_method_bench;
// exports.simple_public_method_bench = simple_public_method_bench;
Loading

0 comments on commit 6dafd2b

Please sign in to comment.