-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongodb_group_test.js
206 lines (188 loc) · 4.96 KB
/
mongodb_group_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var Mongo = require('mongodb'),
Table = require('cli-table'),
Program = require('nomnom'),
fs = require('fs'),
readline = require('readline'),
async = require('async');
var nTimes = 1;
var mongoDb_url = 'localhost';
var mongoDb_port = 27017;
var mongoDb_base = 'aggregation_test';
var nativeTime = [];
var groupTime = [];
var aggregationTime = [];
function setupMongoDB () {
var start = Date.now();
Mongo.connect("mongodb://"+mongoDb_url+":"+mongoDb_port+"/"+mongoDb_base+"?w=1", function(err, db) {
db.dropDatabase(function(err, result){
fs.readFile('./OK Arbres d\'alignement - Données géographiques.csv', 'ascii', function(err, data){
if (err) {
throw err;
} else {
var lines = data.split('\n');
var collection = new Mongo.Collection(db, 'tree');
lines.shift();
async.each(lines, function(line, cb){
if (line !== "") {
var tree = line.split(',');
collection.insert({
genre: tree[1],
hauteur: tree[5],
Lib_Type_E: tree[6],
Lib_Etat_C: tree[7]
}, function(err) {
cb(err);
});
}
}, function(err){
if (err) {
console.warn(err);
}
console.log('Finish in %d ms', Date.now() - start);
db.close();
process.exit(0);
});
}
});
});
});
}
function run (arg) {
function runTest (arg, db, collection) {
async.waterfall([
function (next) {
if (arg.pure === false) {
next(null);
} else {
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 }
}
})
*/
if (arg.group === false) {
next(null);
} else {
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) {
groupTime.push(Date.now() - start);
again(null);
});
}, function (err, times) {
next(null);
});
}
},
function (next) {
if (arg.aggregate === false) {
next(null);
} else {
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', (arg.pure === false) ? 'No tested' : Math.round( nativeTime.reduce(function (a, b) {return a + b;}) / nativeTime.length * 100 ) / 100 ]);
t.push(['Group', (arg.group === false) ? 'No tested' : Math.round( groupTime.reduce(function (a, b) {return a + b;}) / groupTime.length * 100 ) / 100 ]);
t.push(['Aggregation', (arg.aggregate === false) ? 'No tested' : Math.round( aggregationTime.reduce(function (a, b) {return a + b;}) / aggregationTime.length * 100) / 100]);
console.log(t.toString());
process.exit(0);
});
}
Mongo.connect("mongodb://"+mongoDb_url+":"+mongoDb_port+"/"+mongoDb_base+"?w=1", function(err, db) {
if (err) {
throw err;
}
var collection = db.collection('tree');
console.log('Run %d times selected methods', nTimes);
runTest(arg, db, collection);
});
}
Program
.command('setup')
.callback(setupMongoDB)
.help('Setup MongoDB databases');
Program
.command('run')
.callback(run)
.options({
'nTime': {
abbr: 'n',
full: 'n-times',
help: 'Run X times selected methods',
callback: function (n) {
nTimes = n;
}
},
'no-pure': { flag: true, help: 'Dont\'t test with pure js method' },
'no-group': { flag: true, help: 'Dont\'t test with Group() method' },
'no-aggregate': { flag: true, help: 'Dont\'t test with Aggregate method' }
})
.help('Run benchmark test');
Program.parse();