|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore.performance; |
| 16 | + |
| 17 | +import static com.google.firebase.firestore.FieldValue.arrayUnion; |
| 18 | +import static com.google.firebase.firestore.FieldValue.increment; |
| 19 | +import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore; |
| 20 | +import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor; |
| 21 | +import static com.google.firebase.firestore.testutil.TestUtil.map; |
| 22 | + |
| 23 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 24 | +import com.github.javafaker.Faker; |
| 25 | +import com.github.javafaker.service.RandomService; |
| 26 | +import com.google.firebase.firestore.CollectionReference; |
| 27 | +import com.google.firebase.firestore.DocumentReference; |
| 28 | +import com.google.firebase.firestore.FirebaseFirestore; |
| 29 | +import com.google.firebase.firestore.QuerySnapshot; |
| 30 | +import com.google.firebase.firestore.Source; |
| 31 | +import com.google.firebase.firestore.local.Persistence; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.Map; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | + |
| 41 | +// TODO: Add the skipped tests from typescript. |
| 42 | +@RunWith(AndroidJUnit4.class) |
| 43 | +public class PerformanceTest { |
| 44 | + Map<String, List<DocumentReference>> docs = new HashMap<>(); |
| 45 | + FirebaseFirestore db = null; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setUp() { |
| 49 | + Persistence.OVERLAY_SUPPORT_ENABLED = true; |
| 50 | + // Persistence.OVERLAY_SUPPORT_ENABLED = false; |
| 51 | + int mutationPerDoc = 3; |
| 52 | + System.out.println( |
| 53 | + "PERF: Testing with overlay support: " + Persistence.OVERLAY_SUPPORT_ENABLED); |
| 54 | + System.out.println("PERF: Testing with mutation per doc: " + mutationPerDoc * 3); |
| 55 | + Faker faker = new Faker(new Locale("zh-CN")); |
| 56 | + RandomService randomService = new RandomService(); |
| 57 | + db = testFirestore(); |
| 58 | + for (int i = 0; i < 100; ++i) { |
| 59 | + String coll = randomService.hex(20); |
| 60 | + docs.put(coll, new ArrayList<>()); |
| 61 | + for (int j = 0; j < 100; ++j) { |
| 62 | + Object doc = |
| 63 | + map( |
| 64 | + "name", faker.pokemon().name(), |
| 65 | + "location", faker.pokemon().location(), |
| 66 | + "bool", randomService.nextBoolean(), |
| 67 | + "double", randomService.nextDouble(), |
| 68 | + "color", faker.color().name()); |
| 69 | + DocumentReference ref = waitFor(db.collection(coll).add(doc)); |
| 70 | + docs.get(coll).add(ref); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + waitFor(db.disableNetwork()); |
| 75 | + |
| 76 | + long milli = System.currentTimeMillis(); |
| 77 | + for (Map.Entry<String, List<DocumentReference>> entry : docs.entrySet()) { |
| 78 | + for (DocumentReference ref : entry.getValue()) { |
| 79 | + Object doc = |
| 80 | + map( |
| 81 | + "name", faker.pokemon().name(), |
| 82 | + "location", faker.pokemon().location(), |
| 83 | + "bool", randomService.nextBoolean(), |
| 84 | + "double", randomService.nextDouble(), |
| 85 | + "color", faker.color().name()); |
| 86 | + ref.set(doc); |
| 87 | + for (int i = 0; i < mutationPerDoc; i++) { |
| 88 | + ref.update(map("bool", faker.app().version())); |
| 89 | + ref.update(map("double", increment(randomService.nextInt(-10, 10)))); |
| 90 | + ref.update(map("color", arrayUnion(faker.color().name()))); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + waitFor(db.getAsyncQueue().enqueue(() -> {})); |
| 96 | + |
| 97 | + long durationInMilli = System.currentTimeMillis() - milli; |
| 98 | + |
| 99 | + System.out.println("PERF: Setup mutation takes " + durationInMilli + " milliseconds"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testQueries() { |
| 104 | + Faker faker = new Faker(new Locale("zh-CN")); |
| 105 | + RandomService randomService = new RandomService(); |
| 106 | + long milli = System.currentTimeMillis(); |
| 107 | + for (Map.Entry<String, List<DocumentReference>> entry : docs.entrySet()) { |
| 108 | + CollectionReference coll = db.collection(entry.getKey()); |
| 109 | + QuerySnapshot snap = |
| 110 | + waitFor(coll.whereEqualTo("name", faker.pokemon().name()).get(Source.CACHE)); |
| 111 | + snap = waitFor(coll.whereLessThan("double", randomService.nextDouble()).get(Source.CACHE)); |
| 112 | + snap = waitFor(coll.whereArrayContains("color", faker.color().name()).get(Source.CACHE)); |
| 113 | + // System.out.println("PERF: Collection query take " + (System.currentTimeMillis() - milli) + |
| 114 | + // " milliseconds"); |
| 115 | + } |
| 116 | + long durationInMilli = System.currentTimeMillis() - milli; |
| 117 | + System.out.println("PERF: Queries take " + durationInMilli + " milliseconds"); |
| 118 | + } |
| 119 | +} |
0 commit comments