Skip to content

Instantly share code, notes, and snippets.

@jmar777
Created August 29, 2012 20:20
Show Gist options
  • Save jmar777/3518344 to your computer and use it in GitHub Desktop.
Save jmar777/3518344 to your computer and use it in GitHub Desktop.
MongoDB Aggregation Framework Silly Eventing Benchmark
var db = connect('localhost:27017/aggr-test');
print('Dropping indexes...');
db.events.dropIndexes();
print('Creating index...');
var indexStart = Date.now();
db.events.ensureIndex({ ts: 1 });
var indexDuration = ((Date.now() - indexStart) / 1000).toFixed(2);
print('Index created (' + indexDuration + ' seconds)');
print('Total index size: ' + (db.events.totalIndexSize() / 1024 / 1024).toFixed(2) + 'MB');
var db = connect('localhost:27017/aggr-test'),
opts = {
startTime: new Date('01/01/2011').getTime(),
endTime: new Date('01/01/2012').getTime(),
numEvents: 500 * 1000
},
eventTime = opts.startTime,
incrementMs = (opts.endTime - opts.startTime) / opts.numEvents;
function getEvent() {
var event = {
ts: eventTime,
dat1: Math.random() * 1 * 1000,
dat2: Math.random() * 2 * 1000,
dat3: Math.random() * 3 * 1000,
dat4: Math.random() * 4 * 1000,
dat5: Math.random() * 5 * 1000,
dat6: Math.random() * 6 * 1000,
dat7: Math.random() * 7 * 1000,
dat8: Math.random() * 8 * 1000,
dat9: Math.random() * 9 * 1000,
dat10: Math.random() * 10 * 1000
};
eventTime += incrementMs;
return event;
}
print('Dropping database...');
db.dropDatabase();
print('Inserting test data...');
var insertStart = Date.now();
for (var i = 0; i < opts.numEvents; i++) {
(i + 1) % 10000 || print(i + 1 + ' events created');
db.events.insert(getEvent());
}
var insertDuration = ((Date.now() - insertStart) / 1000).toFixed(2);
print('Inserted ' + opts.numEvents + ' events (' + insertDuration + ' seconds)');
var db = connect('localhost:27017/aggr-test');
var pipeline = [
{
$match: {
ts: {
$gte: new Date('06/01/2011').getTime(),
$lt: new Date('07/01/2011').getTime()
}
}
},
{
$group: {
_id: 'aggregations',
sum: { $sum: '$dat1' },
num: { $sum: 1 },
avg: { $avg: '$dat2' }
}
}
];
// clear the cobwebs
for (var i = 5; i--;) {
db.events.aggregate(pipeline);
}
// timed queries
var numQueries = 50,
start = Date.now(),
res;
for (var i = numQueries; i--;) {
res = db.events.aggregate(pipeline);
}
var end = Date.now(),
totalSeconds = (end - start) / 1000,
avgSeconds = (totalSeconds / numQueries).toFixed(2);
print('Query was executed ' + numQueries + ' times');
print('Average query time: ' + avgSeconds + ' seconds (' + ((end - start) / numQueries) + 'ms)');
print('Results:');
printjson(res);
$ mongo ./query.js
MongoDB shell version: 2.2.0
connecting to: test
connecting to: localhost:27017/aggr-test
Query was executed 50 times
Average query time: 0.26 seconds (259.74ms)
Results:
{
"result" : [
{
"_id" : "aggregations",
"sum" : 20534318.95504394,
"num" : 41096,
"avg" : 1004.4187408703551
}
],
"ok" : 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment