-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (95 loc) · 2.65 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
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
const MongoClient = require("mongodb").MongoClient;
const MONGO_URL = "mongodb://localhost:27017";
const DATABASE_NAME = "loadtest";
const COLLECTION_NAME = "test";
const DOCS_TO_INSERT_COUNT = 8 * 1000;
const ARRAY_FIELD_SIZE = 20;
const REQUESTS_COUNT = 300;
const client = new MongoClient(MONGO_URL, {
enableUtf8Validation: false,
ignoreUndefined: true,
});
let db;
async function main() {
await initDbConnection();
await maybePopulateDb();
await runBenchmark();
}
main()
.catch(console.error)
.finally(() => client.close());
async function initDbConnection() {
await client.connect();
db = client.db(DATABASE_NAME);
}
async function runBenchmark() {
const tag = generateTag();
const numBenchmarkIterations = 3;
for (let i = 0; i < numBenchmarkIterations; i += 1) {
console.time(tag);
await benchmark();
console.timeEnd(tag);
/* wait for GC to free up the memory */
await sleep(60);
}
}
function generateTag() {
const driverVersion = process.argv[2];
return `Fetch ${DOCS_TO_INSERT_COUNT} docs ${REQUESTS_COUNT} times in parallel (driver ${driverVersion})`;
}
async function maybePopulateDb() {
const insertedDocsCount = await getInsertedDocsCount();
if (!insertedDocsCount) {
await populateDb();
} else if (DOCS_TO_INSERT_COUNT !== insertedDocsCount) {
await clearDb();
await populateDb();
}
}
function getInsertedDocsCount() {
return db.collection(COLLECTION_NAME).countDocuments({});
}
function clearDb() {
return db.collection(COLLECTION_NAME).drop();
}
async function populateDb() {
const docs = composeDocs();
await bulkInsertDocs(docs);
}
function composeDocs() {
const docs = new Array(DOCS_TO_INSERT_COUNT);
const arrayField = composeArrayField();
for (let i = 0; i < DOCS_TO_INSERT_COUNT; i += 1) {
docs[i] = { _id: i, arrayField };
}
return docs;
}
function composeArrayField() {
const arrayField = new Array(ARRAY_FIELD_SIZE);
for (let i = 0; i < ARRAY_FIELD_SIZE; i += 1) {
arrayField[i] = "5e99f3f5d3ab06936d360" + i;
}
return arrayField;
}
async function bulkInsertDocs(docs) {
const operations = new Array(docs.length);
for (let i = 0; i < docs.length; i += 1) {
operations[i] = { insertOne: docs[i] };
}
await db.collection(COLLECTION_NAME).bulkWrite(operations);
}
async function benchmark() {
const promises = new Array(REQUESTS_COUNT);
for (let i = 0; i < REQUESTS_COUNT; i += 1) {
promises[i] = fetchDocs();
}
return Promise.all(promises);
}
function fetchDocs() {
return db.collection(COLLECTION_NAME).find({}).toArray();
}
function sleep(seconds) {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}