OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/omnibox/browser/history_quick_provider.h" | |
6 | |
7 #include <memory> | |
8 #include <random> | |
9 #include <string> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/run_loop.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "components/history/core/browser/history_backend.h" | |
15 #include "components/history/core/browser/history_database.h" | |
16 #include "components/history/core/browser/history_service.h" | |
17 #include "components/history/core/test/history_service_test_util.h" | |
18 #include "components/omnibox/browser/fake_autocomplete_provider_client.h" | |
19 #include "components/omnibox/browser/history_test_util.h" | |
20 #include "components/omnibox/browser/in_memory_url_index_test_util.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 #include "testing/perf/perf_test.h" | |
23 | |
24 namespace history { | |
25 | |
26 namespace { | |
27 | |
28 // Not threadsafe. | |
29 std::string GenerateFakeHashedString(size_t sym_count) { | |
30 static constexpr char kSyms[] = | |
31 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,/=+?#"; | |
32 CR_DEFINE_STATIC_LOCAL(std::mt19937, engine, ()); | |
33 std::uniform_int_distribution<size_t> index_distribution( | |
34 0, arraysize(kSyms) - 2 /* trailing \0 */); | |
35 | |
36 std::string res; | |
37 res.reserve(sym_count); | |
38 | |
39 std::generate_n(std::back_inserter(res), sym_count, [&index_distribution] { | |
40 return kSyms[index_distribution(engine)]; | |
41 }); | |
42 | |
43 return res; | |
44 } | |
45 | |
46 URLRow GeneratePopularURLRow() { | |
47 static constexpr char kPopularUrl[] = | |
48 "http://long.popular_url_with.many_variations/"; | |
49 | |
50 constexpr size_t kFakeHashLength = 10; | |
51 std::string fake_hash = GenerateFakeHashedString(kFakeHashLength); | |
52 | |
53 URLRow row{GURL(kPopularUrl + fake_hash)}; | |
54 EXPECT_TRUE(row.url().is_valid()); | |
55 row.set_title(base::UTF8ToUTF16("Page " + fake_hash)); | |
56 row.set_visit_count(1); | |
57 row.set_typed_count(1); | |
58 row.set_last_visit(base::Time::Now() - base::TimeDelta::FromDays(1)); | |
59 return row; | |
60 } | |
61 | |
62 using StringPieces = std::vector<base::StringPiece>; | |
63 | |
64 StringPieces AllPrefixes(const std::string& str) { | |
65 std::vector<base::StringPiece> res; | |
66 res.reserve(str.size()); | |
67 for (auto char_it = str.begin(); char_it != str.end(); ++char_it) | |
68 res.push_back({str.begin(), char_it}); | |
69 return res; | |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 class HQPPerfTestOnePopularURL : public testing::Test { | |
75 protected: | |
76 HQPPerfTestOnePopularURL() = default; | |
77 | |
78 void SetUp() override; | |
79 void TearDown() override; | |
80 | |
81 // Populates history with variations of the same URL. | |
82 void PrepareData(); | |
83 | |
84 template <typename PieceIt> | |
Peter Kasting
2016/10/27 09:00:56
Nit: Comments on these next two functions too woul
dyaroshev
2016/10/27 13:04:23
Done.
| |
85 void RunAllTests(PieceIt first, PieceIt last); | |
86 | |
87 void PrintMeasurements(const std::string& trace_name, | |
88 const std::vector<base::TimeDelta>& measurements); | |
89 | |
90 history::HistoryBackend* history_backend() { | |
91 return client_->GetHistoryService()->history_backend_.get(); | |
92 } | |
93 | |
94 private: | |
95 base::TimeDelta RunTest(const base::string16& text); | |
96 | |
97 base::MessageLoop message_loop_; | |
98 std::unique_ptr<FakeAutocompleteProviderClient> client_; | |
99 | |
100 scoped_refptr<HistoryQuickProvider> provider_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(HQPPerfTestOnePopularURL); | |
103 }; | |
104 | |
105 void HQPPerfTestOnePopularURL::SetUp() { | |
106 if (base::ThreadTicks::IsSupported()) | |
107 base::ThreadTicks::WaitUntilInitialized(); | |
108 client_.reset(new FakeAutocompleteProviderClient()); | |
109 ASSERT_TRUE(client_->GetHistoryService()); | |
110 PrepareData(); | |
111 } | |
112 | |
113 void HQPPerfTestOnePopularURL::TearDown() { | |
114 provider_ = nullptr; | |
115 // The InMemoryURLIndex must be explicitly shut down or it will DCHECK() in | |
116 // its destructor. | |
117 client_->GetInMemoryURLIndex()->Shutdown(); | |
118 client_->set_in_memory_url_index(nullptr); | |
119 // History index rebuild task is created from main thread during SetUp, | |
120 // performed on DB thread and must be deleted on main thread. | |
121 // Run main loop to process delete task, to prevent leaks. | |
122 base::RunLoop().RunUntilIdle(); | |
123 } | |
124 | |
125 void HQPPerfTestOnePopularURL::PrepareData() { | |
126 // Adding fake urls to db must be done before RebuildFromHistory(). This will | |
127 // ensure that the index is properly populated with data from the database. | |
128 constexpr size_t kSimilarUrlCount = 10000; | |
129 for (size_t i = 0; i < kSimilarUrlCount; ++i) | |
130 AddFakeURLToHistoryDB(history_backend()->db(), GeneratePopularURLRow()); | |
131 | |
132 InMemoryURLIndex* url_index = client_->GetInMemoryURLIndex(); | |
133 url_index->RebuildFromHistory( | |
134 client_->GetHistoryService()->history_backend_->db()); | |
135 BlockUntilInMemoryURLIndexIsRefreshed(url_index); | |
136 | |
137 // History index refresh creates rebuilt tasks to run on history thread. | |
138 // Block here to make sure that all of them are complete. | |
139 history::BlockUntilHistoryProcessesPendingRequests( | |
140 client_->GetHistoryService()); | |
141 | |
142 provider_ = new HistoryQuickProvider(client_.get()); | |
143 } | |
144 | |
145 void HQPPerfTestOnePopularURL::PrintMeasurements( | |
146 const std::string& trace_name, | |
147 const std::vector<base::TimeDelta>& measurements) { | |
148 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info(); | |
149 | |
150 std::string durations; | |
151 for (const auto& measurement : measurements) | |
152 durations += std::to_string(measurement.InMillisecondsRoundedUp()) + ','; | |
153 | |
154 perf_test::PrintResultList(test_info->test_case_name(), test_info->name(), | |
155 trace_name, durations, "ms", true); | |
156 } | |
157 | |
158 base::TimeDelta HQPPerfTestOnePopularURL::RunTest(const base::string16& text) { | |
159 base::RunLoop().RunUntilIdle(); | |
160 AutocompleteInput input(text, base::string16::npos, std::string(), GURL(), | |
161 metrics::OmniboxEventProto::INVALID_SPEC, false, | |
162 false, true, true, false, TestSchemeClassifier()); | |
163 if (base::ThreadTicks::IsSupported()) { | |
164 base::ThreadTicks start = base::ThreadTicks::Now(); | |
165 provider_->Start(input, false); | |
166 return base::ThreadTicks::Now() - start; | |
167 } | |
168 | |
169 base::Time start = base::Time::Now(); | |
170 provider_->Start(input, false); | |
171 return base::Time::Now() - start; | |
172 } | |
173 | |
174 template <typename PieceIt> | |
175 void HQPPerfTestOnePopularURL::RunAllTests(PieceIt first, PieceIt last) { | |
176 constexpr size_t kTestGroupSize = 5; | |
177 std::vector<base::TimeDelta> measurements; | |
178 measurements.reserve(kTestGroupSize); | |
179 | |
180 for (PieceIt group_start = first; group_start != last;) { | |
181 PieceIt group_end = std::min(group_start + kTestGroupSize, last); | |
182 | |
183 std::transform(group_start, group_end, std::back_inserter(measurements), | |
184 [this](const base::StringPiece& prefix) { | |
185 return RunTest(base::UTF8ToUTF16(prefix)); | |
186 }); | |
187 | |
188 PrintMeasurements(std::to_string(group_start->size()) + '-' + | |
189 std::to_string((group_end - 1)->size()), | |
190 measurements); | |
191 | |
192 measurements.clear(); | |
193 group_start = group_end; | |
194 } | |
195 } | |
196 | |
197 TEST_F(HQPPerfTestOnePopularURL, Typing) { | |
198 std::string test_url = GeneratePopularURLRow().url().spec(); | |
199 StringPieces prefixes = AllPrefixes(test_url); | |
200 RunAllTests(prefixes.begin(), prefixes.end()); | |
201 } | |
202 | |
203 TEST_F(HQPPerfTestOnePopularURL, Backspacing) { | |
204 std::string test_url = GeneratePopularURLRow().url().spec(); | |
205 StringPieces prefixes = AllPrefixes(test_url); | |
206 RunAllTests(prefixes.rbegin(), prefixes.rend()); | |
207 } | |
208 | |
209 } // namespace history | |
OLD | NEW |