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"
20 #include "net/proxy/proxy_resolver_winhttp.h"
21 #elif defined(OS_MACOSX)
22 #include "net/proxy/proxy_resolver_mac.h"
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 :-)
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.
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.
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"},
72 int PacPerfTest::NumQueries() const {
73 for (size_t i
= 0; i
< arraysize(queries
); ++i
) {
74 if (queries
[i
].query_url
== NULL
)
77 NOTREACHED(); // Bad definition.
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
{
88 // |resolver_name| is the label used when logging the results.
89 PacPerfSuiteRunner(ProxyResolverFactory
* factory
,
90 const std::string
& resolver_name
)
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"))) {}
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
,
104 test_data
.NumQueries());
109 void RunTest(const std::string
& script_name
,
110 const PacQuery
* queries
,
112 scoped_ptr
<ProxyResolver
> resolver
;
113 if (!factory_
->expects_pac_bytes()) {
115 test_server_
.GetURL(std::string("files/") + script_name
);
116 int rv
= factory_
->CreateProxyResolver(
117 ProxyResolverScriptData::FromURL(pac_url
), &resolver
,
118 CompletionCallback(), nullptr);
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
129 ProxyInfo proxy_info
;
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
];
145 ProxyInfo proxy_info
;
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.
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
) {
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();
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);
188 ProxyResolverFactory
* factory_
;
189 std::string resolver_name_
;
190 SpawnedTestServer test_server_
;
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();
207 class MockJSBindings
: public ProxyResolverV8::JSBindings
{
211 void Alert(const base::string16
& message
) override
{ CHECK(false); }
213 bool ResolveDns(const std::string
& host
,
214 ResolveDnsOperation op
,
216 bool* terminate
) override
{
221 void OnError(int line_number
, const base::string16
& message
) override
{
226 class ProxyResolverV8Factory
: public LegacyProxyResolverFactory
{
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();
236 MockJSBindings js_bindings_
;
239 TEST(ProxyResolverPerfTest
, ProxyResolverV8
) {
240 ProxyResolverV8Factory factory
;
241 PacPerfSuiteRunner
runner(&factory
, "ProxyResolverV8");
242 runner
.RunAllTests();