5c4d0b9ef1f7d2daf007a6dbd469e96609d5a464
[aai/esr-gui.git] /
1 var MongoClient = require('./').MongoClient,
2   assert = require('assert');
3
4 // var memwatch = require('memwatch-next');
5 // memwatch.on('leak', function(info) {
6 //   console.log("======== leak")
7 // });
8 //
9 // memwatch.on('stats', function(stats) {
10 //   console.log("======== stats")
11 //   console.dir(stats)
12 // });
13
14 // // Take first snapshot
15 // var hd = new memwatch.HeapDiff();
16
17 MongoClient.connect('mongodb://localhost:27017/bench', function(err, db) {
18   var docs = [];
19   var total = 1000;
20   var count = total;
21   var measurements = [];
22
23   // Insert a bunch of documents
24   for(var i = 0; i < 100; i++) {
25     docs.push(JSON.parse(data));
26   }
27
28   var col = db.collection('inserts');
29
30   function execute(col, callback) {
31     var start = new Date().getTime();
32
33     col.find({}).limit(100).toArray(function(e, docs) {
34       measurements.push(new Date().getTime() - start);
35       assert.equal(null, e);
36       callback();
37     });
38   }
39
40   console.log("== insert documents")
41   col.insert(docs, function(e, r) {
42     docs = [];
43     assert.equal(null, e);
44
45     console.log("== start bench")
46     for(var i = 0; i < total; i++) {
47       execute(col, function(e) {
48         count = count - 1;
49
50         if(count == 0) {
51           // Calculate total execution time for operations
52           var totalTime = measurements.reduce(function(prev, curr) {
53             return prev + curr;
54           }, 0);
55
56           console.log("===========================================");
57           console.log("total time: " + totalTime)
58
59           // var diff = hd.end();
60           // console.log("===========================================");
61           // console.log(JSON.stringify(diff, null, 2))
62
63           db.close();
64           process.exit(0)
65         }
66       });
67     }
68   });
69 });
70
71 var data = JSON.stringify({
72   "data": [
73     {
74       "_id": 1,
75       "x": 11
76     },
77     {
78       "_id": 2,
79       "x": 22
80     },
81     {
82       "_id": 3,
83       "x": 33
84     }
85   ],
86   "collection_name": "test",
87   "database_name": "command-monitoring-tests",
88   "tests": [
89     {
90       "description": "A successful mixed bulk write",
91       "operation": {
92         "name": "bulkWrite",
93         "arguments": {
94           "requests": [
95             {
96               "insertOne": {
97                 "document": {
98                   "_id": 4,
99                   "x": 44
100                 }
101               }
102             },
103             {
104               "updateOne": {
105                 "filter": {
106                   "_id": 3
107                 },
108                 "update": {
109                   "set": {
110                     "x": 333
111                   }
112                 }
113               }
114             }
115           ]
116         }
117       },
118       "expectations": [
119         {
120           "command_started_event": {
121             "command": {
122               "insert": "test",
123               "documents": [
124                 {
125                   "_id": 4,
126                   "x": 44
127                 }
128               ],
129               "ordered": true
130             },
131             "command_name": "insert",
132             "database_name": "command-monitoring-tests"
133           }
134         },
135         {
136           "command_succeeded_event": {
137             "reply": {
138               "ok": 1.0,
139               "n": 1
140             },
141             "command_name": "insert"
142           }
143         },
144         {
145           "command_started_event": {
146             "command": {
147               "update": "test",
148               "updates": [
149                 {
150                   "q": {
151                     "_id": 3
152                   },
153                   "u": {
154                     "set": {
155                       "x": 333
156                     }
157                   },
158                   "upsert": false,
159                   "multi": false
160                 }
161               ],
162               "ordered": true
163             },
164             "command_name": "update",
165             "database_name": "command-monitoring-tests"
166           }
167         },
168         {
169           "command_succeeded_event": {
170             "reply": {
171               "ok": 1.0,
172               "n": 1
173             },
174             "command_name": "update"
175           }
176         }
177       ]
178     },
179     {
180       "description": "A successful unordered bulk write with an unacknowledged write concern",
181       "operation": {
182         "name": "bulkWrite",
183         "arguments": {
184           "requests": [
185             {
186               "insertOne": {
187                 "document": {
188                   "_id": 4,
189                   "x": 44
190                 }
191               }
192             }
193           ],
194           "ordered": false,
195           "writeConcern": {
196             "w": 0
197           }
198         }
199       },
200       "expectations": [
201         {
202           "command_started_event": {
203             "command": {
204               "insert": "test",
205               "documents": [
206                 {
207                   "_id": 4,
208                   "x": 44
209                 }
210               ],
211               "ordered": false,
212               "writeConcern": {
213                 "w": 0
214               }
215             },
216             "command_name": "insert",
217             "database_name": "command-monitoring-tests"
218           }
219         },
220         {
221           "command_succeeded_event": {
222             "reply": {
223               "ok": 1.0
224             },
225             "command_name": "insert"
226           }
227         }
228       ]
229     }
230   ]
231 });