This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
303 lines (279 loc) · 10.5 KB
/
main.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
const {Firestore} = require('@google-cloud/firestore');
const sleep = require('sleep');
const fs = require('fs');
const firestore = new Firestore();
// Global variables to configure tests.
const NUM_LOOPS = 100; // Number of loops to run.
const NUM_WRITES_PER_LOOP = 5000; // Number of document writes in each loop.
const BATCH_SIZES = [10, 20, 30, 50, 100, 250];
const NUM_ENTITIES = 100; // Number of entities in each document write.
const USE_SAME_COLLECTION = true; // true: writes all docs into the same collection.
const RUN_WRITE_BATCH_TEST = true; // true: runs BulkWriter test
const USE_RANDOM_FIELDS = false; // true: use overlapping fields
const TIMED_TEST = true; // true: perform a 30-minute benchmark test
const THROTTLING_SETTING = false;
let errored = false;
const writer = firestore.bulkWriter({throttling: THROTTLING_SETTING});
writer.onWriteError((err) => {
errored = true;
return true;
});
async function timedTest() {
const [startingData, updateData] = USE_RANDOM_FIELDS ?
[generateMultiRandomFields(), generateMultiRandomFields()] :
[generateMultiOverlappingFields(), generateMultiOverlappingFields()];
if (RUN_WRITE_BATCH_TEST) {
for (const batchSize of BATCH_SIZES) {
writer._setMaxBatchSize(batchSize);
await runTimedWriteBatchTest(batchSize, startingData, updateData);
}
} else {
for (const batchSize of BATCH_SIZES) {
writer._setMaxBatchSize(batchSize);
await runTimedBulkWriterTest( batchSize, startingData, updateData);
}
}
}
async function main() {
const [startingData, updateData] = USE_RANDOM_FIELDS ?
[generateMultiRandomFields(), generateMultiRandomFields()] :
[generateMultiOverlappingFields(), generateMultiOverlappingFields()];
if (RUN_WRITE_BATCH_TEST) {
for (const batchSize of BATCH_SIZES) {
writer._setMaxBatchSize(batchSize);
await runWriteBatchTest(batchSize, startingData, updateData);
}
} else {
for (const batchSize of BATCH_SIZES) {
writer._setMaxBatchSize(batchSize);
await runBulkWriterTest( batchSize, startingData, updateData);
}
}
}
async function runTimedBulkWriterTest(batchSize, startingData) {
console.log('--------- BulkWriter Timed: Batch size: ' + batchSize + ', numWrites: ' + NUM_WRITES_PER_LOOP);
let totalTimeElapsedMs = 0;
let i =0;
// Run set test
while (totalTimeElapsedMs < 30 * 60 * 1000) {
await sleep.sleep(1);
const startTime = Date.now();
for (let j = 0; j < NUM_WRITES_PER_LOOP; j++) {
const collection = USE_SAME_COLLECTION ? firestore.collection('benchmark') : firestore.collection(randStr());
const docRef = collection.doc();
writer.set(docRef, startingData).catch((err) => {
console.log('set: ' + j + ', failed with: ', err);
});
}
await writer.flush();
const endTime = Date.now();
const timeElapsed = (endTime - startTime);
totalTimeElapsedMs += timeElapsed;
const pastLoopsQps = Math.round(NUM_WRITES_PER_LOOP / timeElapsed* 1000);
const loopCurrentQps = Math.round(((i+1)* NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
const logStr = 'Run #' + i + ': past loop qps: ' + pastLoopsQps + ', avg QPS: ' + loopCurrentQps + ', time elapsed: ' + millisToMinutesAndSeconds(totalTimeElapsedMs) + ' errored: ' + errored;
console.log(logStr);
fs.appendFileSync('./bw-100.txt', logStr + '\n');
i++;
errored = false;
}
const qps = Math.round((i * NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
console.log('average set QPS:', qps);
}
async function runTimedWriteBatchTest(batchSize, startingData, updateData) {
console.log('--------- WriteBatch Timed: Batch size: ' + batchSize + ', numWrites: '
+ NUM_WRITES_PER_LOOP);
let totalTimeElapsedMs = 0;
let i =0;
// Run set test
while (totalTimeElapsedMs < 30 * 60 * 1000) {
await sleep.sleep(1);
const startTime = Date.now();
const commits = [];
for (let j = 0; j < NUM_WRITES_PER_LOOP / batchSize; j++) {
const batch = firestore.batch();
for (let k = 0; k < batchSize; k++) {
const collection = USE_SAME_COLLECTION ? firestore.collection(
'benchmark') : firestore.collection(randStr());
const docRef = collection.doc();
batch.set(docRef, startingData);
}
commits.push(batch.commit().catch((e) => {
console.log('errored');
errored = true;
}));
}
await Promise.all(commits);
const endTime = Date.now();
const timeElapsed = (endTime - startTime);
totalTimeElapsedMs += timeElapsed;
const pastLoopsQps = Math.round(NUM_WRITES_PER_LOOP / timeElapsed* 1000);
const loopCurrentQps = Math.round(((i+1)* NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
const logStr = 'Run #' + i + ': past loop qps: ' + pastLoopsQps + ', avg QPS: ' + loopCurrentQps + ', time elapsed: ' + millisToMinutesAndSeconds(totalTimeElapsedMs) + ' errored: ' + errored;
console.log(logStr);
fs.appendFileSync('./wb-100.txt', logStr + '\n');
i++;
errored = false;
}
let qps = Math.round(
(i * NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
console.log('average set QPS:', qps);
}
async function runBulkWriterTest(batchSize, startingData, updateData) {
console.log('--------- BulkWriter: Batch size: ' + batchSize + ', numWrites: ' + NUM_WRITES_PER_LOOP);
const docRefs = [];
let totalTimeElapsedMs = 0;
// Run set test
for (let i = 0; i < NUM_LOOPS; i++) {
await sleep.sleep(2);
const startTime = Date.now();
for (let j = 0; j < NUM_WRITES_PER_LOOP; j++) {
const collection = USE_SAME_COLLECTION ? firestore.collection('benchmark') : firestore.collection(randStr());
const docRef = collection.doc();
docRefs.push(docRef);
writer.set(docRef, startingData).catch((err) => {
console.log('set: ' + j + ', failed with: ', err);
});
}
await writer.flush();
const endTime = Date.now();
const timeElapsed = (endTime - startTime);
totalTimeElapsedMs += timeElapsed;
}
const qps = Math.round((NUM_LOOPS * NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
console.log('average set QPS:', qps);
// Run update test
let operationFn = (docRef, data) => {
writer.update(docRef, data).catch((err) => {
console.log('update failed with: ', err);
});
};
await runBulkWriterTestStep(batchSize, updateData, docRefs, operationFn);
// Run delete test
operationFn = (docRef) => {
writer.delete(docRef).catch((err) => {
console.log('delete failed with: ', err);
});
};
await runBulkWriterTestStep(batchSize, {}, docRefs, operationFn);
}
async function runBulkWriterTestStep(batchSize, data, docRefs, operationFn) {
let totalTimeElapsedMs = 0;
let index = 0;
for (let i = 0; i < NUM_LOOPS; i++) {
await sleep.sleep(2);
const startTime = Date.now();
for (let j = 0; j < NUM_WRITES_PER_LOOP; j++) {
operationFn(docRefs[index], data);
index++;
}
await writer.flush();
const endTime = Date.now();
const timeElapsed = (endTime - startTime);
totalTimeElapsedMs += timeElapsed;
}
const qps = Math.round((NUM_LOOPS * NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
console.log('average QPS:', qps);
}
async function runWriteBatchTest(batchSize, startingData, updateData) {
console.log('--------- WriteBatch: Batch size: ' + batchSize + ', numWrites: ' + NUM_WRITES_PER_LOOP);
const docRefs = [];
let totalTimeElapsedMs = 0;
// Run set test
for (let i = 0; i < NUM_LOOPS; i++) {
await sleep.sleep(2);
const startTime = Date.now();
const commits = [];
for (let j = 0; j < NUM_WRITES_PER_LOOP / batchSize; j++) {
const batch = firestore.batch();
for (let k = 0; k < batchSize; k++) {
const collection = USE_SAME_COLLECTION ? firestore.collection('benchmark') : firestore.collection(randStr());
const docRef = collection.doc();
docRefs.push(docRef);
batch.set(docRef, startingData);
}
commits.push(batch.commit().catch((e) => {
console.log('write batch error: ', e);
}));
}
await Promise.all(commits);
const endTime = Date.now();
const timeElapsed = (endTime - startTime);
totalTimeElapsedMs += timeElapsed;
}
let qps = Math.round((NUM_LOOPS * NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
console.log('average set QPS:', qps);
// Run update test
totalTimeElapsedMs = 0;
let index = 0;
for (let i = 0; i < NUM_LOOPS; i++) {
await sleep.sleep(2);
const startTime = Date.now();
const commits = [];
for (let j = 0; j < NUM_WRITES_PER_LOOP / batchSize; j++) {
const batch = firestore.batch();
for (let k = 0; k < batchSize; k++) {
batch.update(docRefs[index], updateData);
index++;
}
commits.push(batch.commit().catch((e) => {
console.log('write batch error: ', e);
}));
}
await Promise.all(commits);
const endTime = Date.now();
const timeElapsed = (endTime - startTime);
totalTimeElapsedMs += timeElapsed;
}
qps = Math.round((NUM_LOOPS * NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
console.log('average update QPS:', qps);
// Run delete test
totalTimeElapsedMs = 0;
index = 0;
for (let i = 0; i < NUM_LOOPS; i++) {
await sleep.sleep(2);
const startTime = Date.now();
const commits = [];
for (let j = 0; j < NUM_WRITES_PER_LOOP / batchSize; j++) {
const batch = firestore.batch();
for (let k = 0; k < batchSize; k++) {
batch.delete(docRefs[index]);
index++;
}
commits.push(batch.commit().catch((e) => {
console.log('write batch error: ', e);
}));
}
await Promise.all(commits);
const endTime = Date.now();
const timeElapsed = (endTime - startTime);
totalTimeElapsedMs += timeElapsed;
}
qps = Math.round((NUM_LOOPS * NUM_WRITES_PER_LOOP) / totalTimeElapsedMs * 1000);
console.log('average delete QPS:', qps);
}
function generateMultiOverlappingFields() {
const out = {};
const startingIndex = Math.floor(Math.random() * 100);
for (let i = startingIndex; i < startingIndex + NUM_ENTITIES; i++) {
out['foo' + i] = 'foo' + i;
}
return out;
}
function generateMultiRandomFields() {
const out = {};
for (let j = 0; j < NUM_ENTITIES; j++) {
out[randStr()] = randStr();
}
return out;
}
function randStr() {
return Math.random().toString(26).substring(2, 15) + Math.random().toString(
26).substring(2, 15);
}
function millisToMinutesAndSeconds(millis) {
const minutes = Math.floor(millis / 60000);
const seconds = ((millis % 60000) / 1000).toFixed(0);
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}
TIMED_TEST ? timedTest() : main();