File: visitedlink_perftest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (274 lines) | stat: -rw-r--r-- 9,491 bytes parent folder | download | duplicates (6)
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
// Copyright 2010 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_file_util.h"
#include "base/timer/elapsed_timer.h"
#include "build/build_config.h"
#include "components/visitedlink/browser/visitedlink_writer.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_result_reporter.h"
#include "url/gurl.h"


namespace visitedlink {

namespace {

static constexpr char kMetricAddAndQueryMs[] = "add_and_query";
static constexpr char kMetricTableInitMs[] = "table_initialization";
static constexpr char kMetricLinkInitMs[] = "link_init";
static constexpr char kMetricDatabaseFlushMs[] = "database_flush";
static constexpr char kMetricColdLoadTimeMs[] = "cold_load_time";
static constexpr char kMetricHotLoadTimeMs[] = "hot_load_time";
static constexpr char kMetricAddURLTimeMs[] = "add_url_time";
static constexpr char kMetricAddURLsTimeMs[] = "add_urls_time";

perf_test::PerfResultReporter SetUpReporter(const std::string& metric_suffix) {
  perf_test::PerfResultReporter reporter("VisitedLink.", metric_suffix);
  reporter.RegisterImportantMetric(kMetricAddAndQueryMs, "ms");
  reporter.RegisterImportantMetric(kMetricTableInitMs, "ms");
  reporter.RegisterImportantMetric(kMetricLinkInitMs, "ms");
  reporter.RegisterImportantMetric(kMetricDatabaseFlushMs, "ms");
  reporter.RegisterImportantMetric(kMetricColdLoadTimeMs, "ms");
  reporter.RegisterImportantMetric(kMetricHotLoadTimeMs, "ms");
  reporter.RegisterImportantMetric(kMetricAddURLTimeMs, "ms");
  reporter.RegisterImportantMetric(kMetricAddURLsTimeMs, "ms");
  return reporter;
}

// Designed like base/test/perf_time_logger but uses testing/perf instead of
// base/test/perf* to report timings.
class TimeLogger {
 public:
  explicit TimeLogger(std::string metric_suffix);

  TimeLogger(const TimeLogger&) = delete;
  TimeLogger& operator=(const TimeLogger&) = delete;

  ~TimeLogger();
  void Done();

 private:
  bool logged_;
  std::string metric_suffix_;
  base::ElapsedTimer timer_;
};

TimeLogger::TimeLogger(std::string metric_suffix)
    : logged_(false), metric_suffix_(std::move(metric_suffix)) {}

TimeLogger::~TimeLogger() {
  if (!logged_)
    Done();
}

void TimeLogger::Done() {
  // We use a floating-point millisecond value because it is more
  // intuitive than microseconds and we want more precision than
  // integer milliseconds.
  perf_test::PerfResultReporter reporter = SetUpReporter("baseline_story");
  reporter.AddResult(metric_suffix_, timer_.Elapsed().InMillisecondsF());
  logged_ = true;
}

// how we generate URLs, note that the two strings should be the same length
const int kAddCount = 10000;
const int kLoadTestInitialCount = 250000;
const char kAddedPrefix[] =
    "http://www.google.com/stuff/something/"
    "foo?session=85025602345625&id=1345142319023&seq=";
const char kUnaddedPrefix[] =
    "http://www.google.org/stuff/something/"
    "foo?session=39586739476365&id=2347624314402&seq=";

// Returns a URL with the given prefix and index
GURL TestURL(const char* prefix, int i) {
  return GURL(base::StringPrintf("%s%d", prefix, i));
}

// We have no readers, so all methods on this listener are a no-ops.
class DummyVisitedLinkEventListener : public VisitedLinkWriter::Listener {
 public:
  DummyVisitedLinkEventListener() = default;
  void NewTable(base::ReadOnlySharedMemoryRegion*) override {}
  void Add(VisitedLinkCommon::Fingerprint) override {}
  void Reset(bool invalidate_hashes) override {}
};

// this checks IsVisited for the URLs starting with the given prefix and
// within the given range
void CheckVisited(VisitedLinkWriter& writer,
                  const char* prefix,
                  int begin,
                  int end) {
  for (int i = begin; i < end; i++)
    writer.IsVisited(TestURL(prefix, i));
}

// Fills that writer's table with URLs starting with the given prefix and
// within the given range
void FillTable(VisitedLinkWriter& writer,
               const char* prefix,
               int begin,
               int end,
               int batch_size = 1) {
  if (batch_size > 1) {
    std::vector<GURL> urls;
    urls.reserve(batch_size);
    for (int i = begin; i < end; i += batch_size) {
      for (int j = i; j < end && j < i + batch_size; j++)
        urls.push_back(TestURL(prefix, j));
      writer.AddURLs(urls);
      urls.clear();
    }
  } else {
    for (int i = begin; i < end; i++)
      writer.AddURL(TestURL(prefix, i));
  }
}

class VisitedLinkPerfTest : public testing::Test {
 protected:
  base::FilePath db_path_;
  void SetUp() override { ASSERT_TRUE(base::CreateTemporaryFile(&db_path_)); }
  void TearDown() override { base::DeleteFile(db_path_); }

 private:
  content::BrowserTaskEnvironment task_environment_;
};

} // namespace

// This test tests adding many things to a database, and how long it takes
// to query the database with different numbers of things in it. The time
// is the total time to do all the operations, and as such, it is only
// useful for a regression test. If there is a regression, it might be
// useful to make another set of tests to test these things in isolation.
TEST_F(VisitedLinkPerfTest, TestAddAndQuery) {
  // init
  VisitedLinkWriter writer(new DummyVisitedLinkEventListener(), nullptr, true,
                           true, db_path_, 0);
  ASSERT_TRUE(writer.Init());
  content::RunAllTasksUntilIdle();

  TimeLogger timer(kMetricAddAndQueryMs);

  // first check without anything in the table
  CheckVisited(writer, kAddedPrefix, 0, kAddCount);

  // now fill half the table
  const int half_size = kAddCount / 2;
  FillTable(writer, kAddedPrefix, 0, half_size);

  // check the table again, half of these URLs will be visited, the other half
  // will not
  CheckVisited(writer, kAddedPrefix, 0, kAddCount);

  // fill the rest of the table
  FillTable(writer, kAddedPrefix, half_size, kAddCount);

  // check URLs, doing half visited, half unvisited
  CheckVisited(writer, kAddedPrefix, 0, kAddCount);
  CheckVisited(writer, kUnaddedPrefix, 0, kAddCount);
}

// Tests how long it takes to write and read a large database to and from disk.
// TODO(crbug.com/40719465): Fix flakiness on macOS and Android.
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID)
#define MAYBE_TestBigTable DISABLED_TestBigTable
#else
#define MAYBE_TestBigTable TestBigTable
#endif
TEST_F(VisitedLinkPerfTest, MAYBE_TestBigTable) {
  base::test::ScopedDisableRunLoopTimeout disable_run_timeout;
  // create a big DB
  {
    TimeLogger table_initialization_timer(kMetricTableInitMs);

    auto writer = std::make_unique<VisitedLinkWriter>(
        new DummyVisitedLinkEventListener(), nullptr, true, true, db_path_, 0);

    // time init with empty table
    TimeLogger initTimer(kMetricLinkInitMs);
    bool success = writer->Init();
    content::RunAllTasksUntilIdle();
    initTimer.Done();
    ASSERT_TRUE(success);

    // add a bunch of stuff
    // TODO(maruel): This is very inefficient because the file gets rewritten
    // many time and this is the actual bottleneck of this test. The file should
    // only get written that the end of the FillTable call, not 4169(!) times.
    FillTable(*writer, kAddedPrefix, 0, kLoadTestInitialCount);
    content::RunAllTasksUntilIdle();

    // time writing the file out out
    TimeLogger flushTimer(kMetricDatabaseFlushMs);
    writer->RewriteFile();
    writer.reset();  // Will post a task to fclose() the file and thus flush it.
    content::RunAllTasksUntilIdle();
    flushTimer.Done();

    table_initialization_timer.Done();
  }

  // test loading the DB back.
  // make sure the file has to be re-loaded
  base::EvictFileFromSystemCache(db_path_);

  // cold load (no OS cache, hopefully)
  {
    TimeLogger cold_load_timer(kMetricColdLoadTimeMs);

    VisitedLinkWriter writer(new DummyVisitedLinkEventListener(), nullptr, true,
                             true, db_path_, 0);
    bool success = writer.Init();
    content::RunAllTasksUntilIdle();

    cold_load_timer.Done();
    ASSERT_TRUE(success);
  }

  // hot load (with OS caching the file in memory)
  TimeLogger hot_load_timer(kMetricHotLoadTimeMs);

  VisitedLinkWriter writer(new DummyVisitedLinkEventListener(), nullptr, true,
                           true, db_path_, 0);
  bool success = writer.Init();
  content::RunAllTasksUntilIdle();

  hot_load_timer.Done();
  ASSERT_TRUE(success);

  // Add some more URLs one-by-one.
  TimeLogger add_url_timer(kMetricAddURLTimeMs);
  FillTable(writer, kAddedPrefix, writer.GetUsedCount(),
            writer.GetUsedCount() + kAddCount);
  content::RunAllTasksUntilIdle();
  add_url_timer.Done();

  TimeLogger add_urls_timer(kMetricAddURLsTimeMs);
  // Add some more URLs in groups of 2.
  int batch_size = 2;
  FillTable(writer, kAddedPrefix, writer.GetUsedCount(),
            writer.GetUsedCount() + kAddCount, batch_size);
  // Add some more URLs in a big batch.
  batch_size = kAddCount;
  FillTable(writer, kAddedPrefix, writer.GetUsedCount(),
            writer.GetUsedCount() + kAddCount, batch_size);
  content::RunAllTasksUntilIdle();
  add_urls_timer.Done();
}

}  // namespace visitedlink