deno.land / x / mongoose@6.7.5 / benchmarks / benchjs / update.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
'use strict';
const mongoose = require('../../lib');const Benchmark = require('benchmark');
const suite = new Benchmark.Suite();
const Schema = mongoose.Schema;const mongoClient = require('mongodb').MongoClient;const ObjectId = Schema.Types.ObjectId;const utils = require('../../lib/utils.js');
// to make things work in the way the are normally described online.../* *global.Schema = Schema; *global.mongoose = mongoose; */
/** * These are all the benchmark tests for updating data */
mongoose.connect('mongodb://localhost/mongoose-bench', function (err) { if (err) { throw err; } mongoClient.connect('mongodb://localhost', function (err, client) { if (err) { throw err; }
const db = client.db('mongoose-bench');
const Comments = new Schema(); Comments.add({ title: String, date: Date, body: String, comments: [Comments], });
let BlogPost = new Schema({ title: String, author: String, slug: String, date: Date, meta: { date: Date, visitors: Number, }, published: Boolean, mixed: {}, numbers: [Number], tags: [String], owners: [ObjectId], comments: [Comments], def: { type: String, default: 'kandinsky' }, });
const blogData = { title: 'dummy post', author: 'somebody', slug: 'test.post', date: new Date(), meta: { date: new Date(), visitors: 9001 }, published: true, mixed: { thisIsRandom: true }, numbers: [1, 2, 7, 10, 23432], tags: ['test', 'BENCH', 'things', 'more things'], def: 'THANGS!!!', comments: [], };
const commentData = { title: 'test comment', date: new Date(), body: 'this be some crazzzyyyyy text that would go in a comment', comments: [{ title: 'second level', date: new Date(), body: 'texttt' }], };
for (let i = 0; i < 5; i++) { blogData.comments.push(commentData); }
const UserSchema = new Schema({ name: String, age: Number, likes: [String], address: String, });
const User = mongoose.model('User', UserSchema); BlogPost = mongoose.model('BlogPost', BlogPost); const user = db.collection('user'); const blogpost = db.collection('blogpost');
const mIds = []; const dIds = [];
const bmIds = []; const bdIds = [];
const data = { name: 'name', age: 0, likes: ['dogs', 'cats', 'pizza'], address: ' Nowhere-ville USA', };
// this is for some of the update tests below let testBp; // insert all of the data here let count = 4000; for (let i = 0; i < 1000; i++) { User.create(data, function (err, u) { if (err) { throw err; } mIds.push(u.id); --count || next(); }); const nData = utils.clone(data); user.insertOne(nData, function (err, res) { dIds.push(res.insertedId); --count || next(); }); BlogPost.create(blogData, function (err, bp) { if (err) { throw err; } bmIds.push(bp.id); testBp = bp; --count || next(); });
const bpData = utils.clone(blogData); blogpost.insertOne(bpData, function (err, res) { if (err) { throw err; } bdIds.push(res.insertedId); --count || next(); }); }
let mi = 0, di = 0, bmi = 0, bdi = 0;
function getNextmId() { mi = ++mi % mIds.length; return mIds[mi]; }
function getNextdId() { di = ++di % dIds.length; return dIds[di]; }
function getNextbmId() { bmi = ++bmi % bmIds.length; return bmIds[bmi]; }
function getNextbdId() { bdi = ++bdi % bdIds.length; return bdIds[bdi]; }
function closeDB() { mongoose.connection.db.dropDatabase(function () { mongoose.disconnect(); process.exit(); }); }
suite .add('Update - Mongoose - Basic', { defer: true, fn: function (deferred) { User.update( { _id: getNextmId() }, { $set: { age: 2 }, $push: { likes: 'metal' } }, function (err) { if (err) { throw err; } deferred.resolve(); } ); }, }) .add('Update - Driver - Basic', { defer: true, fn: function (deferred) { user.updateOne( { _id: getNextdId() }, { $set: { age: 2 }, $push: { likes: 'metal' } }, function (err) { if (err) { throw err; } deferred.resolve(); } ); }, }) .add('Update - Mongoose - Embedded Docs', { defer: true, fn: function (deferred) { BlogPost.findOne({ _id: getNextbmId() }, function (err, bp) { if (err) { throw err; } bp.comments[3].title = 'this is a new title'; bp.comments[0].date = new Date(); bp.comments.push(commentData); // save in Mongoose behaves differently than it does in the driver. // The driver will send the full document, while mongoose will check // and only update fields that have been changed. This is meant to // illustrate that difference between the two bp.save(function (err) { if (err) { throw err; } deferred.resolve(); }); }); }, }) .add('Update - Driver - Embdedded Docs', { defer: true, fn: function (deferred) { blogpost.findOne({ _id: getNextbdId() }, function (err, bp) { if (err) { throw err; } blogpost.updateOne( { _id: bp._id }, { $set: { 'comments.3.title': 'this is a new title', 'comments.0.date': new Date(), }, }, function (err) { if (err) { throw err; } deferred.resolve(); } ); }); }, }) .add('Update - Mongoose - Multiple Documents', { defer: true, fn: function (deferred) { const ids = []; for (let i = 0; i < 50; i++) { ids.push(getNextmId()); } User.update( { _id: { $in: ids } }, { $set: { age: 2 }, $push: { likes: 'metal' } }, function (err) { if (err) { throw err; } deferred.resolve(); } ); }, }) .add('Update - Driver - Multiple Documents', { defer: true, fn: function (deferred) { const ids = []; for (let i = 0; i < 50; i++) { ids.push(getNextdId()); } user.updateOne( { _id: { $in: ids } }, { $set: { age: 2 }, $push: { likes: 'metal' } }, function (err) { if (err) { throw err; } deferred.resolve(); } ); }, }) .add('Update - Mongoose - pop and push', { defer: true, fn: function (deferred) { testBp.comments.push(commentData); testBp.comments.$shift(); testBp.save(function (err) { if (err) { throw err; } deferred.resolve(); }); }, }) .add('Update - Mongoose - Array Manipulation, parallel ops', { defer: true, fn: function (deferred) { let done = false; BlogPost.update( { _id: testBp.id }, { $pop: { comments: -1 } }, function (err) { if (err) { throw err; } done && deferred.resolve(); done = true; } ); BlogPost.update( { _id: testBp.id }, { $push: { comments: commentData } }, function (err) { if (err) { throw err; } done && deferred.resolve(); done = true; } ); }, }) .add('Update - Mongoose - findOneAndModify', { defer: true, fn: function (deferred) { BlogPost.findOneAndUpdate( { _id: getNextbmId() }, { $set: { age: 2 }, $push: { likes: 'metal' } }, function (err) { if (err) { throw err; } deferred.resolve(); } ); }, }) .add('Update - Mongoose - find and update, separate ops', { defer: true, fn: function (deferred) { BlogPost.findOne({ _id: getNextbmId() }, function (err, bp) { if (err) { throw err; } bp.update( { $set: { age: 2 }, $push: { likes: 'metal' } }, function (err) { if (err) { throw err; } deferred.resolve(); } ); }); }, }) .on('cycle', function (evt) { if (process.env.MONGOOSE_DEV || process.env.PULL_REQUEST) { console.log(String(evt.target)); } }) .on('complete', function () { closeDB(); if (!process.env.MONGOOSE_DEV && !process.env.PULL_REQUEST) { const outObj = {}; this.forEach(function (item) { const out = {}; out.stats = item.stats; delete out.stats.sample; out.ops = item.hz; outObj[item.name.replace(/\s/g, '')] = out; }); console.dir(outObj, { depth: null, colors: true }); } }); function next() { for (let i = 0; i < 100; i++) { testBp.comments.push(commentData); } testBp.save(function (err) { if (err) { throw err; } suite.run({ async: true }); }); } });});
mongoose

Version Info

Tagged at
a year ago