diff options
author | Rishab Joshi <rishab.joshi@mongodb.com> | 2020-12-30 05:19:55 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-12-30 11:28:21 +0000 |
commit | 9df6363a6699dc20e3abbbd7de386798a4a6a524 (patch) | |
tree | 0690ecb99fe879723632962cc148367f0246f379 /jstests/aggregation | |
parent | 2f8c0125a4c0f58db96e8f9abe35695df054499a (diff) | |
download | mongo-9df6363a6699dc20e3abbbd7de386798a4a6a524.tar.gz |
SERVER-47640 $lookup should increment the serverStatus metrics.queryExecutor counters
Diffstat (limited to 'jstests/aggregation')
-rw-r--r-- | jstests/aggregation/sources/lookup/lookup_query_stats.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/jstests/aggregation/sources/lookup/lookup_query_stats.js b/jstests/aggregation/sources/lookup/lookup_query_stats.js new file mode 100644 index 00000000000..9b5c3ca2099 --- /dev/null +++ b/jstests/aggregation/sources/lookup/lookup_query_stats.js @@ -0,0 +1,114 @@ +/** + * Tests that the queryExecutor stats are correctly returned when $lookup is performed on + * foreign collection. + * + * @tags: [assumes_unsharded_collection] + */ +(function() { +"use strict"; + +const testDB = db.getSiblingDB("lookup_query_stats"); +testDB.dropDatabase(); + +const localColl = testDB.getCollection("local"); +const fromColl = testDB.getCollection("foreign"); +const foreignDocCount = 10; +const localDocCount = 2; + +// Keeps track of the last query execution stats. +let lastScannedObjects = 0; +let lastScannedKeys = 0; + +let insertDocumentToCollection = function(collection, docCount, fieldName) { + const bulk = collection.initializeUnorderedBulkOp(); + for (let i = 0; i < docCount; i++) { + let doc = {_id: i}; + doc[fieldName] = i; + bulk.insert(doc); + } + assert.commandWorked(bulk.execute()); +}; + +let doAggregationLookup = function(localColl, fromColl) { + return localColl.aggregate([ + { + $lookup: { + from: fromColl.getName(), + localField: "localField", + foreignField: "foreignField", + as: "output" + } + }, + { + $sort: {_id: 1} + }]).toArray(); +}; + +let getCurentQueryExecutorStats = function() { + let queryExecutor = testDB.serverStatus().metrics.queryExecutor; + + let curScannedObjects = queryExecutor.scannedObjects - lastScannedObjects; + let curScannedKeys = queryExecutor.scanned - lastScannedKeys; + + lastScannedObjects = queryExecutor.scannedObjects; + lastScannedKeys = queryExecutor.scanned; + + return [curScannedObjects, curScannedKeys]; +}; + +let testQueryExecutorStatsWithCollectionScan = function() { + let output = doAggregationLookup(localColl, fromColl); + + let expectedOutput = [ + {_id: 0, localField: 0, output: [{_id: 0, foreignField: 0}]}, + {_id: 1, localField: 1, output: [{_id: 1, foreignField: 1}]} + ]; + + assert.eq(output, expectedOutput); + + let [curScannedObjects, curScannedKeys] = getCurentQueryExecutorStats(); + + // For collection scan, total scannedObjects should be sum of + // (total documents in local collection + + // total documents in local collection * total documents in foreign collection) + assert.eq(localDocCount + localDocCount * foreignDocCount, curScannedObjects); + + // There is no index in the collection. + assert.eq(0, curScannedKeys); +}; + +let createIndexForCollection = function(collection, fieldName) { + let request = {}; + request[fieldName] = 1; + assert.commandWorked(collection.createIndex(request)); +}; + +let testQueryExecutorStatsWithIndexScan = function() { + createIndexForCollection(fromColl, "foreignField"); + + let output = doAggregationLookup(localColl, fromColl); + + let expectedOutput = [ + {_id: 0, localField: 0, output: [{_id: 0, foreignField: 0}]}, + {_id: 1, localField: 1, output: [{_id: 1, foreignField: 1}]} + ]; + + assert.eq(output, expectedOutput); + + let [curScannedObjects, curScannedKeys] = getCurentQueryExecutorStats(); + + // For index scan, total scannedObjects should be sum of + // (total documents in local collection + total matched documents in foreign collection) + assert.eq(localDocCount + localDocCount, curScannedObjects); + + // Number of keys scanned in the foreign collection should be equal number of keys in local + // collection. + assert.eq(localDocCount, curScannedKeys); +}; + +insertDocumentToCollection(fromColl, foreignDocCount, "foreignField"); +insertDocumentToCollection(localColl, localDocCount, "localField"); + +testQueryExecutorStatsWithCollectionScan(); +testQueryExecutorStatsWithIndexScan(); +}()); |