Skip to content

Add performance spec tests for queries against large collections #1802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/firestore/test/unit/specs/describe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import { ExclusiveTestFunction, PendingTestFunction } from 'mocha';

import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
import { assert } from '../../../src/util/assert';
import { addEqualityMatcher } from '../../util/equality_matcher';
Expand Down Expand Up @@ -48,6 +50,7 @@ const KNOWN_TAGS = [

// TODO(mrschmidt): Make this configurable with mocha options.
const RUN_BENCHMARK_TESTS = false;
const BENCHMARK_TEST_TIMEOUT_MS = 10 * 1000;

// The format of one describeSpec written to a JSON file.
interface SpecOutputFormat {
Expand Down Expand Up @@ -79,7 +82,10 @@ export function setSpecJSONHandler(writer: (json: string) => void): void {
}

/** Gets the test runner based on the specified tags. */
function getTestRunner(tags, persistenceEnabled): Function {
function getTestRunner(
tags,
persistenceEnabled
): ExclusiveTestFunction | PendingTestFunction {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat.

if (tags.indexOf(NO_WEB_TAG) >= 0) {
return it.skip;
} else if (
Expand All @@ -102,6 +108,15 @@ function getTestRunner(tags, persistenceEnabled): Function {
}
}

/** If required, returns a custom test timeout for long-running tests */
function getTestTimeout(tags): number | undefined {
if (tags.indexOf(BENCHMARK_TAG) >= 0) {
return BENCHMARK_TEST_TIMEOUT_MS;
} else {
return undefined;
}
}

/**
* Like it(), but for spec tests.
* @param name A name to give the test.
Expand Down Expand Up @@ -153,9 +168,10 @@ export function specTest(
for (const usePersistence of persistenceModes) {
const spec = builder();
const runner = getTestRunner(tags, usePersistence);
const timeout = getTestTimeout(tags);
const mode = usePersistence ? '(Persistence)' : '(Memory)';
const fullName = `${mode} ${name}`;
runner(fullName, async () => {
const queuedTest = runner(fullName, async () => {
const start = Date.now();
await spec.runAsTest(fullName, usePersistence);
const end = Date.now();
Expand All @@ -164,6 +180,10 @@ export function specTest(
console.log(`Runtime: ${end - start} ms.`);
}
});

if (timeout !== undefined) {
queuedTest.timeout(timeout);
}
}
} else {
assert(
Expand Down
57 changes: 54 additions & 3 deletions packages/firestore/test/unit/specs/perf_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describeSpec(
fromCache: true,
hasPendingWrites: true
})
.writeAcks(`collection/${i}`, ++currentVersion)
.writeAcks(`collection/${i}`, docRemote.version.toMicroseconds())
.watchAcksFull(query, ++currentVersion, docRemote)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that in the existing code, this watchAck will get ignored since docRemote.version here is greater than the writeAck version.

.expectEvents(query, { metadata: [docRemote] })
.userUnlistens(query)
Expand Down Expand Up @@ -143,7 +143,7 @@ describeSpec(
fromCache: true,
hasPendingWrites: true
})
.writeAcks(`collection/doc`, ++currentVersion)
.writeAcks(`collection/doc`, docRemote.version.toMicroseconds())
.watchAcksFull(query, ++currentVersion, docRemote)
.expectEvents(query, { metadata: [docRemote] });

Expand All @@ -163,7 +163,7 @@ describeSpec(
modified: [docLocal],
hasPendingWrites: true
})
.writeAcks(`collection/doc`, ++currentVersion)
.writeAcks(`collection/doc`, docRemote.version.toMicroseconds())
.watchSends({ affects: [query] }, docRemote)
.watchSnapshots(++currentVersion)
.expectEvents(query, { metadata: [docRemote] });
Expand Down Expand Up @@ -292,5 +292,56 @@ describeSpec(

return steps;
});

specTest(
'Add 500 documents, issue 10 queries that return 10 documents each, unlisten',
[],
() => {
const documentCount = 500;
const matchingCount = 10;
const queryCount = 10;

const steps = spec().withGCEnabled(false);

const collPath = `collection`;
const query = Query.atPath(path(collPath)).addOrderBy(orderBy('val'));
steps.userListens(query).watchAcks(query);

const allDocs: Document[] = [];

let currentVersion = 1;
// Create `documentCount` documents.
for (let j = 0; j < documentCount; ++j) {
const document = doc(`${collPath}/doc${j}`, ++currentVersion, {
val: j
});
allDocs.push(document);
steps.watchSends({ affects: [query] }, document);
}

steps.watchCurrents(query, `current-version-${++currentVersion}`);
steps.watchSnapshots(currentVersion);
steps.expectEvents(query, { added: allDocs });
steps.userUnlistens(query).watchRemoves(query);

for (let i = 1; i <= STEP_COUNT; ++i) {
// Create `queryCount` listens, each against collPath but with a
// unique query constraint.
for (let j = 0; j < queryCount; ++j) {
const partialQuery = Query.atPath(path(collPath))
.addFilter(filter('val', '>=', j * matchingCount))
.addFilter(filter('val', '<', (j + 1) * matchingCount));
steps.userListens(partialQuery);
steps.expectEvents(partialQuery, {
added: allDocs.slice(j * matchingCount, (j + 1) * matchingCount),
fromCache: true
});
steps.userUnlistens(partialQuery);
}
}

return steps;
}
);
}
);