aboutsummaryrefslogtreecommitdiff
path: root/icing/file/portable-file-backed-proto-log_benchmark.cc
blob: 80a801115e0a0c6d9e0ca865c58688cd5e9373d5 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (C) 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.

#include <cstdint>
#include <random>

#include "testing/base/public/benchmark.h"
#include "gmock/gmock.h"
#include "icing/document-builder.h"
#include "icing/file/filesystem.h"
#include "icing/file/portable-file-backed-proto-log.h"
#include "icing/legacy/core/icing-string-util.h"
#include "icing/proto/document.pb.h"
#include "icing/testing/common-matchers.h"
#include "icing/testing/random-string.h"
#include "icing/testing/tmp-directory.h"

// go/microbenchmarks
//
// To build and run on a local machine:
//   $ blaze build -c opt --dynamic_mode=off --copt=-gmlt
//   icing/file:portable-file-backed-proto-log_benchmark
//
//   $ blaze-bin/icing/file/portable-file-backed-proto-log_benchmark
//   --benchmarks=all
//
//
// To build and run on an Android device (must be connected and rooted):
//   $ blaze build --copt="-DGOOGLE_COMMANDLINEFLAGS_FULL_API=1"
//   --config=android_arm64 -c opt --dynamic_mode=off --copt=-gmlt
//   icing/file:portable-file-backed-proto-log_benchmark
//
//   $ adb root
//
//   $ adb push
//   blaze-bin/icing/file/portable-file-backed-proto-log_benchmark
//   /data/local/tmp/
//
//   $ adb shell /data/local/tmp/portable-file-backed-proto-log-benchmark
//   --benchmarks=all

namespace icing {
namespace lib {

namespace {

void BM_Write(benchmark::State& state) {
  const Filesystem filesystem;
  int string_length = state.range(0);
  const std::string file_path = IcingStringUtil::StringPrintf(
      "%s%s%d%s", GetTestTempDir().c_str(), "/proto_", string_length, ".log");
  int max_proto_size = (1 << 24) - 1;  // 16 MiB
  bool compress = true;

  // Make sure it doesn't already exist.
  filesystem.DeleteFile(file_path.c_str());

  auto proto_log = PortableFileBackedProtoLog<DocumentProto>::Create(
                       &filesystem, file_path,
                       PortableFileBackedProtoLog<DocumentProto>::Options(
                           compress, max_proto_size))
                       .ValueOrDie()
                       .proto_log;

  DocumentProto document = DocumentBuilder().SetKey("namespace", "uri").Build();

  std::default_random_engine random;
  const std::string rand_str =
      RandomString(kAlNumAlphabet, string_length, &random);

  auto document_properties = document.add_properties();
  document_properties->set_name("string property");
  document_properties->add_string_values(rand_str);

  for (auto _ : state) {
    testing::DoNotOptimize(proto_log->WriteProto(document));
  }
  state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
                          string_length);

  // Cleanup after ourselves
  filesystem.DeleteFile(file_path.c_str());
}
BENCHMARK(BM_Write)
    ->Arg(1)
    ->Arg(32)
    ->Arg(512)
    ->Arg(1024)
    ->Arg(4 * 1024)
    ->Arg(8 * 1024)
    ->Arg(16 * 1024)
    ->Arg(32 * 1024)
    ->Arg(256 * 1024)
    ->Arg(2 * 1024 * 1024)
    ->Arg(8 * 1024 * 1024)
    ->Arg(15 * 1024 * 1024);  // We do 15MiB here since our max proto size is
                              // 16MiB, and we need some extra space for the
                              // rest of the document properties

void BM_Read(benchmark::State& state) {
  const Filesystem filesystem;
  int string_length = state.range(0);
  const std::string file_path = IcingStringUtil::StringPrintf(
      "%s%s%d%s", GetTestTempDir().c_str(), "/proto_", string_length, ".log");
  int max_proto_size = (1 << 24) - 1;  // 16 MiB
  bool compress = true;

  // Make sure it doesn't already exist.
  filesystem.DeleteFile(file_path.c_str());

  auto proto_log = PortableFileBackedProtoLog<DocumentProto>::Create(
                       &filesystem, file_path,
                       PortableFileBackedProtoLog<DocumentProto>::Options(
                           compress, max_proto_size))
                       .ValueOrDie()
                       .proto_log;

  DocumentProto document = DocumentBuilder().SetKey("namespace", "uri").Build();

  std::default_random_engine random;
  const std::string rand_str =
      RandomString(kAlNumAlphabet, string_length, &random);

  auto document_properties = document.add_properties();
  document_properties->set_name("string property");
  document_properties->add_string_values(rand_str);

  ICING_ASSERT_OK_AND_ASSIGN(int64_t write_offset,
                             proto_log->WriteProto(document));

  for (auto _ : state) {
    testing::DoNotOptimize(proto_log->ReadProto(write_offset));
  }
  state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
                          string_length);

