Repository navigation
Expand file tree
/
Copy pathPerfTests.cc
More file actions
277 lines (236 loc) · 8.19 KB
/
Copy pathPerfTests.cc
File metadata and controls
277 lines (236 loc) · 8.19 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// PerfTests.cc
//
// Copyright 2015-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
//
#include "FleeceTests.hh"
#include "FleeceImpl.hh"
#include "JSONConverter.hh"
#include "Doc.hh"
#include "varint.hh"
#include <chrono>
#include <stdlib.h>
#include <thread>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include "betterassert.hh"
#pragma clang diagnostic ignored "-Wmissing-noreturn"
// Catch's REQUIRE is too slow for perf testing
#undef REQUIRE
#define REQUIRE(TEST) do{ if (!(TEST)) abort();} while (false)
#undef CHECK
#define CHECK(TEST) do{ if (!(TEST)) abort();} while (false)
using namespace fleece;
using namespace fleece::impl;
using namespace fleece_test;
#if !FL_EMBEDDED
TEST_CASE("GetUVarint performance", "[.Perf]") {
assert(false); // This test should not be run with a debug build!
static constexpr int kNRounds = 10000000;
Benchmark bench;
uint8_t buf[100];
fprintf(stderr, "buf = %p\n", &buf);
double d = 1.0;
double max = double(UINT64_MAX);
while (d <= max) {
auto n = (uint64_t)d;
size_t nBytes = PutUVarInt(buf, n);
uint64_t result = 0;
bench.start();
for (int round = 0; round < kNRounds; ++round) {
uint64_t nn;
CHECK(GetUVarInt(slice(buf, sizeof(buf)), &nn) == nBytes);
result += nn;
}
bench.stop();
CHECK(result != 1); // bogus
fprintf(stderr, "n = %16llx; %2zu bytes; time = %.3f ns\n",
(unsigned long long)n, nBytes,
bench.elapsed() / kNRounds * 1.0e9);
d *= 1.5;
}
bench.printReport(1.0/kNRounds);
}
TEST_CASE("Perf Convert1000People", "[.Perf]") {
assert(false); // This test should not be run with a debug build!
static const int kSamples = 500;
std::vector<double> elapsedTimes;
auto input = readTestFile(kBigJSONTestFileName);
Benchmark bench;
alloc_slice lastResult;
fprintf(stderr, "Converting JSON to Fleece...\n");
for (int i = 0; i < kSamples; i++) {
bench.start();
{
Encoder e(input.size);
e.uniqueStrings(true);
JSONConverter jr(e);
jr.encodeJSON(input);
e.end();
auto result = e.finish();
if (i == kSamples-1)
lastResult = result;
}
bench.stop();
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
bench.printReport();
fprintf(stderr, "\nJSON size: %zu bytes; Fleece size: %zu bytes (%.2f%%)\n",
input.size, lastResult.size, (lastResult.size*100.0/input.size));
writeToFile(lastResult, kTestFilesDir "1000people.fleece");
}
TEST_CASE("Perf LoadFleece", "[.Perf]") {
assert(false); // This test should not be run with a debug build!
static const int kIterations = 1000;
auto doc = readTestFile("1000people.fleece");
{
fprintf(stderr, "Scanning untrusted Fleece... ");
Benchmark bench;
for (int i = 0; i < kIterations; i++) {
bench.start();
[[maybe_unused]] auto root = Value::fromData(doc)->asArray();
REQUIRE(root != nullptr);
bench.stop();
}
bench.printReport();
}
{
fprintf(stderr, "Scanning trusted Fleece... ");
static const int kIterationsPerSample = 1000000;
Benchmark bench;
for (int i = 0; i < kIterations; i++) {
bench.start();
for (int j = 0; j < kIterationsPerSample; j++) {
[[maybe_unused]] auto root = Value::fromTrustedData(doc)->asArray();
REQUIRE(root != nullptr);
}
bench.stop();
}
bench.printReport(1.0/kIterationsPerSample);
}
}
static void testFindPersonByIndex(int sort) {
assert(false); // This test should not be run with a debug build!
int kSamples = 500;
int kIterations = 10000;
Benchmark bench;
auto doc = readTestFile("1000people.fleece");
Dict::key nameKey(slice("name"));
fprintf(stderr, "Looking up one value, sorted=%d...\n", sort);
for (int i = 0; i < kSamples; i++) {
bench.start();
for (int j = 0; j < kIterations; j++) {
auto root = Value::fromTrustedData(doc)->asArray();
auto person = root->get(123)->asDict();
const Value *name;
switch (sort) {
case 1: name = person->get(slice("name")); break;
case 2: name = person->get(nameKey); break;
default: return;
}
std::string nameStr = (std::string)name->asString();
#ifndef NDEBUG
REQUIRE(nameStr == std::string("Concepcion Burns"));
#endif
}
bench.stop();
}
bench.printReport(1.0/kIterations);
}
TEST_CASE("Perf FindPersonByIndexSorted", "[.Perf]") {testFindPersonByIndex(1);}
TEST_CASE("Perf FindPersonByIndexKeyed", "[.Perf]") {testFindPersonByIndex(2);}
TEST_CASE("Perf LoadPeople", "[.Perf]") {
assert(false); // This test should not be run with a debug build!
for (int shareKeys = 0; shareKeys <= 1; ++shareKeys) {
int kSamples = 50;
int kIterations = 1000;
Benchmark bench;
auto data = readTestFile("1000people.fleece");
auto sk = retained(new SharedKeys);
if (shareKeys) {
Encoder enc;
enc.setSharedKeys(sk);
enc.writeValue(Value::fromTrustedData(data));
data = enc.finish();
}
Dict::key keys[10] = {
{"about"_sl},
{"age"_sl},
{"balance"_sl},
{"guid"_sl},
{"isActive"_sl},
{"latitude"_sl},
{"longitude"_sl},
{"name"_sl},
{"registered"_sl},
{"tags"_sl},
};
fprintf(stderr, "Looking up 1000 people (with%s shared keys)...\n", (shareKeys ? "" : "out"));
for (int i = 0; i < kSamples; i++) {
auto doc = retained(new Doc(data, Doc::kTrusted, sk));
bench.start();
for (int j = 0; j < kIterations; j++) {
auto root = doc->root()->asArray();
for (Array::iterator iter(root); iter; ++iter) {
const Dict *person = iter->asDict();
size_t n = 0;
for (int k = 0; k < 10; k++)
if (person->get(keys[k]) != nullptr)
n++;
REQUIRE(n == 10);
}
}
bench.stop();
}
bench.printReport(1.0/kIterations, "person");
}
}
TEST_CASE("Perf DictSearch", "[.Perf]") {
assert(false); // This test should not be run with a debug build!
static const int kSamples = 500000;
// Convert JSON array into a dictionary keyed by _id:
alloc_slice input = readTestFile("1000people.fleece");
if (!input)
abort();
std::vector<alloc_slice> names;
unsigned nPeople = 0;
Encoder enc;
enc.beginDictionary();
for (Array::iterator i(Value::fromTrustedData(input)->asArray()); i; ++i) {
auto person = i.value()->asDict();
auto key = person->get("guid"_sl)->asString();
enc.writeKey(key);
enc.writeValue(person);
names.emplace_back(key);
if (++nPeople >= 1000)
break;
}
enc.endDictionary();
alloc_slice dictData = enc.finish();
auto people = Value::fromTrustedData(dictData)->asDict();
Benchmark bench;
for (int i = 0; i < kSamples; i++) {
slice keys[100];
for (int k = 0; k < 100; k++)
keys[k] = names[ random() % names.size() ];
bench.start();
{
for (int k = 0; k < 100; k++) {
const Value *person = people->get(keys[k]);
if (!person)
abort();
}
}
bench.stop();
//std::this_thread::sleep_for(std::chrono::microseconds(100));
}
bench.printReport();
}
#endif // !FL_EMBEDDED