OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string> | |
6 #include <vector> | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/perftimer.h" | |
10 #include "base/scoped_ptr.h" | |
11 #include "chrome/browser/privacy_blacklist/blacklist.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace { | |
15 | |
16 // Number of URLs to try for each test. | |
17 const size_t kNumUrls = 10000; | |
18 | |
19 // Fixed random seed for srand(), so always the same random blacklist is | |
20 // generated. | |
21 const unsigned int kSeed = 0; | |
22 | |
23 // Number of entries for the benchmark blacklist. The blacklist will contain | |
24 // five different kinds of patterns: | |
25 // - host.name.tld/ | |
26 // - /some/path | |
27 // - /some/path/script? | |
28 // - host.name.tld/some/path | |
29 // - host.name.tld/some/path/?script | |
30 const size_t kNumHostEntries = 500; | |
31 const size_t kNumPathEntries = 500; | |
32 const size_t kNumScriptEntries = 500; | |
33 const size_t kNumHostPathEntries = 250; | |
34 const size_t kNumHostScriptEntries = 250; | |
35 const size_t kNumPattern = kNumHostEntries + kNumPathEntries + | |
36 kNumScriptEntries + kNumHostPathEntries + | |
37 kNumHostScriptEntries; | |
38 | |
39 // Returns a random integer between 0 and |max| - 1. | |
40 static size_t RandomIndex(size_t max) { | |
41 DCHECK_GT(max, 0U); | |
42 | |
43 return static_cast<size_t>(static_cast<float>(max) * | |
44 (rand() / (RAND_MAX + 1.0))); | |
45 } | |
46 | |
47 // Generates a random alpha-numeric string of a given |length|. | |
48 static std::string GetRandomString(size_t length) { | |
49 // Alpha-numeric characters without the letter a. This is later used | |
50 // in |GetNotMatchingURL| to generate disjoint patterns. | |
51 static const char chars[] = "bcdefghijklmnopqrstuvwxyz0123456789"; | |
52 std::string result; | |
53 | |
54 for (size_t i = 0; i < length; ++i) { | |
55 result += chars[RandomIndex(arraysize(chars) - 1)]; | |
56 } | |
57 | |
58 return result; | |
59 } | |
60 | |
61 // Generates a URL that will match a given pattern. Assumes that the pattern | |
62 // consists of at least a leading wildcard and a trailing wildcard. If invert | |
63 // is true, the returned URL will NOT match the pattern or any other pattern | |
64 // generated for the blacklist. | |
65 static std::string GetURL(const std::string& pattern, bool invert) { | |
66 DCHECK_GT(pattern.length(), 2U); | |
67 DCHECK_EQ(pattern[0], '@'); | |
68 DCHECK_EQ(pattern[pattern.length() - 1], '@'); | |
69 | |
70 std::string url("http://"); | |
71 | |
72 // If this is a path pattern, prepend host.tld, otherwise just prepend host. | |
73 if (pattern[1] == '/') { | |
74 url += GetRandomString(8) + "." + GetRandomString(3); | |
75 } else { | |
76 url += GetRandomString(6) + "."; | |
77 } | |
78 | |
79 // Add the constant part of the pattern. | |
80 url += pattern.substr(1, pattern.length() - 2); | |
81 | |
82 // If this is a script pattern, append name1=value parameters, otherwise | |
83 // append a /path/file.ext | |
84 if (pattern[pattern.length() - 2] == '?') { | |
85 url += GetRandomString(5) + "=" + GetRandomString(10); | |
86 } else { | |
87 url += GetRandomString(6) + "/" + GetRandomString(6) + "." + | |
88 GetRandomString(3); | |
89 } | |
90 | |
91 // If the URL is supposed to match no pattern, we introduce the letter 'a' | |
92 // which is not used for generating patterns | |
93 if (invert) { | |
94 // Position of the host/path separating slash. Skipping the 7 characters | |
95 // of "http://". | |
96 size_t host_path_separator = 7 + url.find_first_of('/', 7); | |
97 | |
98 // If the pattern is starting with a /path, modify the first path element, | |
99 // otherwise modify the hostname. | |
100 if (pattern[1] == '/') { | |
101 url[host_path_separator + 3] = 'a'; | |
102 } else { | |
103 // Locate the domain name. | |
104 url[url.substr(0, host_path_separator).find_last_of('.') - 2] = 'a'; | |
105 } | |
106 } | |
107 | |
108 return url; | |
109 } | |
110 | |
111 class BlacklistPerfTest : public testing::Test { | |
112 public: | |
113 BlacklistPerfTest() : blacklist_(new Blacklist()) {} | |
114 | |
115 protected: | |
116 virtual void SetUp() { | |
117 srand(kSeed); | |
118 | |
119 // Generate random benchmark blacklist. | |
120 Blacklist::Provider* provider = new Blacklist::Provider("test", | |
121 "http://test.com", | |
122 L"test"); | |
123 blacklist_->AddProvider(provider); | |
124 | |
125 // Create host.tld/ patterns. | |
126 for (size_t i = 0; i < kNumHostEntries; ++i) { | |
127 std::string pattern = "@" + GetRandomString(8) + "." + | |
128 GetRandomString(3) + "/@"; | |
129 blacklist_->AddEntry(new Blacklist::Entry(pattern, provider, false)); | |
130 } | |
131 | |
132 // Create /some/path/ patterns. | |
133 for (size_t i = 0; i < kNumPathEntries; ++i) { | |
134 std::string pattern = "@/" + GetRandomString(6) + "/" + | |
135 GetRandomString(6) + "/@"; | |
136 blacklist_->AddEntry(new Blacklist::Entry(pattern, provider, false)); | |
137 } | |
138 | |
139 // Create /some/path/script? patterns. | |
140 for (size_t i = 0; i < kNumScriptEntries; ++i) { | |
141 std::string pattern = "@/" + GetRandomString(6) + "/" + | |
142 GetRandomString(6) + "/" + GetRandomString(6) + | |
143 "?@"; | |
144 blacklist_->AddEntry(new Blacklist::Entry(pattern, provider, false)); | |
145 } | |
146 | |
147 // Create host.tld/some/path patterns. | |
148 for (size_t i = 0; i < kNumHostPathEntries; ++i) { | |
149 std::string pattern = "@" + GetRandomString(8) + "." + | |
150 GetRandomString(3) + "/" + GetRandomString(6) + | |
151 "/" + GetRandomString(6) + "/@"; | |
152 blacklist_->AddEntry(new Blacklist::Entry(pattern, provider, false)); | |
153 } | |
154 | |
155 // Create host.tld/some/path/script? patterns. | |
156 for (size_t i = 0; i < kNumHostScriptEntries; ++i) { | |
157 std::string pattern = "@" + GetRandomString(8) + "." + | |
158 GetRandomString(3) + "/" + GetRandomString(6) + | |
159 "/" + GetRandomString(6) + "?@"; | |
160 blacklist_->AddEntry(new Blacklist::Entry(pattern, provider, false)); | |
161 } | |
162 | |
163 DCHECK_EQ(std::distance(blacklist_->entries_begin(), | |
164 blacklist_->entries_end()), static_cast<ptrdiff_t>(kNumPattern)); | |
165 } | |
166 | |
167 // Randomly generated benchmark blacklist. | |
168 scoped_refptr<Blacklist> blacklist_; | |
169 }; | |
170 | |
171 // Perf test for matching URLs which are contained in the blacklist. | |
172 TEST_F(BlacklistPerfTest, Match) { | |
173 // Pick random patterns and generate matching URLs | |
174 std::vector<std::string> urls; | |
175 for (size_t i = 0; i < kNumUrls; ++i) { | |
176 const Blacklist::Entry* entry = (blacklist_->entries_begin() + | |
177 RandomIndex(kNumPattern))->get(); | |
178 urls.push_back(GetURL(entry->pattern(), false)); | |
179 } | |
180 | |
181 // Measure performance for matching urls against the blacklist. | |
182 PerfTimeLogger timer("blacklist_match"); | |
183 | |
184 for (size_t i = 0; i < kNumUrls; ++i) { | |
185 scoped_ptr<Blacklist::Match> match(blacklist_->FindMatch(GURL(urls[i]))); | |
186 ASSERT_TRUE(match.get()); | |
187 } | |
188 | |
189 timer.Done(); | |
190 } | |
191 | |
192 // Perf test for matching URLs which are NOT contained in the blacklist. | |
193 TEST_F(BlacklistPerfTest, Mismatch) { | |
194 // We still use randomly picked patterns for generating URLs, but the URLs | |
195 // are made so that they do NOT match any generated pattern | |
196 std::vector<std::string> urls; | |
197 for (size_t i = 0; i < kNumUrls; ++i) { | |
198 const Blacklist::Entry* entry = (blacklist_->entries_begin() + | |
199 RandomIndex(kNumPattern))->get(); | |
200 urls.push_back(GetURL(entry->pattern(), true)); | |
201 } | |
202 | |
203 // Measure performance for matching urls against the blacklist. | |
204 PerfTimeLogger timer("blacklist_mismatch"); | |
205 | |
206 for (size_t i = 0; i < kNumUrls; ++i) { | |
207 scoped_ptr<Blacklist::Match> match(blacklist_->FindMatch(GURL(urls[i]))); | |
208 ASSERT_FALSE(match.get()); | |
209 } | |
210 | |
211 timer.Done(); | |
212 } | |
213 | |
214 } // namespace | |
OLD | NEW |