Skip to content

Instantly share code, notes, and snippets.

@PierrickP
Last active December 17, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PierrickP/105e04b7b486c08e712c to your computer and use it in GitHub Desktop.
Save PierrickP/105e04b7b486c08e712c to your computer and use it in GitHub Desktop.
Bench Mongodb Group
Bench Mongodb Groupvar Mongo = require('mongodb');
var Table = require('cli-table');
var fs = require('fs'),
async = require('async');
console.log('Play without aggregation\nGroup tree by genre and get total by genre');
var nTimes = 1;
var nativeTime = [];
var groupTime = [];
var aggregationTime = [];
Mongo.connect("mongodb://localhost:27017/aggregation_test?w=1", function(err, db) {
var collection = db.collection('tree');
if (err) {
throw err;
}
runTest(db, collection);
});
function runTest (db, collection) {
async.waterfall([
function (next) {
console.log('Group with pure js');
async.timesSeries(
nTimes,
function (n, again) {
var start = Date.now();
collection.find({}, {genre: 1}).toArray(function(err, doc){
var result = [];
doc.forEach(function(tree){
if (result[tree.genre] === undefined) {
result[tree.genre] = {};
result[tree.genre].totalByGenre = 1;
} else {
result[tree.genre].totalByGenre++;
}
});
nativeTime.push(Date.now() - start);
again(null);
});
},
function (err, times) {
next(null);
}
);
},
function (next) {
/*
db.runCommand({
group: {
ns: 'tree',
key: { genre: 1},
$reduce: function ( curr, result ) {
result.totalByGenre++;
},
initial: { totalByGenre : 0 }
}
})
*/
console.log('Group with group mongodb');
async.timesSeries(nTimes, function (n, again){
var start = Date.now();
var time = Date.now();
collection.group(
['genre'],
{},
{ totalByGenre: 0 },
function ( curr, result ) {
result.totalByGenre += 1;
},
function (err, result) {
console.log(result);
groupTime.push(Date.now() - start);
again(null);
});
}, function (err, times) {
next(null);
});
},
function (next) {
console.log('Group with aggregate mongodb');
async.timesSeries(nTimes, function (n, again){
var start = Date.now();
var time = Date.now();
collection.aggregate(
{
$project : {
genre: 1
}
}, {
$group : {
_id: '$genre',
totalByGenre: { $sum: 1}
}
},{
$sort: {totalByGenre: -1}
}, function(err, result) {
aggregationTime.push(Date.now() - start);
again(null);
});
}, function (err, times) {
next(null);
});
}
], function () {
var t = new Table({
head: ['Method', 'avg time (ms)']
});
t.push(['Pure js', Math.round( nativeTime.reduce(function (a, b) {return a + b;}) / nativeTime.length * 100 ) / 100 ]);
t.push(['Group', Math.round( groupTime.reduce(function (a, b) {return a + b;}) / groupTime.length * 100 ) / 100 ]);
t.push(['Aggregation', Math.round( aggregationTime.reduce(function (a, b) {return a + b;}) / aggregationTime.length * 100) / 100]);
console.log(t.toString());
process.exit(0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment