File: persistent_cache_perftest.cc

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (284 lines) | stat: -rw-r--r-- 10,017 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/persistent_cache/persistent_cache.h"

#include <algorithm>

#include "base/auto_reset.h"
#include "base/containers/heap_array.h"
#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/function_ref.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/gmock_expected_support.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "build/buildflag.h"
#include "components/persistent_cache/backend_storage.h"
#include "components/persistent_cache/backend_type.h"
#include "components/persistent_cache/pending_backend.h"
#include "components/persistent_cache/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_result_reporter.h"

namespace persistent_cache {

class PersistentCachePerftest : public testing::Test {
 protected:
  static constexpr base::FilePath::StringViewType kBaseName =
      FILE_PATH_LITERAL("perftest");

  void SetUp() override {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    backend_storage_.emplace(BackendType::kSqlite, temp_dir_.GetPath());
  }

  std::unique_ptr<PersistentCache> CreateCache(bool single_connection = false,
                                               bool journal_mode_wal = false) {
    if (auto pending_backend = backend_storage_->MakePendingBackend(
            base::FilePath(kBaseName), single_connection, journal_mode_wal);
        pending_backend.has_value()) {
      return PersistentCache::Bind(*std::move(pending_backend));
    }
    ADD_FAILURE() << "Failed to make PendingBackend";
    return nullptr;
  }

  std::optional<PendingBackend> ShareReadWriteConnection(
      PersistentCache& cache) {
    return backend_storage_->ShareReadWriteConnection(base::FilePath(kBaseName),
                                                      cache);
  }

  void RunAndTimeTest(std::string operation_name,
                      int iteration_count,
                      base::FunctionRef<void()> test_body) {
    base::AutoReset<bool> resetter(&under_measurment_, true);
    base::ElapsedTimer elapsed_timer;
    base::ElapsedThreadTimer elapsed_thread_timer;

    test_body();

    ReportMeasurment(operation_name, iteration_count, elapsed_timer.Elapsed(),
                     elapsed_thread_timer.Elapsed());
  }

  // Pregenerates keys. Use to avoid timing allocation overhead.
  std::vector<std::string> GenerateKeys(int iteration_count) {
    CHECK(!under_measurment_);

    std::vector<std::string> keys(iteration_count);
    std::generate(keys.begin(), keys.end(),
                  [i = 0]() mutable { return base::NumberToString(i++); });
    return keys;
  }

  // Generates a value buffer to be inserted according to params. Should be done
  // outside of timing to avoid measuring overhead.
  base::HeapArray<uint8_t> MakeValue() {
    CHECK(!under_measurment_);

    // Median size of entries for a use case of PersistentCache as reported by
    // UMA on November 7th 2025.
    static constexpr size_t kValueSize = 6958;
    auto value = base::HeapArray<uint8_t>::Uninit(kValueSize);

    // Fill the data with random bytes to avoid unknown optimizations for
    // identical pages.
    base::RandBytes(value);
    return value;
  }

 private:
  void ReportMeasurment(std::string operation_name,
                        int iteration_count,
                        base::TimeDelta elapsed_time,
                        base::TimeDelta elapsed_thread_time) {
    const std::string reporter_name("PersistentCache");
    perf_test::PerfResultReporter reporter(reporter_name, operation_name);
    reporter.RegisterImportantMetric(".wall_time", "us");
    reporter.AddResult(
        ".wall_time",
        static_cast<size_t>(elapsed_time.InMicroseconds() / iteration_count));
    reporter.RegisterImportantMetric(".thread_time", "us");
    reporter.AddResult(
        ".thread_time",
        static_cast<size_t>(elapsed_thread_time.InMicroseconds() /
                            iteration_count));
  }

  base::ScopedTempDir temp_dir_;
  std::optional<BackendStorage> backend_storage_;
  bool under_measurment_ = false;
};

// Only compile and run these tests on configurations that are monitored.
#if (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)) && \
    !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER)

TEST_F(PersistentCachePerftest, OpenClose) {
  auto persistent_cache = CreateCache();

  static constexpr int kIterationCount = 1024;

  int success_count = 0;
  RunAndTimeTest("OpenClose", kIterationCount, [&] {
    for (size_t i = 0; i < kIterationCount; ++i) {
      auto persistent_cache_under_test =
          PersistentCache::Bind(*ShareReadWriteConnection(*persistent_cache));
      if (persistent_cache_under_test) {
        ++success_count;
      }
    }
  });

  ASSERT_EQ(success_count, kIterationCount);
}

TEST_F(PersistentCachePerftest, Insert) {
  auto persistent_cache = CreateCache();

  static constexpr int kIterationCount = 1024;
  std::vector<std::string> keys = GenerateKeys(kIterationCount);
  base::HeapArray<uint8_t> value = MakeValue();

  int success_count = 0;
  RunAndTimeTest("Insert", kIterationCount, [&] {
    success_count = std::ranges::count_if(
        keys, [&cache = *persistent_cache, &value](const auto& key) {
          return cache.Insert(key, value.as_span()).has_value();
        });
  });
  ASSERT_EQ(success_count, kIterationCount);
}

