OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/test/perf_log.h" | |
13 #include "base/test/perf_time_logger.h" | |
14 #include "base/test/test_file_util.h" | 12 #include "base/test/test_file_util.h" |
15 #include "base/timer/elapsed_timer.h" | 13 #include "base/timer/elapsed_timer.h" |
16 #include "components/visitedlink/browser/visitedlink_master.h" | 14 #include "components/visitedlink/browser/visitedlink_master.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "testing/perf/perf_test.h" | |
20 #include "url/gurl.h" | 19 #include "url/gurl.h" |
21 | 20 |
22 using base::TimeDelta; | 21 using base::TimeDelta; |
23 | 22 |
24 namespace visitedlink { | 23 namespace visitedlink { |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
27 // Designed like base/test/perf_time_logger but uses testing/perf instead of | |
28 // base/test/perf* to report timings. | |
29 class TimeLogger { | |
30 public: | |
31 explicit TimeLogger(std::string test_name); | |
Alexei Svitkine (slow)
2016/10/21 19:01:46
Nit: const&
Peter Kasting
2016/10/21 19:45:21
Can't, he's doing std::move() below to move into t
| |
32 ~TimeLogger(); | |
33 void Done(); | |
34 | |
35 private: | |
36 bool logged_; | |
37 std::string test_name_; | |
38 base::ElapsedTimer timer_; | |
39 | |
40 DISALLOW_COPY_AND_ASSIGN(TimeLogger); | |
41 }; | |
42 | |
43 TimeLogger::TimeLogger(std::string test_name) | |
44 : logged_(false), test_name_(std::move(test_name)) {} | |
45 | |
46 TimeLogger::~TimeLogger() { | |
47 if (!logged_) | |
48 Done(); | |
49 } | |
50 | |
51 void TimeLogger::Done() { | |
52 // We use a floating-point millisecond value because it is more | |
53 // intuitive than microseconds and we want more precision than | |
54 // integer milliseconds. | |
55 perf_test::PrintResult(test_name_, std::string(), std::string(), | |
56 timer_.Elapsed().InMillisecondsF(), "ms", true); | |
57 logged_ = true; | |
58 } | |
59 | |
28 // how we generate URLs, note that the two strings should be the same length | 60 // how we generate URLs, note that the two strings should be the same length |
29 const int add_count = 10000; | 61 const int add_count = 10000; |
30 const int load_test_add_count = 250000; | 62 const int load_test_add_count = 250000; |
31 const char added_prefix[] = "http://www.google.com/stuff/something/foo?session=8 5025602345625&id=1345142319023&seq="; | 63 const char added_prefix[] = "http://www.google.com/stuff/something/foo?session=8 5025602345625&id=1345142319023&seq="; |
32 const char unadded_prefix[] = "http://www.google.org/stuff/something/foo?session =39586739476365&id=2347624314402&seq="; | 64 const char unadded_prefix[] = "http://www.google.org/stuff/something/foo?session =39586739476365&id=2347624314402&seq="; |
33 | 65 |
34 // Returns a URL with the given prefix and index | 66 // Returns a URL with the given prefix and index |
35 GURL TestURL(const char* prefix, int i) { | 67 GURL TestURL(const char* prefix, int i) { |
36 return GURL(base::StringPrintf("%s%d", prefix, i)); | 68 return GURL(base::StringPrintf("%s%d", prefix, i)); |
37 } | 69 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 // is the total time to do all the operations, and as such, it is only | 111 // is the total time to do all the operations, and as such, it is only |
80 // useful for a regression test. If there is a regression, it might be | 112 // useful for a regression test. If there is a regression, it might be |
81 // useful to make another set of tests to test these things in isolation. | 113 // useful to make another set of tests to test these things in isolation. |
82 TEST_F(VisitedLink, TestAddAndQuery) { | 114 TEST_F(VisitedLink, TestAddAndQuery) { |
83 // init | 115 // init |
84 VisitedLinkMaster master(new DummyVisitedLinkEventListener(), | 116 VisitedLinkMaster master(new DummyVisitedLinkEventListener(), |
85 NULL, true, true, db_path_, 0); | 117 NULL, true, true, db_path_, 0); |
86 ASSERT_TRUE(master.Init()); | 118 ASSERT_TRUE(master.Init()); |
87 content::RunAllBlockingPoolTasksUntilIdle(); | 119 content::RunAllBlockingPoolTasksUntilIdle(); |
88 | 120 |
89 base::PerfTimeLogger timer("Visited_link_add_and_query"); | 121 TimeLogger timer("Visited_link_add_and_query"); |
90 | 122 |
91 // first check without anything in the table | 123 // first check without anything in the table |
92 CheckVisited(master, added_prefix, 0, add_count); | 124 CheckVisited(master, added_prefix, 0, add_count); |
93 | 125 |
94 // now fill half the table | 126 // now fill half the table |
95 const int half_size = add_count / 2; | 127 const int half_size = add_count / 2; |
96 FillTable(master, added_prefix, 0, half_size); | 128 FillTable(master, added_prefix, 0, half_size); |
97 | 129 |
98 // check the table again, half of these URLs will be visited, the other half | 130 // check the table again, half of these URLs will be visited, the other half |
99 // will not | 131 // will not |
100 CheckVisited(master, added_prefix, 0, add_count); | 132 CheckVisited(master, added_prefix, 0, add_count); |
101 | 133 |
102 // fill the rest of the table | 134 // fill the rest of the table |
103 FillTable(master, added_prefix, half_size, add_count); | 135 FillTable(master, added_prefix, half_size, add_count); |
104 | 136 |
105 // check URLs, doing half visited, half unvisited | 137 // check URLs, doing half visited, half unvisited |
106 CheckVisited(master, added_prefix, 0, add_count); | 138 CheckVisited(master, added_prefix, 0, add_count); |
107 CheckVisited(master, unadded_prefix, 0, add_count); | 139 CheckVisited(master, unadded_prefix, 0, add_count); |
108 } | 140 } |
109 | 141 |
110 // Tests how long it takes to write and read a large database to and from disk. | 142 // Tests how long it takes to write and read a large database to and from disk. |
111 TEST_F(VisitedLink, TestLoad) { | 143 TEST_F(VisitedLink, TestLoad) { |
112 // create a big DB | 144 // create a big DB |
113 { | 145 { |
114 base::PerfTimeLogger table_initialization_timer("Table_initialization"); | 146 TimeLogger table_initialization_timer("Table_initialization"); |
115 | 147 |
116 VisitedLinkMaster master(new DummyVisitedLinkEventListener(), | 148 VisitedLinkMaster master(new DummyVisitedLinkEventListener(), |
117 NULL, true, true, db_path_, 0); | 149 NULL, true, true, db_path_, 0); |
118 | 150 |
119 // time init with empty table | 151 // time init with empty table |
120 base::PerfTimeLogger initTimer("Empty_visited_link_init"); | 152 TimeLogger initTimer("Empty_visited_link_init"); |
121 bool success = master.Init(); | 153 bool success = master.Init(); |
122 content::RunAllBlockingPoolTasksUntilIdle(); | 154 content::RunAllBlockingPoolTasksUntilIdle(); |
123 initTimer.Done(); | 155 initTimer.Done(); |
124 ASSERT_TRUE(success); | 156 ASSERT_TRUE(success); |
125 | 157 |
126 // add a bunch of stuff | 158 // add a bunch of stuff |
127 // TODO(maruel): This is very inefficient because the file gets rewritten | 159 // TODO(maruel): This is very inefficient because the file gets rewritten |
128 // many time and this is the actual bottleneck of this test. The file should | 160 // many time and this is the actual bottleneck of this test. The file should |
129 // only get written that the end of the FillTable call, not 4169(!) times. | 161 // only get written that the end of the FillTable call, not 4169(!) times. |
130 FillTable(master, added_prefix, 0, load_test_add_count); | 162 FillTable(master, added_prefix, 0, load_test_add_count); |
131 | 163 |
132 // time writing the file out out | 164 // time writing the file out out |
133 base::PerfTimeLogger flushTimer("Visited_link_database_flush"); | 165 TimeLogger flushTimer("Visited_link_database_flush"); |
134 master.RewriteFile(); | 166 master.RewriteFile(); |
135 // TODO(maruel): Without calling FlushFileBuffers(master.file_); you don't | 167 // TODO(maruel): Without calling FlushFileBuffers(master.file_); you don't |
136 // know really how much time it took to write the file. | 168 // know really how much time it took to write the file. |
137 flushTimer.Done(); | 169 flushTimer.Done(); |
138 | 170 |
139 table_initialization_timer.Done(); | 171 table_initialization_timer.Done(); |
140 } | 172 } |
141 | 173 |
142 // test loading the DB back, we do this several times since the flushing is | 174 // test loading the DB back, we do this several times since the flushing is |
143 // not very reliable. | 175 // not very reliable. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 cold_load_times.erase(std::max_element(cold_load_times.begin(), | 221 cold_load_times.erase(std::max_element(cold_load_times.begin(), |
190 cold_load_times.end())); | 222 cold_load_times.end())); |
191 hot_load_times.erase(std::max_element(hot_load_times.begin(), | 223 hot_load_times.erase(std::max_element(hot_load_times.begin(), |
192 hot_load_times.end())); | 224 hot_load_times.end())); |
193 | 225 |
194 double cold_sum = 0, hot_sum = 0; | 226 double cold_sum = 0, hot_sum = 0; |
195 for (int i = 0; i < static_cast<int>(cold_load_times.size()); i++) { | 227 for (int i = 0; i < static_cast<int>(cold_load_times.size()); i++) { |
196 cold_sum += cold_load_times[i]; | 228 cold_sum += cold_load_times[i]; |
197 hot_sum += hot_load_times[i]; | 229 hot_sum += hot_load_times[i]; |
198 } | 230 } |
199 base::LogPerfResult( | 231 |
200 "Visited_link_cold_load_time", cold_sum / cold_load_times.size(), "ms"); | 232 perf_test::PrintResult("Visited_link_cold_load_time", std::string(), |
201 base::LogPerfResult( | 233 std::string(), cold_sum / cold_load_times.size(), "ms", |
202 "Visited_link_hot_load_time", hot_sum / hot_load_times.size(), "ms"); | 234 true); |
235 perf_test::PrintResult("Visited_link_hot_load_time", std::string(), | |
236 std::string(), hot_sum / hot_load_times.size(), "ms", | |
237 true); | |
203 } | 238 } |
204 | 239 |
205 } // namespace visitedlink | 240 } // namespace visitedlink |
OLD | NEW |