Use native radiobutton for Default Search Engine picker dialog.
[chromium-blink-merge.git] / net / proxy / proxy_resolver_perftest.cc
blob4307759ebe55664d835a9685e02aaa97170ba9ec
1 // Copyright (c) 2012 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.
5 #include "base/base_paths.h"
6 #include "base/compiler_specific.h"
7 #include "base/files/file_util.h"
8 #include "base/path_service.h"
9 #include "base/strings/string_util.h"
10 #include "base/test/perf_time_logger.h"
11 #include "net/base/net_errors.h"
12 #include "net/dns/mock_host_resolver.h"
13 #include "net/proxy/proxy_info.h"
14 #include "net/proxy/proxy_resolver_factory.h"
15 #include "net/proxy/proxy_resolver_v8.h"
16 #include "net/test/spawned_test_server/spawned_test_server.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 #if defined(OS_WIN)
20 #include "net/proxy/proxy_resolver_winhttp.h"
21 #elif defined(OS_MACOSX)
22 #include "net/proxy/proxy_resolver_mac.h"
23 #endif
25 namespace net {
27 namespace {
29 // This class holds the URL to use for resolving, and the expected result.
30 // We track the expected result in order to make sure the performance
31 // test is actually resolving URLs properly, otherwise the perf numbers
32 // are meaningless :-)
33 struct PacQuery {
34 const char* query_url;
35 const char* expected_result;
38 // Entry listing which PAC scripts to load, and which URLs to try resolving.
39 // |queries| should be terminated by {NULL, NULL}. A sentinel is used
40 // rather than a length, to simplify using initializer lists.
41 struct PacPerfTest {
42 const char* pac_name;
43 PacQuery queries[100];
45 // Returns the actual number of entries in |queries| (assumes NULL sentinel).
46 int NumQueries() const;
49 // List of performance tests.
50 static PacPerfTest kPerfTests[] = {
51 // This test uses an ad-blocker PAC script. This script is very heavily
52 // regular expression oriented, and has no dependencies on the current
53 // IP address, or DNS resolving of hosts.
54 { "no-ads.pac",
55 { // queries:
56 {"http://www.google.com", "DIRECT"},
57 {"http://www.imdb.com/photos/cmsicons/x", "PROXY 0.0.0.0:3421"},
58 {"http://www.imdb.com/x", "DIRECT"},
59 {"http://www.staples.com/", "DIRECT"},
60 {"http://www.staples.com/pixeltracker/x", "PROXY 0.0.0.0:3421"},
61 {"http://www.staples.com/pixel/x", "DIRECT"},
62 {"http://www.foobar.com", "DIRECT"},
63 {"http://www.foobarbaz.com/x/y/z", "DIRECT"},
64 {"http://www.testurl1.com/index.html", "DIRECT"},
65 {"http://www.testurl2.com", "DIRECT"},
66 {"https://www.sample/pirate/arrrrrr", "DIRECT"},
67 {NULL, NULL}
72 int PacPerfTest::NumQueries() const {
73 for (size_t i = 0; i < arraysize(queries); ++i) {
74 if (queries[i].query_url == NULL)
75 return i;
77 NOTREACHED(); // Bad definition.
78 return 0;
81 // The number of URLs to resolve when testing a PAC script.
82 const int kNumIterations = 500;
84 // Helper class to run through all the performance tests using the specified
85 // proxy resolver implementation.
86 class PacPerfSuiteRunner {
87 public:
88 // |resolver_name| is the label used when logging the results.
89 PacPerfSuiteRunner(ProxyResolverFactory* factory,
90 const std::string& resolver_name)
91 : factory_(factory),
92 resolver_name_(resolver_name),
93 test_server_(SpawnedTestServer::TYPE_HTTP,
94 SpawnedTestServer::kLocalhost,
95 base::FilePath(FILE_PATH_LITERAL(
96 "net/data/proxy_resolver_perftest"))) {}
98 void RunAllTests() {
99 ASSERT_TRUE(test_server_.Start());
100 for (size_t i = 0; i < arraysize(kPerfTests); ++i) {
101 const PacPerfTest& test_data = kPerfTests[i];
102 RunTest(test_data.pac_name,
103 test_data.queries,
104 test_data.NumQueries());
108 private:
109 void RunTest(const std::string& script_name,
110 const PacQuery* queries,
111 int queries_len) {
112 scoped_ptr<ProxyResolver> resolver;
113 if (!factory_->expects_pac_bytes()) {
114 GURL pac_url =
115 test_server_.GetURL(std::string("files/") + script_name);
116 int rv = factory_->CreateProxyResolver(
117 ProxyResolverScriptData::FromURL(pac_url), &resolver,
118 CompletionCallback(), nullptr);
119 EXPECT_EQ(OK, rv);
120 } else {
121 resolver = LoadPacScriptAndCreateResolver(script_name);
123 ASSERT_TRUE(resolver);
125 // Do a query to warm things up. In the case of internal-fetch proxy
126 // resolvers, the first resolve will be slow since it has to download
127 // the PAC script.
129 ProxyInfo proxy_info;
130 int result =
131 resolver->GetProxyForURL(GURL("http://www.warmup.com"), &proxy_info,
132 CompletionCallback(), NULL, BoundNetLog());
133 ASSERT_EQ(OK, result);
136 // Start the perf timer.
137 std::string perf_test_name = resolver_name_ + "_" + script_name;
138 base::PerfTimeLogger timer(perf_test_name.c_str());
140 for (int i = 0; i < kNumIterations; ++i) {
141 // Round-robin between URLs to resolve.
142 const PacQuery& query = queries[i % queries_len];
144 // Resolve.
145 ProxyInfo proxy_info;
146 int result =
147 resolver->GetProxyForURL(GURL(query.query_url), &proxy_info,
148 CompletionCallback(), NULL, BoundNetLog());
150 // Check that the result was correct. Note that ToPacString() and
151 // ASSERT_EQ() are fast, so they won't skew the results.
152 ASSERT_EQ(OK, result);
153 ASSERT_EQ(query.expected_result, proxy_info.ToPacString());
156 // Print how long the test ran for.
157 timer.Done();
160 // Read the PAC script from disk and initialize the proxy resolver with it.
161 scoped_ptr<ProxyResolver> LoadPacScriptAndCreateResolver(
162 const std::string& script_name) {
163 base::FilePath path;
164 PathService::Get(base::DIR_SOURCE_ROOT, &path);
165 path = path.AppendASCII("net");
166 path = path.AppendASCII("data");
167 path = path.AppendASCII("proxy_resolver_perftest");
168 path = path.AppendASCII(script_name);
170 // Try to read the file from disk.
171 std::string file_contents;
172 bool ok = base::ReadFileToString(path, &file_contents);
174 // If we can't load the file from disk, something is misconfigured.
175 LOG_IF(ERROR, !ok) << "Failed to read file: " << path.value();
176 if (!ok)
177 return nullptr;
179 // Load the PAC script into the ProxyResolver.
180 scoped_ptr<ProxyResolver> resolver;
181 int rv = factory_->CreateProxyResolver(
182 ProxyResolverScriptData::FromUTF8(file_contents), &resolver,
183 CompletionCallback(), nullptr);
184 EXPECT_EQ(OK, rv);
185 return resolver;
188 ProxyResolverFactory* factory_;
189 std::string resolver_name_;
190 SpawnedTestServer test_server_;
193 #if defined(OS_WIN)
194 TEST(ProxyResolverPerfTest, ProxyResolverWinHttp) {
195 ProxyResolverFactoryWinHttp factory;
196 PacPerfSuiteRunner runner(&factory, "ProxyResolverWinHttp");
197 runner.RunAllTests();
199 #elif defined(OS_MACOSX)
200 TEST(ProxyResolverPerfTest, ProxyResolverMac) {
201 ProxyResolverFactoryMac factory;
202 PacPerfSuiteRunner runner(&factory, "ProxyResolverMac");
203 runner.RunAllTests();
205 #endif
207 class MockJSBindings : public ProxyResolverV8::JSBindings {
208 public:
209 MockJSBindings() {}
211 void Alert(const base::string16& message) override { CHECK(false); }
213 bool ResolveDns(const std::string& host,
214 ResolveDnsOperation op,
215 std::string* output,
216 bool* terminate) override {
217 CHECK(false);
218 return false;
221 void OnError(int line_number, const base::string16& message) override {
222 CHECK(false);
226 class ProxyResolverV8Factory : public LegacyProxyResolverFactory {
227 public:
228 ProxyResolverV8Factory() : LegacyProxyResolverFactory(true) {}
229 scoped_ptr<ProxyResolver> CreateProxyResolver() override {
230 scoped_ptr<ProxyResolverV8> resolver(new ProxyResolverV8);
231 resolver->set_js_bindings(&js_bindings_);
232 return resolver.Pass();
235 private:
236 MockJSBindings js_bindings_;
239 TEST(ProxyResolverPerfTest, ProxyResolverV8) {
240 ProxyResolverV8Factory factory;
241 PacPerfSuiteRunner runner(&factory, "ProxyResolverV8");
242 runner.RunAllTests();
245 } // namespace
247 } // namespace net