Skip to content

[DO NOT SUBMIT] Overlay performance tests #3161

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions firebase-firestore/firebase-firestore.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ dependencies {
annotationProcessor 'com.google.auto.value:auto-value:1.6.5'

testImplementation 'junit:junit:4.12'
testImplementation 'com.github.javafaker:javafaker:1.0.2'
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'org.mockito:mockito-core:2.25.0'
testImplementation ("org.robolectric:robolectric:$robolectricVersion") {
Expand All @@ -178,6 +179,7 @@ dependencies {
testImplementation 'com.google.guava:guava-testlib:12.0-rc2'

androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'com.github.javafaker:javafaker:1.0.2'
androidTestImplementation("com.google.truth:truth:$googleTruthVersion"){
exclude group: "org.codehaus.mojo", module: "animal-sniffer-annotations"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.firestore.performance;

import static com.google.firebase.firestore.FieldValue.arrayUnion;
import static com.google.firebase.firestore.FieldValue.increment;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor;
import static com.google.firebase.firestore.testutil.TestUtil.map;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.github.javafaker.Faker;
import com.github.javafaker.service.RandomService;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.firestore.Source;
import com.google.firebase.firestore.local.Persistence;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

// TODO: Add the skipped tests from typescript.
@RunWith(AndroidJUnit4.class)
public class PerformanceTest {
Map<String, List<DocumentReference>> docs = new HashMap<>();
FirebaseFirestore db = null;

@Before
public void setUp() {
Persistence.OVERLAY_SUPPORT_ENABLED = true;
// Persistence.OVERLAY_SUPPORT_ENABLED = false;
int mutationPerDoc = 3;
System.out.println(
"PERF: Testing with overlay support: " + Persistence.OVERLAY_SUPPORT_ENABLED);
System.out.println("PERF: Testing with mutation per doc: " + mutationPerDoc * 3);
Faker faker = new Faker(new Locale("zh-CN"));
RandomService randomService = new RandomService();
db = testFirestore();
for (int i = 0; i < 100; ++i) {
String coll = randomService.hex(20);
docs.put(coll, new ArrayList<>());
for (int j = 0; j < 100; ++j) {
Object doc =
map(
"name", faker.pokemon().name(),
"location", faker.pokemon().location(),
"bool", randomService.nextBoolean(),
"double", randomService.nextDouble(),
"color", faker.color().name());
DocumentReference ref = waitFor(db.collection(coll).add(doc));
docs.get(coll).add(ref);
}
}

waitFor(db.disableNetwork());

long milli = System.currentTimeMillis();
for (Map.Entry<String, List<DocumentReference>> entry : docs.entrySet()) {
for (DocumentReference ref : entry.getValue()) {
Object doc =
map(
"name", faker.pokemon().name(),
"location", faker.pokemon().location(),
"bool", randomService.nextBoolean(),
"double", randomService.nextDouble(),
"color", faker.color().name());
ref.set(doc);
for (int i = 0; i < mutationPerDoc; i++) {
ref.update(map("bool", faker.app().version()));
ref.update(map("double", increment(randomService.nextInt(-10, 10))));
ref.update(map("color", arrayUnion(faker.color().name())));
}
}
}

waitFor(db.getAsyncQueue().enqueue(() -> {}));

long durationInMilli = System.currentTimeMillis() - milli;

System.out.println("PERF: Setup mutation takes " + durationInMilli + " milliseconds");
}

@Test
public void testQueries() {
Faker faker = new Faker(new Locale("zh-CN"));
RandomService randomService = new RandomService();
long milli = System.currentTimeMillis();
for (Map.Entry<String, List<DocumentReference>> entry : docs.entrySet()) {
CollectionReference coll = db.collection(entry.getKey());
QuerySnapshot snap =
waitFor(coll.whereEqualTo("name", faker.pokemon().name()).get(Source.CACHE));
snap = waitFor(coll.whereLessThan("double", randomService.nextDouble()).get(Source.CACHE));
snap = waitFor(coll.whereArrayContains("color", faker.color().name()).get(Source.CACHE));
// System.out.println("PERF: Collection query take " + (System.currentTimeMillis() - milli) +
// " milliseconds");
}
long durationInMilli = System.currentTimeMillis() - milli;
System.out.println("PERF: Queries take " + durationInMilli + " milliseconds");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.firestore.auth.User;
import com.google.firebase.firestore.core.DatabaseInfo;
import com.google.firebase.firestore.local.Persistence;
import com.google.firebase.firestore.model.DatabaseId;
import com.google.firebase.firestore.testutil.provider.FirestoreProvider;
import com.google.firebase.firestore.util.AsyncQueue;
Expand Down Expand Up @@ -97,7 +96,7 @@ public class IntegrationTestUtil {
private static final Map<FirebaseFirestore, Boolean> firestoreStatus = new HashMap<>();

/** Default amount of time to wait for a given operation to complete, used by waitFor() helper. */
private static final long OPERATION_WAIT_TIMEOUT_MS = 30000;
private static final long OPERATION_WAIT_TIMEOUT_MS = 300000;

/**
* Firestore databases can be subject to a ~30s "cold start" delay if they have not been used
Expand Down Expand Up @@ -256,8 +255,8 @@ public static FirebaseFirestore testFirestore(
Logger.setLogLevel(logLevel);

// TODO(Overlay): Remove below once this is ready to ship.
Persistence.OVERLAY_SUPPORT_ENABLED = true;
Persistence.INDEXING_SUPPORT_ENABLED = true;
// Persistence.OVERLAY_SUPPORT_ENABLED = true;
// Persistence.INDEXING_SUPPORT_ENABLED = true;

Context context = ApplicationProvider.getApplicationContext();
DatabaseId databaseId = DatabaseId.forDatabase(projectId, DatabaseId.DEFAULT_DATABASE_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ public Task<Void> waitForPendingWrites() {
}

@VisibleForTesting
AsyncQueue getAsyncQueue() {
public AsyncQueue getAsyncQueue() {
return asyncQueue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ public Task<Void> configureFieldIndexes(List<FieldIndex> fieldIndices) {
return asyncQueue.enqueue(() -> localStore.configureFieldIndexes(fieldIndices));
}

public AsyncQueue getAsyncQueue() {
return asyncQueue;
}

public void removeSnapshotsInSyncListener(EventListener<Void> listener) {
// Checks for shutdown but does not raise error, allowing remove after shutdown to be a no-op.
if (isTerminated()) {
Expand Down