-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
85 lines (69 loc) · 2.39 KB
/
index.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
const mongoose = require('mongoose')
const casual = require('casual')
const { User, UserWithIndex } = require('./database/user');
(async () => {
try {
await mongoose.connect('mongodb://localhost:27017/perftest', {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
})
await init()
const query = { age: { $gt: 22 } }
// const query = { favoriteFruit: 'potato' }
console.time('query')
await User.find(query)
console.timeEnd('query')
console.time('query_with_index')
await UserWithIndex.find(query)
console.timeEnd('query_with_index')
console.time('query_with_select')
await User.find(query)
.select({ name: 1, _id: 1, age: 1, email: 1 })
console.timeEnd('query_with_select')
console.time('query_with_select_index')
await UserWithIndex.find(query)
.select({ name: 1, _id: 1, age: 1, email: 1 })
console.timeEnd('query_with_select_index')
console.time('lean_query')
await User.find(query).lean()
console.timeEnd('lean_query')
console.time('lean_with_index')
await UserWithIndex.find(query).lean()
console.timeEnd('lean_with_index')
console.time('lean_with_select')
await User.find(query)
.select({ name: 1, _id: 1, age: 1, email: 1 })
.lean()
console.timeEnd('lean_with_select')
console.time('lean_select_index')
await UserWithIndex.find(query)
.select({ name: 1, _id: 1, age: 1, email: 1 })
.lean()
console.timeEnd('lean_select_index')
process.exit(0)
} catch (err) {
console.error(err)
}
})()
function populateDBWithDummyData (numberOfItems) {
const docs = [...new Array(numberOfItems)].map((_, index) => ({
email: casual.email,
name: casual.name,
age: casual.integer(0, 50),
details: casual.array_of_integers(100),
birthDate: new Date(casual.date('YYYY-MM-DD')),
favoriteFruit: index % 2 === 0 ? 'tomato' : 'potato'
}))
return Promise.all([UserWithIndex.insertMany(docs), User.insertMany(docs)])
}
async function init () {
console.log('cleaning db')
await Promise.all([User.deleteMany({}), UserWithIndex.deleteMany({})])
console.log('db cleaned')
const numberOfItems = 10000
console.log(`adding ${numberOfItems} users to the database`)
await populateDBWithDummyData(numberOfItems)
console.log(`finished populating the database with ${numberOfItems} users`)
}