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 constexpr size_t kFakeHashLength = 10; | |
Peter Kasting
2016/10/27 00:59:38
Nit: It's legal to do either way, but I would defi
dyaroshev
2016/10/27 06:09:21
Done.
| |
29 constexpr size_t kSimilarUrlCount = 10000; | |
30 constexpr size_t kTestGroupSize = 5; | |
31 | |
32 // Not threadsafe. | |
33 std::string GenerateFakeHashedString(size_t sym_count) { | |
34 static constexpr char kSyms[] = | |
35 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,/=+?#"; | |
36 CR_DEFINE_STATIC_LOCAL(std::mt19937, engine, (0)); | |
Peter Kasting
2016/10/27 00:59:38
Why pass 0 explicitly? Just using the default see
dyaroshev
2016/10/27 06:09:21
Done.
| |
37 std::uniform_int_distribution<size_t> index_distribution( | |
38 0, arraysize(kSyms) - 2 /* trailing \0 */); | |
39 | |
40 std::string res; | |
41 res.reserve(sym_count); | |
42 | |
43 std::generate_n(std::back_inserter(res), sym_count, [&index_distribution] { | |
44 return kSyms[index_distribution(engine)]; | |
45 }); | |
46 | |
47 return res; | |
48 } | |
49 | |
50 URLRow GeneratePopularURLRow() { | |
51 static constexpr char kPopularUrl[] = | |
52 "http://long.popular_url_with.many_variations/"; | |
53 | |
54 std::string fake_hash = GenerateFakeHashedString(kFakeHashLength); | |
55 URLRow row{GURL(kPopularUrl + fake_hash)}; | |
56 CHECK(row.url().is_valid()); | |
Peter Kasting
2016/10/27 00:59:38
Nit: Prefer ASSERT_TRUE to CHECK in test code (cau
dyaroshev
2016/10/27 06:09:21
Done.
| |
57 row.set_title(base::UTF8ToUTF16("Page " + fake_hash)); | |
58 row.set_visit_count(1); | |
59 row.set_typed_count(1); | |
60 row.set_last_visit(base::Time::Now() - base::TimeDelta::FromDays(1)); | |
61 return row; | |
62 } | |
63 | |
64 template <typename Action> | |
65 base::TimeDelta TimeAction(Action action) { | |
Peter Kasting
2016/10/27 00:59:38
Nit: This helper is only called once; is there rea
dyaroshev
2016/10/27 06:09:21
Done.
| |
66 if (base::ThreadTicks::IsSupported()) { | |
67 base::ThreadTicks start = base::ThreadTicks::Now(); | |
68 action(); | |
69 return base::ThreadTicks::Now() - start; | |
70 } | |
71 | |
72 base::Time start = base::Time::Now(); | |
73 action(); | |
74 return base::Time::Now() - start; | |
75 } | |
76 | |
77 using StringPieces = std::vector<base::StringPiece>; | |
78 | |
79 StringPieces AllPrefixes(const std::string& str) { | |
80 std::vector<base::StringPiece> res; | |
81 res.reserve(str.size()); | |
82 for (auto char_it = str.begin(); char_it != str.end(); ++char_it) | |
83 res.push_back({str.begin(), char_it}); | |
84 return res; | |
85 } | |
86 | |
87 } // namespace | |
88 | |
89 class HQPPerfTestOnePopularURL : public testing::Test { | |
90 protected: | |
91 HQPPerfTestOnePopularURL() = default; | |
92 | |
93 void SetUp() override; | |
94 void TearDown() override; | |
95 | |
96 void PrepareData(); | |
97 | |
98 template <typename PieceIt> | |
99 void RunAllTests(PieceIt first, PieceIt last); | |
100 | |
101 void PrintMeasurements(const std::string& trace_name, | |
102 const std::vector<base::TimeDelta>& measurements); | |
103 | |
104 history::HistoryBackend* history_backend() { | |
105 return client_->GetHistoryService()->history_backend_.get(); | |
106 } | |
107 | |
108 private: | |
109 base::TimeDelta RunTest(const base::string16& text); | |
110 | |
111 base::MessageLoop message_loop_; | |
112 std::unique_ptr<FakeAutocompleteProviderClient> client_; | |
113 | |
114 scoped_refptr<HistoryQuickProvider> provider_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(HQPPerfTestOnePopularURL); | |
117 }; | |
118 | |
119 void HQPPerfTestOnePopularURL::SetUp() { | |
120 if (base::ThreadTicks::IsSupported()) | |
121 base::ThreadTicks::WaitUntilInitialized(); | |
122 client_.reset(new FakeAutocompleteProviderClient()); | |
123 ASSERT_TRUE(client_->GetHistoryService()); | |
124 PrepareData(); | |
125 } | |
126 | |
127 void HQPPerfTestOnePopularURL::TearDown() { | |
128 provider_ = nullptr; | |
129 // The InMemoryURLIndex must be explicitly shut down or it will DCHECK() in | |
130 // its destructor. | |
131 client_->GetInMemoryURLIndex()->Shutdown(); | |
132 client_->set_in_memory_url_index(nullptr); | |
133 // History index rebuild task is created from main thread during SetUp, | |
134 // performed on DB thread and must be deleted on main thread. | |
135 // Run main loop to process delete task, to prevent leaks. | |
136 base::RunLoop().RunUntilIdle(); | |
137 } | |
138 | |
139 void HQPPerfTestOnePopularURL::PrepareData() { | |
140 for (size_t i = 0; i < kSimilarUrlCount; ++i) | |
141 AddFakeURLToHistoryDB(history_backend()->db(), GeneratePopularURLRow()); | |
142 | |
143 // |FillHistoryDB()| must be called before |RebuildFromHistory()|. This will | |
Peter Kasting
2016/10/27 00:59:38
Nit: No || on function names
dyaroshev
2016/10/27 06:09:21
Done.
| |
144 // ensure that the index is properly populated with data from the database. | |
145 InMemoryURLIndex* url_index = client_->GetInMemoryURLIndex(); | |
146 url_index->RebuildFromHistory( | |
147 client_->GetHistoryService()->history_backend_->db()); | |
148 BlockUntilInMemoryURLIndexIsRefreshed(url_index); | |
149 | |
150 // History index refresh creates rebuilt tasks to run on history thread. | |
151 // Block here to make sure that all of them are complete. | |
152 history::BlockUntilHistoryProcessesPendingRequests( | |
153 client_->GetHistoryService()); | |
154 | |
155 provider_ = new HistoryQuickProvider(client_.get()); | |
156 } | |
157 | |
158 void HQPPerfTestOnePopularURL::PrintMeasurements( | |
159 const std::string& trace_name, | |
160 const std::vector<base::TimeDelta>& measurements) { | |
161 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info(); | |
162 | |
163 std::string durations; | |
164 for (const auto& measurement : measurements) | |
165 durations += std::to_string(measurement.InMillisecondsRoundedUp()) + ','; | |
166 | |
167 perf_test::PrintResultList(test_info->test_case_name(), test_info->name(), | |
168 trace_name, durations, "ms", true); | |
169 } | |
170 | |
171 base::TimeDelta HQPPerfTestOnePopularURL::RunTest(const base::string16& text) { | |
172 base::RunLoop().RunUntilIdle(); | |
173 AutocompleteInput input(text, base::string16::npos, std::string(), GURL(), | |
174 metrics::OmniboxEventProto::INVALID_SPEC, false, | |
175 false, true, true, false, TestSchemeClassifier()); | |
176 | |
177 return TimeAction( | |
178 [this, &input]() mutable { provider_->Start(input, false); }); | |
179 } | |
180 | |
181 template <typename PieceIt> | |
182 void HQPPerfTestOnePopularURL::RunAllTests(PieceIt first, PieceIt last) { | |
183 std::vector<base::TimeDelta> measurements; | |
184 measurements.reserve(kTestGroupSize); | |
185 | |
186 for (PieceIt group_start = first; group_start != last;) { | |
187 PieceIt group_end = std::min(group_start + kTestGroupSize, last); | |
188 | |
189 std::transform(group_start, group_end, std::back_inserter(measurements), | |
190 [this](const base::StringPiece& prefix) { | |
191 return RunTest(base::UTF8ToUTF16(prefix)); | |
192 }); | |
193 | |
194 PrintMeasurements(std::to_string(group_start->size()) + '-' + | |
195 std::to_string((group_end - 1)->size()), | |
196 measurements); | |
197 | |
198 measurements.clear(); | |
199 group_start = group_end; | |
200 } | |
201 } | |
202 | |
203 TEST_F(HQPPerfTestOnePopularURL, Typing) { | |
204 std::string test_url = GeneratePopularURLRow().url().spec(); | |
205 StringPieces prefixes = AllPrefixes(test_url); | |
206 RunAllTests(prefixes.begin(), prefixes.end()); | |
207 } | |
208 | |
209 TEST_F(HQPPerfTestOnePopularURL, Backspacing) { | |
210 std::string test_url = GeneratePopularURLRow().url().spec(); | |
211 StringPieces prefixes = AllPrefixes(test_url); | |
212 RunAllTests(prefixes.rbegin(), prefixes.rend()); | |
213 } | |
214 | |
215 } // namespace history | |
OLD | NEW |