  // Cleanup after ourselves
  filesystem.DeleteFile(file_path.c_str());
}
BENCHMARK(BM_Read)
    ->Arg(1)
    ->Arg(32)
    ->Arg(512)
    ->Arg(1024)
    ->Arg(4 * 1024)
    ->Arg(8 * 1024)
    ->Arg(16 * 1024)
    ->Arg(32 * 1024)
    ->Arg(256 * 1024)
    ->Arg(2 * 1024 * 1024)
    ->Arg(8 * 1024 * 1024)
    ->Arg(15 * 1024 * 1024);  // We do 15MiB here since our max proto size is
                              // 16MiB, and we need some extra space for the
                              // rest of the document properties
                              //
void BM_Erase(benchmark::State& state) {
  const Filesystem filesystem;
  const std::string file_path = IcingStringUtil::StringPrintf(
      "%s%s", GetTestTempDir().c_str(), "/proto.log");
  int max_proto_size = (1 << 24) - 1;  // 16 MiB
  bool compress = true;

  // Make sure it doesn't already exist.
  filesystem.DeleteFile(file_path.c_str());

  auto proto_log = PortableFileBackedProtoLog<DocumentProto>::Create(
                       &filesystem, file_path,
                       PortableFileBackedProtoLog<DocumentProto>::Options(
                           compress, max_proto_size))
                       .ValueOrDie()
                       .proto_log;

  DocumentProto document = DocumentBuilder().SetKey("namespace", "uri").Build();

  std::default_random_engine random;
  const std::string rand_str = RandomString(kAlNumAlphabet, /*len=*/1, &random);

  auto document_properties = document.add_properties();
  document_properties->set_name("string property");
  document_properties->add_string_values(rand_str);

  for (auto _ : state) {
    state.PauseTiming();
    ICING_ASSERT_OK_AND_ASSIGN(int64_t write_offset,
                               proto_log->WriteProto(document));
    state.ResumeTiming();

    testing::DoNotOptimize(proto_log->EraseProto(write_offset));
  }

  // Cleanup after ourselves
  filesystem.DeleteFile(file_path.c_str());
}
BENCHMARK(BM_Erase);

void BM_ComputeChecksum(benchmark::State& state) {
  const Filesystem filesystem;
  const std::string file_path = GetTestTempDir() + "/proto.log";
  int max_proto_size = (1 << 24) - 1;  // 16 MiB
  bool compress = true;

  // Make sure it doesn't already exist.
  filesystem.DeleteFile(file_path.c_str());

  auto proto_log = PortableFileBackedProtoLog<DocumentProto>::Create(
                       &filesystem, file_path,
                       PortableFileBackedProtoLog<DocumentProto>::Options(
                           compress, max_proto_size))
                       .ValueOrDie()
                       .proto_log;

  DocumentProto document = DocumentBuilder().SetKey("namespace", "uri").Build();

  // Make each document 1KiB
  int string_length = 1024;
  std::default_random_engine random;
  const std::string rand_str =
      RandomString(kAlNumAlphabet, string_length, &random);

  auto document_properties = document.add_properties();
  document_properties->set_name("string property");
  document_properties->add_string_values(rand_str);

  int num_docs = state.range(0);
  for (int i = 0; i < num_docs; ++i) {
    ICING_ASSERT_OK(proto_log->WriteProto(document));
  }

  for (auto _ : state) {
    testing::DoNotOptimize(proto_log->ComputeChecksum());
  }

  // Cleanup after ourselves
  filesystem.DeleteFile(file_path.c_str());
}
BENCHMARK(BM_ComputeChecksum)->Range(1024, 1 << 20);

void BM_ComputeChecksumWithCachedChecksum(benchmark::State& state) {
  const Filesystem filesystem;
  const std::string file_path = GetTestTempDir() + "/proto.log";
  int max_proto_size = (1 << 24) - 1;  // 16 MiB
  bool compress = true;

  // Make sure it doesn't already exist.
  filesystem.DeleteFile(file_path.c_str());

  auto proto_log = PortableFileBackedProtoLog<DocumentProto>::Create(
                       &filesystem, file_path,
                       PortableFileBackedProtoLog<DocumentProto>::Options(
                           compress, max_proto_size))
                       .ValueOrDie()
                       .proto_log;

  DocumentProto document = DocumentBuilder().SetKey("namespace", "uri").Build();

  // Make the document 1KiB
  int string_length = 1024;
  std::default_random_engine random;
  const std::string rand_str =
      RandomString(kAlNumAlphabet, string_length, &random);

  auto document_properties = document.add_properties();
  document_properties->set_name("string property");
  document_properties->add_string_values(rand_str);

  // Write some content and persist. This should update our cached checksum to
  // include the document.
  ICING_ASSERT_OK(proto_log->WriteProto(document));
  ICING_ASSERT_OK(proto_log->PersistToDisk());

  // This ComputeChecksum call shouldn't need to do any computation since we can
  // reuse our cached checksum.
  for (auto _ : state) {
    testing::DoNotOptimize(proto_log->ComputeChecksum());
  }

  // Cleanup after ourselves
  filesystem.DeleteFile(file_path.c_str());
}
BENCHMARK(BM_ComputeChecksumWithCachedChecksum);

void BM_ComputeChecksumOnlyForTail(benchmark::State& state) {
  const Filesystem filesystem;
  const std::string file_path = GetTestTempDir() + "/proto.log";
  int max_proto_size = (1 << 24) - 1;  // 16 MiB
  bool compress = true;

  // Make sure it doesn't already exist.
  filesystem.DeleteFile(file_path.c_str());

  auto proto_log = PortableFileBackedProtoLog<DocumentProto>::Create(
                       &filesystem, file_path,
                       PortableFileBackedProtoLog<DocumentProto>::Options(
                           compress, max_proto_size))
                       .ValueOrDie()
                       .proto_log;

  DocumentProto document = DocumentBuilder().SetKey("namespace", "uri").Build();

  // Make the document 1KiB
  int string_length = 1024;
  std::default_random_engine random;
  const std::string rand_str =
      RandomString(kAlNumAlphabet, string_length, &random);

  auto document_properties = document.add_properties();
  document_properties->set_name("string property");
  document_properties->add_string_values(rand_str);

  // Write some content and persist. This should update our cached checksum to
  // include the document.
  ICING_ASSERT_OK(proto_log->WriteProto(document));
  ICING_ASSERT_OK(proto_log->PersistToDisk());

  // Write another proto into the tail, but it's not included in our cached
  // checksum since we didn't call persist.
  ICING_ASSERT_OK(proto_log->WriteProto(document));

  // ComputeChecksum should be calculating the checksum of the tail and adding
  // it to the cached checksum we have.
  for (auto _ : state) {
    testing::DoNotOptimize(proto_log->ComputeChecksum());
  }

  // Cleanup after ourselves
  filesystem.DeleteFile(file_path.c_str());
}
BENCHMARK(BM_ComputeChecksumOnlyForTail);

}  // namespace
}  // namespace lib
}  // namespace icing