TEST_F(PersistentCachePerftest, InsertSingleConnection) {
  auto persistent_cache = CreateCache(/*single_connection=*/true);

  static constexpr int kIterationCount = 1024;
  std::vector<std::string> keys = GenerateKeys(kIterationCount);
  base::HeapArray<uint8_t> value = MakeValue();

  int success_count = 0;
  RunAndTimeTest("InsertSingleConnection", kIterationCount, [&] {
    success_count = std::ranges::count_if(
        keys, [&cache = *persistent_cache, &value](const auto& key) {
          return cache.Insert(key, value.as_span()).has_value();
        });
  });
  ASSERT_EQ(success_count, kIterationCount);
}

TEST_F(PersistentCachePerftest, InsertWal) {
  auto persistent_cache =
      CreateCache(/*single_connection=*/true, /*journal_mode_wal=*/true);

  static constexpr int kIterationCount = 1024;
  std::vector<std::string> keys = GenerateKeys(kIterationCount);
  base::HeapArray<uint8_t> value = MakeValue();

  int success_count = 0;
  RunAndTimeTest("InsertWal", kIterationCount, [&] {
    success_count = std::ranges::count_if(
        keys, [&cache = *persistent_cache, &value](const auto& key) {
          return cache.Insert(key, value.as_span()).has_value();
        });
  });
  ASSERT_EQ(success_count, kIterationCount);
}

TEST_F(PersistentCachePerftest, Find) {
  auto persistent_cache = CreateCache();

  static constexpr int kIterationCount = 1024;
  std::vector<std::string> keys = GenerateKeys(kIterationCount);
  base::HeapArray<uint8_t> value = MakeValue();

  // Fill the cache.
  for (const std::string& key : keys) {
    ASSERT_THAT(persistent_cache->Insert(key, value.as_span()),
                base::test::HasValue());
  }

  // Shuffle the keys around to avoid taking advantage of file-system caching
  // behavior.
  base::RandomShuffle(keys.begin(), keys.end());

  int success_count = 0;
  RunAndTimeTest("Find", kIterationCount, [&] {
    success_count = std::ranges::count_if(keys, [&cache = *persistent_cache](
                                                    const auto& key) {
      return cache
          .Find(key, [](size_t content_size) { return base::span<uint8_t>(); })
          .has_value();
    });
  });
  ASSERT_EQ(success_count, kIterationCount);
}

TEST_F(PersistentCachePerftest, FindSingleConnection) {
  auto persistent_cache = CreateCache(/*single_connection=*/true);

  static constexpr int kIterationCount = 1024;
  std::vector<std::string> keys = GenerateKeys(kIterationCount);
  base::HeapArray<uint8_t> value = MakeValue();

  // Fill the cache.
  for (const std::string& key : keys) {
    ASSERT_THAT(persistent_cache->Insert(key, value.as_span()),
                base::test::HasValue());
  }

  // Shuffle the keys around to avoid taking advantage of file-system caching
  // behavior.
  base::RandomShuffle(keys.begin(), keys.end());

  int success_count = 0;
  RunAndTimeTest("FindSingleConnection", kIterationCount, [&] {
    success_count = std::ranges::count_if(keys, [&cache = *persistent_cache](
                                                    const auto& key) {
      return cache
          .Find(key, [](size_t content_size) { return base::span<uint8_t>(); })
          .has_value();
    });
  });
  ASSERT_EQ(success_count, kIterationCount);
}

TEST_F(PersistentCachePerftest, FindWal) {
  auto persistent_cache =
      CreateCache(/*single_connection=*/true, /*journal_mode_wal=*/true);

  static constexpr int kIterationCount = 1024;
  std::vector<std::string> keys = GenerateKeys(kIterationCount);
  base::HeapArray<uint8_t> value = MakeValue();

  // Fill the cache.
  for (const std::string& key : keys) {
    ASSERT_THAT(persistent_cache->Insert(key, value.as_span()),
                base::test::HasValue());
  }

  // Shuffle the keys around to avoid taking advantage of file-system caching
  // behavior.
  base::RandomShuffle(keys.begin(), keys.end());

  int success_count = 0;
  RunAndTimeTest("FindWal", kIterationCount, [&] {
    success_count = std::ranges::count_if(keys, [&cache = *persistent_cache](
                                                    const auto& key) {
      return cache
          .Find(key, [](size_t content_size) { return base::span<uint8_t>(); })
          .has_value();
    });
  });
  ASSERT_EQ(success_count, kIterationCount);
}

#endif  // (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)) &&
        // !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER)

}  // namespace persistent_cache