-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkload.js
99 lines (86 loc) · 2.72 KB
/
workload.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
(function() {
const usage = "\n\nmongo --eval 'var run=1;[options]' workload.js\n" +
"\nOptions:\n" +
" threads default 8. number of worker threads\n" +
" numOps default: 1000. number of total operations per thread\n" +
" docSize default: 128. size of the document to send\n" +
" batchSize default: 1. number of documents per batch\n" +
"\nExample: mongo --eval 'var run=1, threads=8, numOps=10000, batchSize=1000' workload.js\n" +
"\nNote: you must connect to a host while running this script.";
if ((typeof db === 'undefined') || (run !== 1)) {
print(usage);
quit(1);
}
// Clean up input
// ==============
// Threads
if (typeof threads === 'undefined') {
threads = 8;
} else if (typeof threads !== 'number') {
threads = ~~threads;
}
threads = Math.max(threads, 1);
// Num Ops
if (typeof numOps === 'undefined') {
numOps = 1000;
} else if (typeof numOps !== 'number') {
numOps = ~~numOps;
}
numOps = Math.max(numOps, 1);
// Doc Size
if (typeof docSize === 'undefined') {
docSize = 128;
} else if (typeof docSize !== 'number') {
docSize = ~~docSize;
}
docSize = Math.max(docSize, 25);
// Batch Size
if (typeof batchSize === 'undefined') {
batchSize = 1;
} else if (typeof batchSize !== 'number') {
batchSize = ~~batchSize;
}
batchSize = Math.max(batchSize, 1);
// Set up workload
// ===============
db.getSiblingDB('workload').dropDatabase();
const Thread = function() {
this.init.apply(this, arguments);
};
_threadInject(Thread.prototype);
// docs will look like: size, '\x07_id\0', oid, '\x02', 'a\0', str, '\0\0'
// In bytes, 4 + 5 + 12 + 1 + 2 + x + 2 = docSize, so x = docSize - 24
let str = '';
const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < (docSize - 24); i++) {
str += charSet.charAt(~~(Math.random() * charSet.length));
}
const makeThread = function() {
clearRawMongoProgramOutput();
const batches = Math.floor(numOps / batchSize) + 1;
const lastSize = numOps % batchSize;
const doc = {
a: str
};
const host = db.getMongo().host + '/workload';
return new Thread(function(batches, batchSize, lastSize, doc, host) {
const myDb = connect(host);
while (batches--) {
const size = (batches ? batchSize : lastSize);
db.test.insert(Array(size).fill(doc));
}
}, batches, batchSize, lastSize, doc, host);
};
// Run the worker threads
// ======================
const threadArr = [];
while (threadArr.push(makeThread()) < threads) {
; // pass
}
threadArr.forEach((t) => {
t.start()
});
threadArr.forEach((t) => {
t.join()
});
})();