forked from gharriso/MongoDBPerformanceTuningBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoTuning.js
executable file
·832 lines (784 loc) · 26.2 KB
/
mongoTuning.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/*
* Master utility script for the Apress book "MongoDB Performance Tuning" containing all functions from the "scripts" directory.
*
* @Authors: Michael Harrison ([email protected]) and Guy Harrison ([email protected]).
* @Date: 2020-09-03T17:54:50+10:00
* @Last modified by: Michael Harrison
* @Last modified time: 2021-04-08T10:53:12+10:00
*
*/
var mongoTuning = {};
// SERVER STATS
/**
* Base function that will collect and shape raw server statistics data.
*
* @returns {FlatServerStats} An object containing many different server statistics.
*/
mongoTuning.serverStatistics = function () {
const output = {};
let value;
let rate;
output.statistics = [];
var serverStats = mongoTuning.flattenServerStatus(db.serverStatus()).stats; // eslint-disable-line
const uptime = serverStats.uptimeMillis / 1000; // seconds with precision
Object.keys(serverStats).forEach((stat) => {
// print(stat);
value = serverStats[stat];
rate = null;
if (typeof value === 'number') {
rate = (value / uptime).toFixed(4);
}
if (!stat.match(/_mongo/)) {
output.statistics.push({
statistic: stat,
value,
ratePs: rate,
});
}
});
return output;
};
/**
* Helper function for monitoring the MongoDB server over the duration and then
* calculating delta and final values across the duration for key statistics.
*
* @param {int} duration - How long to monitor the server for.
* @returns {Object} - Data object containing Deltas and final values.
*/
mongoTuning.monitorServer = function (duration) {
let runningStats;
let initialStats;
const runTime = 0;
initialStats = mongoTuning.serverStatistics();
sleep(duration);
finalStats = mongoTuning.serverStatistics();
const deltas = mongoTuning.serverStatDeltas(initialStats, finalStats);
const finals = mongoTuning.convertStat(finalStats);
return { deltas, finals };
};
mongoTuning.keyServerStats = function (duration, regex) {
const monitoringData = mongoTuning.monitorServer(duration);
return mongoTuning.keyServerStatsFromSample(monitoringData, regex);
};
mongoTuning.keyServerStatsFromSample = function (monitoringData, regex) {
const data = mongoTuning.derivedStatistics(monitoringData);
if (regex) {
return mongoTuning.serverStatSearch(data, regex);
}
return data;
};
/**
* Monitor the MongoDB server for a given duration and return some derived statistics.
*
* @param {int} duration - How many milliseconds to monitor the server for.
* @param {string} regex - OPTIONAL - A string to perform a match against returned statistic keys.
* @returns {Object} - An object containing the derived statistics matching the regex if given.
*/
mongoTuning.monitorServerDerived = function (duration, regex) {
if (!duration) {
duration = 5000;
}
const monitoringData = mongoTuning.monitorServer(duration);
const derivedStats = mongoTuning.derivedStatistics(monitoringData);
if (regex) {
return mongoTuning.serverStatSearch(derivedStats, regex);
}
return derivedStats;
};
/**
* Monitor the MongoDB server for a given duration and return the raw statistics.
*
* @param {int} duration - How many milliseconds to monitor the server for.
* @param {string} regex - OPTIONAL - A string to perform a match against returned statistic keys.
* @returns {Object} - An object containing the derived statistics matching the regex if given.
*/
mongoTuning.monitorServerRaw = function (duration, regex) {
if (!duration) {
duration = 5000;
}
const monitoringData = mongoTuning.monitorServer(duration);
if (regex) {
return mongoTuning.serverStatSearchRaw(monitoringData, regex);
}
return monitoringData;
};
/**
* Converts structured mongodb ServerStatus object into a flattened array of stats.
*
* @param {RawServerStatus} dbServerStatus - The raw result of the mongodb command.
* @returns {FlatServerStatus} - Flattened array of server status metrics.
*/
mongoTuning.flattenServerStatus = function (dbServerStatus) {
const flattenedServerStatus = {};
flattenedServerStatus.stats = {};
function internalflattenServerStatus(serverStatus, rootTerm) {
let prefix = '';
if (arguments.length > 1) {
prefix = rootTerm + '.';
}
Object.getOwnPropertyNames(serverStatus).forEach((key) => {
if (key !== '_mongo') {
let value = serverStatus[key];
// eslint-disable-next-line
if (value.constructor === NumberLong) {
value = value.toNumber();
}
const valtype = typeof value;
const fullkey = prefix + key;
// print(key, value, valtype, fullkey);
if (valtype == 'object') {
// recurse into nested objects
internalflattenServerStatus(value, prefix + key);
} else {
/* No more nesting */
flattenedServerStatus.stats[fullkey] = value;
}
}
});
}
internalflattenServerStatus(dbServerStatus);
return flattenedServerStatus;
};
/**
* Flattens complex server statistics into a simpler form.
*
* @param {RawServerStatus} serverStat - The raw server statistics result from Mongo.
* @returns {SimpleServerStatus} - A single object with key value pairs for each stat.
*/
mongoTuning.convertStat = function (serverStat) {
const returnStat = {};
serverStat.statistics.forEach((stat) => {
returnStat[stat.statistic] = stat.value;
});
return returnStat;
};
/**
* Takes two sets of server statistics and calculates the difference and rate of change.
* @param {Object} initialStats - First set of statistics.
* @param {Object} finalStats - Second set of statistics.
* @returns {Array<Object>} - Array of delta information.
*/
mongoTuning.serverStatDeltas = function (initialStats, finalStats) {
const stat1 = mongoTuning.convertStat(initialStats);
const stat2 = mongoTuning.convertStat(finalStats);
let delta;
let rate;
const statDelta = {};
statDelta.timeDelta = stat2.uptime - stat1.uptime;
Object.keys(stat2).forEach((key) => {
// print(key,typeof stat2[key]);
if (typeof stat2[key] === 'number') {
delta = stat2[key] - stat1[key];
rate = delta / statDelta.timeDelta;
} else {
delta = null;
rate = null;
}
statDelta[key] = {
lastValue: stat2[key],
firstValue: stat1[key],
delta,
rate,
};
});
return statDelta;
};
/**
* Simple helper function for searching derived server stats for matching keys.
*
* @param {Object} stats - The server statistics to search.
* @param {String} regex - Regex to search for statistic keys.
* @returns {Array<Object>} - An array of matching key value pairs.
*/
mongoTuning.serverStatSearch = function (stats, regex) {
const returnArray = {};
Object.keys(stats).forEach((key) => {
if (key.match(regex)) {
returnArray[key] = stats[key];
}
});
return returnArray;
};
/**
* Simple helper function for searching raw server stats for matching keys.
*
* @param {Object} stats - The server statistics to search.
* @param {String} regex - Regex to search for statistic keys.
* @returns {Array<Object>} - An array of matching key value pairs.
*/
mongoTuning.serverStatSearchRaw = function (stats, regex) {
const returnArray = { deltas: {}, finals: {} };
// First filter deltas.
Object.keys(stats.deltas).forEach((key) => {
if (key.match(regex)) {
returnArray.deltas[key] = stats.deltas[key];
}
});
// Then filter finals
Object.keys(stats.finals).forEach((key) => {
if (key.match(regex)) {
returnArray.finals[key] = stats.finals[key];
}
});
return returnArray;
};
/**
* Derive some summary statistics from observed values.
* @param {Object} serverData - Server data gathered from mongoTuning.monitorServer, should contain deltas and final values.
* @returns {Object} - Data object containing the derived statistics.
*/
mongoTuning.derivedStatistics = function (serverData) {
const { deltas, finals } = serverData;
const data = {};
const descriptions = {};
// *********************************************
// Network counters
// *********************************************
data.netKBInPS = deltas['network.bytesIn'].rate / 1024;
data.netKBOutPS = deltas['network.bytesOut'].rate / 1024;
// ********************************************
// Activity counters
// ********************************************
data.intervalSeconds = deltas.timeDelta;
data.queryPS = deltas['opcounters.query'].rate;
data.getmorePS = deltas['opcounters.getmore'].rate;
data.commandPS = deltas['opcounters.command'].rate;
data.insertPS = deltas['opcounters.insert'].rate;
data.updatePS = deltas['opcounters.update'].rate;
data.deletePS = deltas['opcounters.delete'].rate;
// ********************************************
// Document counters
// ********************************************
data.docsReturnedPS = deltas['metrics.document.returned'].rate;
data.docsUpdatedPS = deltas['metrics.document.updated'].rate;
data.docsInsertedPS = deltas['metrics.document.inserted'].rate;
data.ixscanDocsPS = deltas['metrics.queryExecutor.scanned'].rate;
data.collscanDocsPS = deltas['metrics.queryExecutor.scannedObjects'].rate;
descriptions.scansToDocumentRatio =
'Ratio of documents scanned to documents returned';
if (data.docsReturnedPS > 0) {
data.scansToDocumentRatio =
(data.ixscanDocsPS + data.collscanDocsPS) / data.docsReturnedPS;
} else {
data.scansToDocumentRatio = 0;
}
// ********************************************
// Transaction statistics
// ********************************************
data.transactionsStartedPS = deltas['transactions.totalStarted'].rate;
data.transactionsAbortedPS = deltas['transactions.totalAborted'].rate;
data.transactionsCommittedPS = deltas['transactions.totalCommitted'].rate;
if (data.transactionsStartedPS > 0) {
data.transactionAbortPct =
(data.transactionsAbortedPS * 100) / data.transactionsStartedPS;
} else {
data.transactionAbortPct = 0;
}
if (deltas['opLatencies.reads.ops'].delta > 0) {
data.readLatencyMs =
deltas['opLatencies.reads.latency'].delta /
deltas['opLatencies.reads.ops'].delta /
1000;
} else data.readLatency = 0;
if (deltas['opLatencies.writes.ops'].delta > 0) {
data.writeLatencyMs =
deltas['opLatencies.writes.latency'].delta /
deltas['opLatencies.writes.ops'].delta /
1000;
} else data.writeLatency = 0;
if (deltas['opLatencies.commands.ops'].delta > 0) {
data.cmdLatencyMs =
deltas['opLatencies.commands.latency'].delta /
deltas['opLatencies.commands.ops'].delta /
1000;
} else data.cmdLatency = 0;
data.connections = deltas['connections.current'].lastValue;
data.availableConnections = deltas['connections.available'].firstValue;
data.assertsPS =
deltas['asserts.regular'].rate +
deltas['asserts.warning'].rate +
deltas['asserts.msg'].rate +
deltas['asserts.user'].rate +
deltas['asserts.rollovers'].rate;
data.activeReaders = finals['globalLock.activeClients.readers'];
data.activeWriters = finals['globalLock.activeClients.writers'];
data.queuedReaders = finals['globalLock.currentQueue.readers'];
data.queuedWriters = finals['globalLock.currentQueue.writers'];
data.globalLockQueue = {
readActive: data.activeReaders,
readQueued: data.queuedReaders,
writeActive: data.activeWriters,
writeQueued: data.queuedWriters,
};
// *********************************************************
// Memory counters
// *********************************************************
data.cacheReadQAvailable =
deltas['wiredTiger.concurrentTransactions.read.available'].lastValue;
data.cacheReadQUsed =
deltas['wiredTiger.concurrentTransactions.read.out'].lastValue;
data.cacheWriteQAvailable =
deltas['wiredTiger.concurrentTransactions.write.available'].lastValue;
data.cacheWriteQUsed =
deltas['wiredTiger.concurrentTransactions.write.out'].lastValue;
data.cacheGetsPS =
deltas['wiredTiger.cache.pages requested from the cache'].rate;
data.cacheReadInsPS = deltas['wiredTiger.cache.pages read into cache'].rate;
descriptions.cacheHitRate = 'Hit Rate in the wiredTigerCache ';
if (data.cacheGetsPS > 0) {
data.cacheHitRate =
((data.cacheGetsPS - data.cacheReadInsPS) * 100) / data.cacheGetsPS;
} else {
data.cacheHitRate = 0;
}
data.evictionsPs = deltas['wiredTiger.cache.internal pages evicted'].rate;
data.evictionBlockedPs =
deltas['wiredTiger.thread-yield.page acquire eviction blocked'].rate;
if (data.evictionsPs > 0) {
data.evictionBlockRate = (data.evictionBlockedPs * 100) / data.evictionsPs;
} else data.evictionBlockRate = 0;
if (data.cacheReadInsPS > 0) {
data.evictionRate = (data.evictionsPs * 100) / data.cacheReadInsPS;
} else data.evictionRate = 0;
data.cacheHighWaterMB =
deltas['wiredTiger.cache.maximum bytes configured'].lastValue / 1048576;
data.cacheSizeMB =
deltas['wiredTiger.cache.bytes currently in the cache'].lastValue / 1048576;
data.diskBlockReadsPS = deltas['wiredTiger.block-manager.blocks read'].rate;
data.diskBlockWritesPS =
deltas['wiredTiger.block-manager.blocks written'].rate;
data.logKBRatePS = deltas['wiredTiger.log.log bytes written'].rate / 1024;
data.logSyncTimeRateMsPS =
deltas['wiredTiger.log.log sync time duration (usecs)'].rate / 1000;
data.logSyncOpsPS = deltas['wiredTiger.log.log sync operations'].rate;
if (data.logSyncOpsPS > 0) {
data.logAvgSyncTime = data.logSyncTimeRateMsPS / data.logSyncOpsPS;
} else data.logAvgSyncTime = 0;
// *********************************************************
// Disk IO
// *********************************************************
Object.keys(data).forEach((key) => {
if (data[key] % 1 > 0.01) {
data[key] = data[key].toFixed(4);
}
});
return data;
};
mongoTuning.memoryReport = () => {
const serverStats = db.serverStatus();
print('Mongod virtual memory ', serverStats.mem.virtual);
print('Mongod resident memory', serverStats.mem.resident);
print(
'Wired Tiger cache size',
Math.round(
serverStats.wiredTiger.cache['bytes currently in the cache'] / 1048576
)
);
};
// QUERY PROFILER
mongoTuning.profileQuery = () => {
const profileQuery = db.system.profile.aggregate([
{
$group: {
_id: { cursorid: '$cursorid' },
count: { $sum: 1 },
'queryHash-max': { $max: '$queryHash' },
'millis-sum': { $sum: '$millis' },
'ns-max': { $max: '$ns' },
},
},
{
$group: {
_id: {
queryHash: '$queryHash-max',
collection: '$ns-max',
},
count: { $sum: 1 },
millis: { $sum: '$millis-sum' },
},
},
{ $sort: { millis: -1 } },
{ $limit: 10 },
]);
return profileQuery;
};
/**
* Get details of a query from system.profile using the queryhash
*
* @param {string} queryHash - The queryHash of the query of interest.
*
* @returns {queryDetails} query ns, command and basic statistics
*/
mongoTuning.getQueryByHash = function (queryHash) {
return db.system.profile.findOne(
{ queryHash },
{ ns: 1, command: 1, docsExamined: 1, millis: 1, planSummary: 1 }
);
};
/**
* Fetch simplified profiling info for a given database and namespace.
*
* @param {string} dbName - The name of the database to fetch profiling data for.
* @param {string} collectionName - The name of the collection to fetch profiling data for.
*
* @returns {ProfilingData} Profiling data for the given namespace (queries only), grouped and simplified.
*/
mongoTuning.getProfileData = function (dbName, collectionName) {
var mydb = db.getSiblingDB(dbName); // eslint-disable-line
const ns = dbName + '.' + collectionName;
const profileData = mydb
.getSiblingDB(dbName)
.getCollection('system.profile')
.aggregate([
{
$match: {
ns,
op: 'query',
},
},
{
$group: {
_id: {
filter: '$query.filter',
},
count: {
$sum: 1,
},
'millis-sum': {
$sum: '$millis',
},
'nreturned-sum': {
$sum: '$nreturned',
},
'planSummary-first': {
$first: '$planSummary',
},
'docsExamined-sum': {
$sum: '$docsExamined',
},
},
},
{
$sort: {
'millis-sum': -1,
},
},
]);
return profileData;
};
// CURENT OP
mongoTuning.printCurrentOps = function (printZeroSecs, printInternalProcess) {
// console.log(COps);
var mydb = db.getSiblingDB('admin'); // eslint-disable-line
var output = [];
var result = {};
var currentOps = mydb.currentOp();
if (currentOps.hasOwnProperty('errmsg')) {
output.push({
error: currentOps.errmsg,
});
} else {
var opArray = [];
// print(clusterOps); print("+++++++++++++++"); print(JSON.stringify(currentOps));
var inprog = currentOps.inprog;
var server = currentOps.server;
inprog.forEach(function (currentOp) {
// printjson(currentOp);
var secs = 0;
if (currentOp.hasOwnProperty('secs_running')) {
secs = currentOp.secs_running;
}
var myop = currentOp.op;
var query = {};
if ('query' in currentOp) {
query = JSON.stringify(currentOp.query);
} else if ('command' in currentOp) {
query = JSON.stringify(currentOp.command);
}
if (query.length > 2) {
myop = query;
}
opArray.push({
server: server,
desc: currentOp.desc,
secs: secs,
ns: currentOp.ns,
op: myop,
opid: currentOp.opid,
});
//
});
opArray.sort(function (a, b) {
// Sort in desc order of seconds active
return b.secs - a.secs;
});
// printjson(opArray); // eslint-disable-line
opArray.forEach(function (op) {
if (
(printZeroSecs === true || op.secs > 0) &&
(printInternalProcess === true ||
(op.desc !== 'rsBackgroundSync' &&
op.desc !== 'ReplBatcher' &&
op.desc !== 'rsSync' &&
op.desc !== 'WT RecordStoreThread: local.oplog.rs' &&
op.desc !== 'SyncSourceFeedback' &&
op.desc !== 'NoopWriter' &&
op.ns != 'local.oplog.rs'))
) {
output.push({
desc: op.desc,
secs: op.secs,
ns: op.ns,
op: op.op,
opid: op.opid,
});
}
});
}
result.ops = output;
return result;
};
mongoTuning.opForKillList = function () {
var output = [];
mongoTuning.printCurrentOps(true, false).ops.forEach(function (op) {
var outStr =
op.opid + ' ' + op.secs + ' seconds running. ' + op.desc + ' ' + op.ns;
output.push(outStr);
});
return output;
};
mongoTuning.killOp = function (opIdString) {
var opid = opIdString.split(' ')[0];
if (opid.indexOf(':') == -1) {
opid = parseInt(opid); // eslint-disable-line
}
print('Issuing kill on ' + opid);
var ret = db.killOp(opid); //eslint-disable-line
printjson(ret); // eslint-disable-line
};
// EXPLAIN
mongoTuning.prepExplain = (explainInput) => {
// Takes as input explain output in one of the follow formats:
// A fully explain JSON document, in which case emits winningPlan
// An explain() cursor in which case, extracts the winningPlan from the cursor
// A specific plan step in which case just returns that
const keys = Object.keys(explainInput);
// printjson(keys);
if (keys.includes('queryPlanner')) {
// This looks like a top level Explain
return explainInput.queryPlanner.winningPlan;
} else if (keys.includes('hasNext')) {
// This looks like a cursor
if (explainInput.hasNext()) {
return mongoTuning.prepExplain(explainInput.next());
}
return { ok: 0, error: 'No plan found' };
} else if (keys.includes('stage')) {
// This looks like an actual plan
return explainInput;
}
return { ok: 0, error: 'No plan found' };
};
mongoTuning.quickExplain = (inputPlan) => {
// Takes as input an explain Plan. Emits a simplified
// version of that plan
const explainPlan = mongoTuning.prepExplain(inputPlan);
let stepNo = 1;
const printSpaces = function (n) {
let s = '';
for (let i = 1; i < n; i++) {
s += ' ';
}
return s;
};
const printInputStage = function (step, depth) {
if ('inputStage' in step) {
printInputStage(step.inputStage, depth + 1);
}
if ('inputStages' in step) {
step.inputStages.forEach((inputStage) => {
printInputStage(inputStage, depth + 1);
});
}
if ('indexName' in step) {
print(stepNo++, printSpaces(depth), step.stage, step.indexName);
} else {
print(stepNo++, printSpaces(depth), step.stage);
}
};
printInputStage(explainPlan, 1);
};
mongoTuning.prepExecutionStats = (explainInput) => {
// Takes as input explain output in one of the follow formats:
// A fully explain JSON document, in which case emits executionStats
// An explain() cursor in which case, extracts the exectionStats from the cursor
const keys = Object.keys(explainInput);
if (keys.includes('executionStats')) {
// This looks like a top level Explain
return explainInput.executionStats;
} else if (keys.includes('hasNext')) {
// This looks like a cursor
if (explainInput.hasNext()) {
return mongoTuning.prepExecutionStats(explainInput.next());
}
} else if (explainInput.stages) {
} else return { ok: 0, error: 'No executionStats found' };
};
mongoTuning.executionStats = (execStatsIn) => {
if (execStatsIn.stages) {
return aggregationExecutionStats(execStatsIn);
}
const execStats = mongoTuning.prepExecutionStats(execStatsIn);
// printjson(execStats);
let stepNo = 1;
print('\n');
const printSpaces = function (n) {
let s = '';
for (let i = 1; i < n; i++) {
s += ' ';
}
return s;
};
var printInputStage = function (step, depth) {
if ('inputStage' in step) {
printInputStage(step.inputStage, depth + 1);
}
if ('inputStages' in step) {
step.inputStages.forEach((inputStage) => {
printInputStage(inputStage, depth + 1);
});
}
if ('shards' in step) {
step.shards.forEach((inputShard) => {
printInputStage(inputShard, depth + 1);
});
}
if ('shardName' in step) {
printInputStage(step.executionStages, depth + 1);
}
let extraData = '(';
let printStage = 'unknown';
if ('stage' in step) {
printStage = step.stage;
}
if ('shardName' in step) {
printStage = 'Shard ==> ' + step.shardName;
}
if ('indexName' in step) extraData += ' ' + step.indexName;
if ('executionTimeMillisEstimate' in step) {
extraData += ' ms:' + step.executionTimeMillisEstimate;
}
if ('executionTimeMillis' in step) {
extraData += ' ms:' + step.executionTimeMillis;
}
if ('nReturned' in step) {
extraData += ' returned:' + step.nReturned;
}
if ('keysExamined' in step) extraData += ' keys:' + step.keysExamined;
if ('docsExamined' in step) extraData += ' docs:' + step.docsExamined;
if ('nWouldModify' in step && step.nWouldModify !== false)
extraData += ' upd:' + step.nWouldModify;
if ('wouldInsert' in step && step.wouldInsert !== false)
extraData += ' ins:' + step.wouldInsert;
extraData += ')';
print(stepNo++, printSpaces(depth), printStage, extraData);
};
printInputStage(execStats.executionStages, 1);
print(
'\nTotals: ms:',
execStats.executionTimeMillis,
' keys:',
execStats.totalKeysExamined,
' Docs:',
execStats.totalDocsExamined
);
};
mongoTuning.aggregationExecutionStats = (execStatsIn) => {
// printjson(execStatsIn);
let execStats = {};
let stepNo = 1;
if (
execStatsIn.stages &&
execStatsIn.stages[0].$cursor &&
execStatsIn.stages[0].$cursor.executionStats
) {
execStats = execStatsIn.stages[0].$cursor.executionStats;
} else if (execStatsIn.executionStats) {
execStats = execStatsIn.executionStats;
}
print('\n');
const printSpaces = function (n) {
let s = '';
for (let i = 1; i < n; i++) {
s += ' ';
}
return s;
};
var printInputStage = function (step, depth) {
if ('inputStage' in step) {
printInputStage(step.inputStage, depth + 1);
}
if ('inputStages' in step) {
step.inputStages.forEach((inputStage) => {
printInputStage(inputStage, depth + 1);
});
}
let extraData = '(';
if ('indexName' in step) extraData += ' ' + step.indexName;
if ('executionTimeMillisEstimate' in step) {
extraData += ' ms:' + step.executionTimeMillisEstimate;
}
if ('keysExamined' in step) extraData += ' keys:' + step.keysExamined;
if ('docsExamined' in step) {
extraData += ' docsExamined:' + step.docsExamined;
}
if ('nReturned' in step) extraData += ' nReturned:' + step.nReturned;
extraData += ')';
print(stepNo++, printSpaces(1), step.stage, extraData);
};
const printAggStage = function (stage, depth) {
let extraData = '(';
if ('executionTimeMillisEstimate' in stage) {
extraData += ' ms:' + stage.executionTimeMillisEstimate;
}
if ('docsExamined' in stage) extraData += ' examined:' + stage.docsExamined;
if ('nReturned' in stage) extraData += ' returned:' + stage.nReturned;
extraData += ')';
print(
stepNo++,
printSpaces(depth),
Object.keys(stage)
.find((key) => key.match(/$/))
.toUpperCase(),
extraData
);
};
if (execStats.executionStages) {
printInputStage(execStats.executionStages, 1);
}
if (execStatsIn && execStatsIn.stages) {
for (let stageNum = 1; stageNum < execStatsIn.stages.length; stageNum++) {
if (execStatsIn.stages[stageNum]) {
printAggStage(execStatsIn.stages[stageNum], 1);
}
}
}
print(
'\nTotals: ms:',
execStats.executionTimeMillis,
' keys:',
execStats.totalKeysExamined,
' Docs:',
execStats.totalDocsExamined
);
};
// COMPACT
mongoTuning.reusablePct = function (collectionName) {
let collstats = db.getCollection(collectionName).stats();
let reusable =
collstats.wiredTiger['block-manager']['file bytes available for reuse'];
let size = collstats.wiredTiger['block-manager']['file size in bytes'];
let reusablePct = Math.round((reusable * 100) / size);
print('Size:', size, ' Reusable: ', reusable, ' ', reusablePct, '%');
return Math.round((reusable * 100) / size);
};