summaryrefslogtreecommitdiff
path: root/skunk_2021_perf.js
blob: ab68308646695a48a2a4168d596693270031f946 (plain)
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
(function() {
"use strict";

load("skunk_shared.js");  // For 'SharedSkunkState.'

const collNaive = db.naive;
const collAttributePattern = db.attribute_pattern;
const collEnhancedAttributePattern = db.enhanced_attribute_pattern;
const collSingleWildCard = db.wildcard;
const collCompoundWildCard = db.compound_wildcard;

// Maximum value for the integer fields, minimum is 0
const kMaxInt = 20;

// Minimum and maximum values for the DateTime fields
const kBaseDate = new ISODate("1970-01-01T00:00:00Z");
const kMaxDate = new ISODate("2070-01-01T00:00:00Z");
const kMaximumSeconds = (kMaxDate.getTime() - kBaseDate.getTime()) / 1000;

function addAttributesToQuery(andClauses, attributes, numAttrs, useEnhancedAttributePattern) {
    // If this is the attribute field it needs to be a subdocument or sub-array
    if (attributes instanceof Array) {
        assert.lte(numAttrs, attributes.length);
        // Be sure to avoid selecting 'numAttrs' with replacement. If we allow specifying the same
        // attribute twice, we are likely to specify a query which is tautalogically false such as
        // attr1 = 4 and attr1 = 7.
        attributes = attributes.slice();  // Make a copy to avoid modifying the template.
        for (let i = 0; i < numAttrs; i++) {
            let selectedEntryIndex = Random.randInt(attributes.length);
            let attrEntry = attributes[selectedEntryIndex];
            attributes.splice(selectedEntryIndex, 1);  // Take it out to avoid selecting it again.
            if (useEnhancedAttributePattern) {
                andClauses.push({
                    [SharedSkunkState.kAttributesField]:
                        {k: attrEntry.k, v: SharedSkunkState.getRandomValue(attrEntry.v)}
                });
            } else {
                andClauses.push({
                    [SharedSkunkState.kAttributesField]: {
                        $elemMatch:
                            {k: attrEntry.k, v: SharedSkunkState.getRandomValue(attrEntry.v)}
                    }
                });
            }
        }
    } else {
        let attrEntries = Object.entries(attributes);
        assert.lte(numAttrs, attrEntries.length);
        // Be sure to avoid selecting 'numAttrs' with replacement. If we allow specifying the same
        // attribute twice, we are likely to specify a query which is tautalogically false such as
        // attr1 = 4 and attr1 = 7.
        for (let i = 0; i < numAttrs; i++) {
            let selectedEntryIndex = Random.randInt(attrEntries.length);
            let [attrKey, attrVal] = attrEntries[selectedEntryIndex];
            attrEntries.splice(selectedEntryIndex, 1);  // Take it out to avoid selecting it again.
            andClauses.push({
                [`${SharedSkunkState.kAttributesField}.${attrKey}`]:
                    SharedSkunkState.getRandomValue(attrVal)
            });
        }
    }
}

// Builds a compound equality query based off the template doc querying 'numField' top level fields
// and 'numAttr' attributes stored within 'SharedSkunkState.kAttributesField'. The fields at the top
// level matter for which index may be applicable, so they will be applied in order given in the
// template document. The attributes will be added randomly - so a query on two attributes may be on
// the 4th and 10th attribute for example.
function buildQuery(template, numFields, numAttrs, useEnhancedAttributePattern) {
    let andClauses = [];
    let fieldsAdded = 0;
    for (let [topKey, topValue] of Object.entries(template)) {
        if (fieldsAdded == numFields) {
            break;
        }
        assert(
            topKey != SharedSkunkState.kAttributesField,
            "Added too many top-level fields. Ran out of non-attribute fields in the template (expected non-attribute fields to come first).");
        andClauses.push({[topKey]: SharedSkunkState.getRandomValue(topValue)});
        ++fieldsAdded;
    }
    const attributes = template[SharedSkunkState.kAttributesField];
    addAttributesToQuery(andClauses, attributes, numAttrs, useEnhancedAttributePattern);
    return {$and: andClauses};
}

function avgTime(func, runs) {
    let a = [];
    runs = runs || 10;

    for (var i = 0; i < runs; i++) {
        a.push(Date.timeFunc(func))
    }

    let out = {avg: Array.avg(a), stdDev: Array.stdDev(a)};
    out.sampStdDev = Math.sqrt((1 / (runs - 1)) * (out.stdDev * out.stdDev));
    return out;
}

function buildBatchOfQueries(template, numFields, numAttrs, useEnhancedAttributePattern) {
    const kNumUniqueQueries = 1000;

    let allQueries = [];
    for (let i = 0; i < kNumUniqueQueries; i++) {
        allQueries.push(buildQuery(template, numFields, numAttrs, useEnhancedAttributePattern));
    }
    return allQueries;
}

function runQueries(collection, queryBatch) {
    let numResults = [];
    for (let query of queryBatch) {
        numResults.push(collection.find(query).itcount());
    }
    print("Average number of results: " + Array.avg(numResults));
}

function testAllNumAttrs(collection, templateDoc, maxNumAttrs, useEnhancedAttributePattern) {
    let allTimingInfo = [];
    for (let numAttrs = 1; numAttrs <= maxNumAttrs; ++numAttrs) {
        let queries = buildBatchOfQueries(templateDoc, 3, numAttrs, useEnhancedAttributePattern);
        jsTestLog("Example query: " + tojson(queries[0]));
        jsTestLog(`About to benchmark with ${numAttrs} attributes...`);
        let timingInfo = avgTime(() => runQueries(collection, queries), 5);
        jsTestLog("Avg time: " + tojson(timingInfo));
        allTimingInfo.push(timingInfo);
    }
    return allTimingInfo;
}

const kMaxNumAttrs = 5;
let allStats = {};
jsTestLog("Testing compound wildcard configuration...");
let allTimingInfo =
    testAllNumAttrs(collCompoundWildCard, SharedSkunkState.kTemplateDoc, kMaxNumAttrs);
allStats.compoundWildcard = {
    timingInfo: allTimingInfo
};

jsTestLog("Testing enhanced attribute configuration...");
allTimingInfo = testAllNumAttrs(
    collEnhancedAttributePattern, SharedSkunkState.kAttributeTemplateDoc, kMaxNumAttrs, true);
allStats.enhancedAttributePattern = {
    timingInfo: allTimingInfo
};

jsTestLog("Testing attribute configuration...");
allTimingInfo =
    testAllNumAttrs(collAttributePattern, SharedSkunkState.kAttributeTemplateDoc, kMaxNumAttrs);
allStats.attributePattern = {
    timingInfo: allTimingInfo
};

jsTestLog("Testing naive configuration...");
allTimingInfo = testAllNumAttrs(collNaive, SharedSkunkState.kTemplateDoc, kMaxNumAttrs);
allStats.naive = {
    timingInfo: allTimingInfo
};

/*
jsTestLog("Testing wildcard configuration...");
allTimingInfo = testAllNumAttrs(collSingleWildCard, SharedSkunkState.kTemplateDoc, kMaxNumAttrs);
allStats.singleWildcard = {
    timingInfo: allTimingInfo
};
*/

jsTestLog("Finished! " + tojson(allStats));
}());