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 different output strategy. | |
Peter Kasting
2016/10/17 18:44:46
Nit: Initial capital
You might want to describe w
| |
28 class TimeLogger { | |
29 public: | |
30 explicit TimeLogger(std::string test_name); | |
31 ~TimeLogger(); | |
32 void Done(); | |
33 | |
34 private: | |
35 bool logged_; | |
36 std::string test_name_; | |
37 base::ElapsedTimer timer_; | |
38 | |
39 DISALLOW_COPY_AND_ASSIGN(TimeLogger); | |
40 }; | |
41 | |
42 TimeLogger::TimeLogger(std::string test_name) | |
43 : logged_(false), test_name_(std::move(test_name)) {} | |
44 | |
45 TimeLogger::~TimeLogger() { | |
46 if (!logged_) | |
47 Done(); | |
48 } | |
49 | |
50 void TimeLogger::Done() { | |
51 // we use a floating-point millisecond value because it is more | |
52 // intuitive than microseconds and we want more precision than | |
53 // integer milliseconds | |
Peter Kasting
2016/10/17 18:44:45
Nit: Add trailing period
| |
54 perf_test::PrintResult(test_name_, "", "", timer_.Elapsed().InMillisecondsF(), | |
Peter Kasting
2016/10/17 18:44:46
Nit: "" -> std::string()
| |
55 "ms", true); | |
56 logged_ = true; | |
57 } | |
58 | |
28 // how we generate URLs, note that the two strings should be the same length | 59 // how we generate URLs, note that the two strings should be the same length |
29 const int add_count = 10000; | 60 const int add_count = 10000; |
30 const int load_test_add_count = 250000; | 61 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="; | 62 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="; | 63 const char unadded_prefix[] = "http://www.google.org/stuff/something/foo?session =39586739476365&id=2347624314402&seq="; |
33 | 64 |
34 // Returns a URL with the given prefix and index | 65 // Returns a URL with the given prefix and index |
35 GURL TestURL(const char* prefix, int i) { | 66 GURL TestURL(const char* prefix, int i) { |
36 return GURL(base::StringPrintf("%s%d", prefix, i)); | 67 return GURL(base::StringPrintf("%s%d", prefix, i)); |
37 } | 68 } |
(...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 | 110 // 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 | 111 // 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. | 112 // useful to make another set of tests to test these things in isolation. |
82 TEST_F(VisitedLink, TestAddAndQuery) { | 113 TEST_F(VisitedLink, TestAddAndQuery) { |
83 // init | 114 // init |
84 VisitedLinkMaster master(new DummyVisitedLinkEventListener(), | 115 VisitedLinkMaster master(new DummyVisitedLinkEventListener(), |
85 NULL, true, true, db_path_, 0); | 116 NULL, true, true, db_path_, 0); |
86 ASSERT_TRUE(master.Init()); | 117 ASSERT_TRUE(master.Init()); |
87 content::RunAllBlockingPoolTasksUntilIdle(); | 118 content::RunAllBlockingPoolTasksUntilIdle(); |
88 | 119 |
89 base::PerfTimeLogger timer("Visited_link_add_and_query"); | 120 TimeLogger timer("Visited_link_add_and_query"); |
90 | 121 |
91 // first check without anything in the table | 122 // first check without anything in the table |
92 CheckVisited(master, added_prefix, 0, add_count); | 123 CheckVisited(master, added_prefix, 0, add_count); |
93 | 124 |
94 // now fill half the table | 125 // now fill half the table |
95 const int half_size = add_count / 2; | 126 const int half_size = add_count / 2; |
96 FillTable(master, added_prefix, 0, half_size); | 127 FillTable(master, added_prefix, 0, half_size); |
97 | 128 |
98 // check the table again, half of these URLs will be visited, the other half | 129 // check the table again, half of these URLs will be visited, the other half |
99 // will not | 130 // will not |
100 CheckVisited(master, added_prefix, 0, add_count); | 131 CheckVisited(master, added_prefix, 0, add_count); |
101 | 132 |
102 // fill the rest of the table | 133 // fill the rest of the table |
103 FillTable(master, added_prefix, half_size, add_count); | 134 FillTable(master, added_prefix, half_size, add_count); |
104 | 135 |
105 // check URLs, doing half visited, half unvisited | 136 // check URLs, doing half visited, half unvisited |
106 CheckVisited(master, added_prefix, 0, add_count); | 137 CheckVisited(master, added_prefix, 0, add_count); |
107 CheckVisited(master, unadded_prefix, 0, add_count); | 138 CheckVisited(master, unadded_prefix, 0, add_count); |
108 } | 139 } |
109 | 140 |
110 // Tests how long it takes to write and read a large database to and from disk. | 141 // Tests how long it takes to write and read a large database to and from disk. |
111 TEST_F(VisitedLink, TestLoad) { | 142 TEST_F(VisitedLink, TestLoad) { |
112 // create a big DB | 143 // create a big DB |
113 { | 144 { |
114 base::PerfTimeLogger table_initialization_timer("Table_initialization"); | 145 TimeLogger table_initialization_timer("Table_initialization"); |
115 | 146 |
116 VisitedLinkMaster master(new DummyVisitedLinkEventListener(), | 147 VisitedLinkMaster master(new DummyVisitedLinkEventListener(), |
117 NULL, true, true, db_path_, 0); | 148 NULL, true, true, db_path_, 0); |
118 | 149 |
119 // time init with empty table | 150 // time init with empty table |
120 base::PerfTimeLogger initTimer("Empty_visited_link_init"); | 151 TimeLogger initTimer("Empty_visited_link_init"); |
121 bool success = master.Init(); | 152 bool success = master.Init(); |
122 content::RunAllBlockingPoolTasksUntilIdle(); | 153 content::RunAllBlockingPoolTasksUntilIdle(); |
123 initTimer.Done(); | 154 initTimer.Done(); |
124 ASSERT_TRUE(success); | 155 ASSERT_TRUE(success); |
125 | 156 |
126 // add a bunch of stuff | 157 // add a bunch of stuff |
127 // TODO(maruel): This is very inefficient because the file gets rewritten | 158 // 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 | 159 // 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. | 160 // only get written that the end of the FillTable call, not 4169(!) times. |
130 FillTable(master, added_prefix, 0, load_test_add_count); | 161 FillTable(master, added_prefix, 0, load_test_add_count); |
131 | 162 |
132 // time writing the file out out | 163 // time writing the file out out |
133 base::PerfTimeLogger flushTimer("Visited_link_database_flush"); | 164 TimeLogger flushTimer("Visited_link_database_flush"); |
134 master.RewriteFile(); | 165 master.RewriteFile(); |
135 // TODO(maruel): Without calling FlushFileBuffers(master.file_); you don't | 166 // TODO(maruel): Without calling FlushFileBuffers(master.file_); you don't |
136 // know really how much time it took to write the file. | 167 // know really how much time it took to write the file. |
137 flushTimer.Done(); | 168 flushTimer.Done(); |
138 | 169 |
139 table_initialization_timer.Done(); | 170 table_initialization_timer.Done(); |
140 } | 171 } |
141 | 172 |
142 // test loading the DB back, we do this several times since the flushing is | 173 // test loading the DB back, we do this several times since the flushing is |
143 // not very reliable. | 174 // 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(), | 220 cold_load_times.erase(std::max_element(cold_load_times.begin(), |
190 cold_load_times.end())); | 221 cold_load_times.end())); |
191 hot_load_times.erase(std::max_element(hot_load_times.begin(), | 222 hot_load_times.erase(std::max_element(hot_load_times.begin(), |
192 hot_load_times.end())); | 223 hot_load_times.end())); |
193 | 224 |
194 double cold_sum = 0, hot_sum = 0; | 225 double cold_sum = 0, hot_sum = 0; |
195 for (int i = 0; i < static_cast<int>(cold_load_times.size()); i++) { | 226 for (int i = 0; i < static_cast<int>(cold_load_times.size()); i++) { |
196 cold_sum += cold_load_times[i]; | 227 cold_sum += cold_load_times[i]; |
197 hot_sum += hot_load_times[i]; | 228 hot_sum += hot_load_times[i]; |
198 } | 229 } |
199 base::LogPerfResult( | 230 |
200 "Visited_link_cold_load_time", cold_sum / cold_load_times.size(), "ms"); | 231 perf_test::PrintResult("Visited_link_cold_load_time", "", "", |
Peter Kasting
2016/10/17 18:44:45
Nit: "" -> std::string (2 places)
| |
201 base::LogPerfResult( | 232 cold_sum / cold_load_times.size(), "ms", true); |
202 "Visited_link_hot_load_time", hot_sum / hot_load_times.size(), "ms"); | 233 perf_test::PrintResult("Visited_link_hot_load_time", "", "", |
234 hot_sum / hot_load_times.size(), "ms", true); | |
203 } | 235 } |
204 | 236 |
205 } // namespace visitedlink | 237 } // namespace visitedlink |
OLD | NEW |