Skip to content

Commit 3bc0d02

Browse files
committed
Working on adding some benchmarking code
1 parent 97382cb commit 3bc0d02

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ data/
1515
dev/
1616
examples/
1717
test/
18+
performance/
1819
.coverage_data/
1920

2021
external-libs/bson/build/

performance/benchmark.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var read_all_tests = require('./util').read_all_tests
2+
, run_test = require('./util').run_test;
3+
4+
// Load all the tests
5+
var tests = read_all_tests(__dirname + "/test");
6+
7+
// Get a test
8+
var run_number_of_times = 100;
9+
var concurrent = false;
10+
var results = [];
11+
var default_url = "mongodb://localhost:27017/db";
12+
13+
console.log("=======================================================");
14+
console.log("= running benchmarks =")
15+
console.log("=======================================================");
16+
17+
var start = new Date();
18+
run_test(default_url, tests[0], run_number_of_times, concurrent, function(err, results) {
19+
var end = new Date();
20+
for(var key in results) {
21+
// Calculate the averages
22+
var result = results[key];
23+
var total_time = 0;
24+
for(var i = 0; i < result.results.length; i++) {
25+
total_time = total_time + result.results[i].time;
26+
}
27+
// Calculate the average
28+
var average = total_time / result.results.length;
29+
console.log("= test: " + key);
30+
console.log(" tot :: " + total_time);
31+
console.log(" avg :: " + average);
32+
}
33+
34+
35+
// console.dir(err)
36+
// console.dir(results["simple_100_document_toArray"])
37+
console.log("total time :: " + (end.getTime() - start.getTime()));
38+
});
39+
40+
41+
// console.dir(tests)

performance/test/cursor_small_set_.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var mongodb = require('../../lib/mongodb')
2+
, Db = mongodb.Db
3+
, Server = mongodb.Server
4+
, MongoClient = mongodb.MongoClient;
5+
6+
var simple_100_document_toArray = function(connection_string) {
7+
return function() {
8+
return {
9+
db: null,
10+
11+
// Setup function, called once before tests are run
12+
setup: function(callback) {
13+
var self = this;
14+
15+
MongoClient.connect(connection_string, function(err, db) {
16+
if(err) return callback(err);
17+
self.db = db;
18+
// Create 100 documents
19+
var docs = [];
20+
for(var i = 0; i < 100; i++) docs.push({a:1, b:'hello world', c:1});
21+
// Setup the 100 documents
22+
db.collection('simple_100_document_toArray').insert(docs, {w:1}, callback);
23+
});
24+
},
25+
26+
// Setup function, called once after test are run
27+
teardown: function(callback) {
28+
if(this.db != null) this.db.close(callback);
29+
},
30+
31+
// Actual operation we are measuring
32+
test: function(callback) {
33+
this.db.collection('simple_100_document_toArray').find().toArray(callback);
34+
}
35+
}
36+
}
37+
}
38+
39+
exports.simple_100_document_toArray = simple_100_document_toArray;

performance/util.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
var fs = require('fs');
2+
3+
var read_all_tests = function(directory) {
4+
return fs.readdirSync(directory).filter(function(element, index, array) {
5+
return element.indexOf(".js") != -1;
6+
}).map(function(element) {
7+
return directory + "/" + element;
8+
})
9+
}
10+
11+
var run_test = function(url, file, number_of_times, concurrent, test_name, callback) {
12+
var final_results = {};
13+
var number_of_test_to_run = 0;
14+
// If we have not test_name set up
15+
if(typeof test_name == 'function') {
16+
callback = test_name;
17+
test_name = null;
18+
}
19+
20+
// Load the file
21+
var _module = require(file);
22+
// No test set run all the available tests and store the results
23+
if(test_name == null) {
24+
// Execute the setup
25+
var keys = Object.keys(_module);
26+
var number_of_tests = keys.length;
27+
// Go over all the tests
28+
for(var i = 0; i < keys.length; i++) {
29+
// Wrap the scope so we can execute it by itself
30+
new function(_key, _module_func) {
31+
run_single_test(url, _key, _module_func, number_of_times, concurrent, function(err, results) {
32+
// Final results
33+
final_results[_key] = { results: results};
34+
if(err) { final_results[_key].err = err; }
35+
// Adjust number of tests left to run
36+
number_of_tests = number_of_tests - 1;
37+
if(number_of_tests == 0) callback(null, final_results)
38+
});
39+
}(keys[i], _module[keys[i]])
40+
}
41+
} else {
42+
43+
}
44+
}
45+
46+
var run_single_test = function(url, func_name, func, number_of_times, concurrent, callback) {
47+
var results = [];
48+
var test = func(url)();
49+
50+
// Run the setup part first
51+
test.setup(function(err, setup_result) {
52+
if(err) return callback(err, null);
53+
54+
if(!concurrent) {
55+
exceute_test_serially(url, func_name, test, number_of_times, results, function(err, test_results) {
56+
test.teardown(function(_t_err, _t_result) {
57+
if(_t_err) return callback(_t_err, null);
58+
callback(err, test_results)
59+
})
60+
});
61+
} else {
62+
var number_left_to_run = number_of_times;
63+
// Execute all the tests
64+
for(var i = 0; i < number_of_times; i++) {
65+
new function() {
66+
// Set start function
67+
var start = new Date();
68+
// Execute function
69+
test.test(function(err, result) {
70+
var end = new Date();
71+
var time = end.getTime() - start.getTime();
72+
results.push({start: start, end: end, time: time});
73+
// Adjust the number of tests to run
74+
number_left_to_run = number_left_to_run - 1;
75+
if(number_left_to_run == 0) {
76+
test.teardown(function(_t_err, _t_result) {
77+
if(_t_err) return callback(_t_err, null);
78+
callback(err, results)
79+
})
80+
}
81+
});
82+
}();
83+
}
84+
}
85+
})
86+
}
87+
88+
var exceute_test_serially = function(url, func_name, test, number_of_times, results, callback) {
89+
if(number_of_times == 0) return callback(null, results);
90+
number_of_times = number_of_times - 1;
91+
// Set start function
92+
var start = new Date();
93+
// Execute function
94+
test.test(function(err, result) {
95+
var end = new Date();
96+
var time = end.getTime() - start.getTime();
97+
results.push({start: start, end: end, time: time});
98+
// Execute the next tick
99+
process.nextTick(function() {
100+
exceute_test_serially(url, func_name, test, number_of_times - 1, results, callback);
101+
})
102+
});
103+
}
104+
105+
106+
exports.read_all_tests = read_all_tests;
107+
exports.run_test = run_test;

0 commit comments

Comments
 (0)