mirror of
https://github.com/firebase/firebase-ios-sdk
synced 2026-11-09 22:36:01 +05:45
* Use a valid project id This avoids tripping up over newly added validation in the emulator * Allow passing arguments to the emulator e.g. to pass a non-default host * Add a benchmark for reading documents Baseline results: Run on (12 X 2900 MHz CPU s) CPU Caches: L1 Data 32K (x6) L1 Instruction 32K (x6) L2 Unified 262K (x6) L3 Unified 12582K (x1) Load Average: 2.11, 1.96, 1.92 --------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------- BM_QueryMatching/0/1 168359 ns 13750 ns 53878 BM_QueryMatching/1/1 179118 ns 16706 ns 40193 BM_QueryMatching/1/10 721218 ns 35729 ns 10000 BM_QueryMatching/10/10 726533 ns 51864 ns 13587 BM_QueryMatching/1/100 5326040 ns 42820 ns 1000 BM_QueryMatching/100/100 5734101 ns 335289 ns 1000 BM_QueryMatching/1/1000 54110241 ns 41310 ns 100 BM_QueryMatching/10/1000 54584725 ns 95060 ns 100 BM_QueryMatching/100/1000 55083753 ns 527750 ns 100 BM_QueryMatching/1000/1000 58299823 ns 29130 ns 100 BM_QueryAll/1 108892 ns 13355 ns 51197 BM_QueryAll/10 657727 ns 43867 ns 10000 BM_QueryAll/100 5786091 ns 328244 ns 1000 BM_QueryAll/1000 60092767 ns 19380 ns 100 * Add a dedicated XCTest/Google Benchmark adapter Remove FSTLevelDBBenchmarkTests. This benchmark appears to explore some trade-off in document-size vs indexing but doesn't seem useful to re-run.
150 lines
4.9 KiB
Plaintext
150 lines
4.9 KiB
Plaintext
/*
|
|
* Copyright 2020 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.
|
|
*/
|
|
|
|
#import <FirebaseCore/FIRApp.h>
|
|
#import <FirebaseFirestore/FirebaseFirestore.h>
|
|
|
|
#include "Firestore/core/src/firebase/firestore/util/autoid.h"
|
|
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
|
|
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
|
|
#include "Firestore/core/test/firebase/firestore/testutil/app_testing.h"
|
|
#include "benchmark/benchmark.h"
|
|
|
|
namespace {
|
|
|
|
using firebase::firestore::testutil::AppForUnitTesting;
|
|
using firebase::firestore::util::CreateAutoId;
|
|
using firebase::firestore::util::MakeNSString;
|
|
using firebase::firestore::util::MakeString;
|
|
|
|
FIRFirestore* OpenFirestore() {
|
|
FIRApp* app = AppForUnitTesting();
|
|
auto db = [FIRFirestore firestoreForApp:app];
|
|
auto settings = db.settings;
|
|
|
|
// Default to running against the emulator because we're evaluating local execution speed.
|
|
settings.host = @"localhost:8080";
|
|
settings.sslEnabled = NO;
|
|
|
|
// The default is the main queue and this deadlocks because the benchmark does not start an event
|
|
// loop and just runs on the main thread.
|
|
settings.dispatchQueue = dispatch_queue_create("results", DISPATCH_QUEUE_SERIAL);
|
|
db.settings = settings;
|
|
|
|
return db;
|
|
}
|
|
|
|
NSMutableDictionary<NSString*, id>* MakeDocumentData() {
|
|
NSMutableDictionary<NSString*, id>* doc = [[NSMutableDictionary alloc] init];
|
|
NSString* value = MakeNSString(std::string('a', 100));
|
|
|
|
// Create keys "a", "b", "c", ..., "j", each associated with the 100 byte
|
|
// value. This makes the total document size ~1 kb.
|
|
std::string key_bytes("a");
|
|
for (int i = 0; i < 10; i++) {
|
|
key_bytes[0] = static_cast<char>('a' + i);
|
|
doc[MakeNSString(key_bytes)] = value;
|
|
}
|
|
return doc;
|
|
}
|
|
|
|
FIRQuerySnapshot* GetDocuments(FIRQuery* query) {
|
|
__block FIRQuerySnapshot* result;
|
|
dispatch_semaphore_t done = dispatch_semaphore_create(0);
|
|
[query getDocumentsWithSource:FIRFirestoreSourceCache
|
|
completion:^(FIRQuerySnapshot* snap, NSError* error) {
|
|
HARD_ASSERT(error == nil, "Failed: %s", MakeString([error description]));
|
|
result = snap;
|
|
dispatch_semaphore_signal(done);
|
|
}];
|
|
dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
|
|
return result;
|
|
}
|
|
|
|
void WaitForPendingWrites(FIRFirestore* db) {
|
|
dispatch_semaphore_t done = dispatch_semaphore_create(0);
|
|
[db waitForPendingWritesWithCompletion:^(NSError* error) {
|
|
dispatch_semaphore_signal(done);
|
|
}];
|
|
dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
|
|
}
|
|
|
|
void WriteDocs(FIRCollectionReference* collection, int64_t count, bool match) {
|
|
auto doc = MakeDocumentData();
|
|
for (int64_t i = 0; i < count; i++) {
|
|
doc[@"match"] = @(match);
|
|
FIRDocumentReference* ref = [collection documentWithAutoID];
|
|
[ref setData:doc];
|
|
}
|
|
WaitForPendingWrites(collection.firestore);
|
|
}
|
|
|
|
void Shutdown(FIRFirestore* db) {
|
|
dispatch_semaphore_t done = dispatch_semaphore_create(0);
|
|
[db terminateWithCompletion:^(NSError* error) {
|
|
dispatch_semaphore_signal(done);
|
|
}];
|
|
dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
|
|
}
|
|
|
|
void BM_QueryMatching(benchmark::State& state) {
|
|
int64_t matching_docs = state.range(0);
|
|
int64_t total_docs = state.range(1);
|
|
|
|
FIRFirestore* db = OpenFirestore();
|
|
auto collection = [db collectionWithPath:MakeNSString("docs-" + CreateAutoId())];
|
|
WriteDocs(collection, matching_docs, /* match= */ true);
|
|
WriteDocs(collection, total_docs - matching_docs, /* match= */ false);
|
|
|
|
for (auto _ : state) {
|
|
auto docs = GetDocuments([collection queryWhereField:@"match" isEqualTo:@YES]);
|
|
(void)docs;
|
|
}
|
|
|
|
Shutdown(db);
|
|
}
|
|
BENCHMARK(BM_QueryMatching)
|
|
->Unit(benchmark::kMicrosecond)
|
|
->Args({0, 1})
|
|
->Args({1, 1})
|
|
->Args({1, 10})
|
|
->Args({10, 10})
|
|
->Args({1, 100})
|
|
->Args({100, 100})
|
|
->Args({1, 1000})
|
|
->Args({10, 1000})
|
|
->Args({100, 1000})
|
|
->Args({1000, 1000});
|
|
|
|
void BM_QueryAll(benchmark::State& state) {
|
|
int64_t total_docs = state.range(0);
|
|
|
|
FIRFirestore* db = OpenFirestore();
|
|
auto collection = [db collectionWithPath:MakeNSString("docs-" + CreateAutoId())];
|
|
WriteDocs(collection, total_docs, /* match= */ true);
|
|
|
|
for (auto _ : state) {
|
|
auto docs = GetDocuments(collection);
|
|
(void)docs;
|
|
}
|
|
|
|
Shutdown(db);
|
|
}
|
|
|
|
BENCHMARK(BM_QueryAll)->Unit(benchmark::kMicrosecond)->Arg(1)->Arg(10)->Arg(100)->Arg(1000);
|
|
|
|
} // namespace
|