-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
85 lines (66 loc) · 2.55 KB
/
functions.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
// Some CRUD functions
function bulkInsert(collection, start, end) {
const bulkData = [];
for (let i = start; i <= end; i++) {
// documents have a value (numeric) and 5 random UUID in an array
bulkData.push({ value: i , created_at: new Date(), filler: Array.from({ length: 5 }, () => new UUID()) });
}
collection.insertMany(bulkData);
}
function insertOne(collection) {
const value = Math.floor(1e3*Math.random()) ;
collection.insertOne({ value: value, created_at: new Date(), filler: Array.from({ length: 5 }, () => new UUID()) });
}
function deleteOne(collection) {
const value = Math.floor(1e3*Math.random()) ;
collection.deleteOne( { value: value } );
}
function deleteMany(collection) {
const value = Math.floor(1e3*Math.random()) ;
collection.deleteMany( { value: { $gte: value , $lte: value+10 } } );
}
function deleteAll(collection) {
collection.deleteMany({});
}
function replaceOne(collection) {
const oldValue = Math.floor(1e3*Math.random()) ;
const newValue = Math.floor(1e3*Math.random()) ;
collection.findOneAndReplace({ value: oldValue }, { value: newValue, filler: Array.from({ length: 5 }, () => new UUID()) });
}
function updateOne(collection, query) {
const value = Math.floor(1e3*Math.random()) ;
collection.updateOne({ value: value }, { $inc: { value: 1 } });
}
function queryById(collection) {
return collection.findOne({ "_id": { $gte: new UUID() } });
}
function queryValue(collection) {
const value = Math.floor(1e3*Math.random()) ;
return collection.findOne({ value: value });
}
function queryRange(collection) {
const minValue = Math.floor(1e3*Math.random()) ;
const maxValue = Math.floor(1e3*Math.random()) ;
return collection.find({ value: { $gte: minValue, $lte: maxValue } }).toArray();
}
// function to run another function in a loop
function run(durationInSeconds, func, ...args) {
const startTime = Date.now();
let executionCount = 0;
while ((Date.now() - startTime) / 1000 < durationInSeconds) {
executionCount++;
const timestamp = new Date().toISOString();
func(...args);
console.log(`[${timestamp}] Executing: ${func.name}(${args.join(", ")}) ${(executionCount/((Date.now()-startTime)/1000)).toFixed(1)}/s`);
}
}
print (`
Examples:
// insertMany of 1000 documents during 5 minutes into a clustered collection "demo"
db.demo.drop();
db.runCommand( {
create: "demo",
clusteredIndex: { "key": { _id: 1 }, "unique": true, "name": "demo clustered key" }
} )
run(300, bulkInsert, db.demo, 1, 1000);
`);