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/file_util.h"
8 #include "base/path_service.h"
9 #include "base/perftimer.h"
10 #include "base/string_util.h"
11 #include "net/base/mock_host_resolver.h"
12 #include "net/base/net_errors.h"
13 #include "net/proxy/proxy_info.h"
14 #include "net/proxy/proxy_resolver_js_bindings.h"
15 #include "net/proxy/proxy_resolver_v8.h"
16 #include "net/proxy/sync_host_resolver.h"
17 #include "net/test/test_server.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 #include "net/proxy/proxy_resolver_winhttp.h"
22 #elif defined(OS_MACOSX)
23 #include "net/proxy/proxy_resolver_mac.h"
26 class MockSyncHostResolver
: public net::SyncHostResolver
{
28 virtual int Resolve(const net::HostResolver::RequestInfo
& info
,
29 net::AddressList
* addresses
,
30 const net::BoundNetLog
& net_log
) OVERRIDE
{
31 return net::ERR_NAME_NOT_RESOLVED
;
34 virtual void Shutdown() OVERRIDE
{}
37 // This class holds the URL to use for resolving, and the expected result.
38 // We track the expected result in order to make sure the performance
39 // test is actually resolving URLs properly, otherwise the perf numbers
40 // are meaningless :-)
42 const char* query_url
;
43 const char* expected_result
;
46 // Entry listing which PAC scripts to load, and which URLs to try resolving.
47 // |queries| should be terminated by {NULL, NULL}. A sentinel is used
48 // rather than a length, to simplify using initializer lists.
51 PacQuery queries
[100];
53 // Returns the actual number of entries in |queries| (assumes NULL sentinel).
54 int NumQueries() const;
57 // List of performance tests.
58 static PacPerfTest kPerfTests
[] = {
59 // This test uses an ad-blocker PAC script. This script is very heavily
60 // regular expression oriented, and has no dependencies on the current
61 // IP address, or DNS resolving of hosts.
64 {"http://www.google.com", "DIRECT"},
65 {"http://www.imdb.com/photos/cmsicons/x", "PROXY 0.0.0.0:3421"},
66 {"http://www.imdb.com/x", "DIRECT"},
67 {"http://www.staples.com/", "DIRECT"},
68 {"http://www.staples.com/pixeltracker/x", "PROXY 0.0.0.0:3421"},
69 {"http://www.staples.com/pixel/x", "DIRECT"},
70 {"http://www.foobar.com", "DIRECT"},
71 {"http://www.foobarbaz.com/x/y/z", "DIRECT"},
72 {"http://www.testurl1.com/index.html", "DIRECT"},
73 {"http://www.testurl2.com", "DIRECT"},
74 {"https://www.sample/pirate/arrrrrr", "DIRECT"},
80 int PacPerfTest::NumQueries() const {
81 for (size_t i
= 0; i
< arraysize(queries
); ++i
) {
82 if (queries
[i
].query_url
== NULL
)
85 NOTREACHED(); // Bad definition.
89 // The number of URLs to resolve when testing a PAC script.
90 const int kNumIterations
= 500;
92 // Helper class to run through all the performance tests using the specified
93 // proxy resolver implementation.
94 class PacPerfSuiteRunner
{
96 // |resolver_name| is the label used when logging the results.
97 PacPerfSuiteRunner(net::ProxyResolver
* resolver
,
98 const std::string
& resolver_name
)
99 : resolver_(resolver
),
100 resolver_name_(resolver_name
),
102 net::TestServer::TYPE_HTTP
,
103 net::TestServer::kLocalhost
,
104 FilePath(FILE_PATH_LITERAL("net/data/proxy_resolver_perftest"))) {
108 ASSERT_TRUE(test_server_
.Start());
109 for (size_t i
= 0; i
< arraysize(kPerfTests
); ++i
) {
110 const PacPerfTest
& test_data
= kPerfTests
[i
];
111 RunTest(test_data
.pac_name
,
113 test_data
.NumQueries());
118 void RunTest(const std::string
& script_name
,
119 const PacQuery
* queries
,
121 if (!resolver_
->expects_pac_bytes()) {
123 test_server_
.GetURL(std::string("files/") + script_name
);
124 int rv
= resolver_
->SetPacScript(
125 net::ProxyResolverScriptData::FromURL(pac_url
),
126 net::CompletionCallback());
127 EXPECT_EQ(net::OK
, rv
);
129 LoadPacScriptIntoResolver(script_name
);
132 // Do a query to warm things up. In the case of internal-fetch proxy
133 // resolvers, the first resolve will be slow since it has to download
136 net::ProxyInfo proxy_info
;
137 int result
= resolver_
->GetProxyForURL(
138 GURL("http://www.warmup.com"), &proxy_info
, net::CompletionCallback(),
139 NULL
, net::BoundNetLog());
140 ASSERT_EQ(net::OK
, result
);
143 // Start the perf timer.
144 std::string perf_test_name
= resolver_name_
+ "_" + script_name
;
145 PerfTimeLogger
timer(perf_test_name
.c_str());
147 for (int i
= 0; i
< kNumIterations
; ++i
) {
148 // Round-robin between URLs to resolve.
149 const PacQuery
& query
= queries
[i
% queries_len
];
152 net::ProxyInfo proxy_info
;
153 int result
= resolver_
->GetProxyForURL(
154 GURL(query
.query_url
), &proxy_info
, net::CompletionCallback(), NULL
,
157 // Check that the result was correct. Note that ToPacString() and
158 // ASSERT_EQ() are fast, so they won't skew the results.
159 ASSERT_EQ(net::OK
, result
);
160 ASSERT_EQ(query
.expected_result
, proxy_info
.ToPacString());
163 // Print how long the test ran for.
167 // Read the PAC script from disk and initialize the proxy resolver with it.
168 void LoadPacScriptIntoResolver(const std::string
& script_name
) {
170 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
171 path
= path
.AppendASCII("net");
172 path
= path
.AppendASCII("data");
173 path
= path
.AppendASCII("proxy_resolver_perftest");
174 path
= path
.AppendASCII(script_name
);
176 // Try to read the file from disk.
177 std::string file_contents
;
178 bool ok
= file_util::ReadFileToString(path
, &file_contents
);
180 // If we can't load the file from disk, something is misconfigured.
181 LOG_IF(ERROR
, !ok
) << "Failed to read file: " << path
.value();
184 // Load the PAC script into the ProxyResolver.
185 int rv
= resolver_
->SetPacScript(
186 net::ProxyResolverScriptData::FromUTF8(file_contents
),
187 net::CompletionCallback());
188 EXPECT_EQ(net::OK
, rv
);
191 net::ProxyResolver
* resolver_
;
192 std::string resolver_name_
;
193 net::TestServer test_server_
;
197 TEST(ProxyResolverPerfTest
, ProxyResolverWinHttp
) {
198 net::ProxyResolverWinHttp resolver
;
199 PacPerfSuiteRunner
runner(&resolver
, "ProxyResolverWinHttp");
200 runner
.RunAllTests();
202 #elif defined(OS_MACOSX)
203 TEST(ProxyResolverPerfTest
, ProxyResolverMac
) {
204 net::ProxyResolverMac resolver
;
205 PacPerfSuiteRunner
runner(&resolver
, "ProxyResolverMac");
206 runner
.RunAllTests();
210 TEST(ProxyResolverPerfTest
, ProxyResolverV8
) {
211 net::ProxyResolverJSBindings
* js_bindings
=
212 net::ProxyResolverJSBindings::CreateDefault(
213 new MockSyncHostResolver
, NULL
, NULL
);
215 net::ProxyResolverV8
resolver(js_bindings
);
216 PacPerfSuiteRunner
runner(&resolver
, "ProxyResolverV8");
217 runner
.RunAllTests();