| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 "base/perftimer.h" |
| 6 #include "net/proxy/proxy_resolver_v8.h" |
| 7 #include "net/url_request/url_request_unittest.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 #if defined(OS_WIN) |
| 11 #include "net/proxy/proxy_resolver_winhttp.h" |
| 12 #elif defined(OS_MACOSX) |
| 13 #include "net/proxy/proxy_resolver_mac.h" |
| 14 #endif |
| 15 |
| 16 // This class holds the URL to use for resolving, and the expected result. |
| 17 // We track the expected result in order to make sure the performance |
| 18 // test is actually resolving URLs properly, otherwise the perf numbers |
| 19 // are meaningless :-) |
| 20 struct PacQuery { |
| 21 const char* query_url; |
| 22 const char* expected_result; |
| 23 }; |
| 24 |
| 25 // Entry listing which PAC scripts to load, and which URLs to try resolving. |
| 26 // |queries| should be terminated by {NULL, NULL}. A sentinel is used |
| 27 // rather than a length, to simplify using initializer lists. |
| 28 struct PacPerfTest { |
| 29 const char* pac_name; |
| 30 PacQuery queries[100]; |
| 31 |
| 32 // Returns the actual number of entries in |queries| (assumes NULL sentinel). |
| 33 int NumQueries() const; |
| 34 }; |
| 35 |
| 36 // List of performance tests. |
| 37 static PacPerfTest kPerfTests[] = { |
| 38 // This test uses an ad-blocker PAC script. This script is very heavily |
| 39 // regular expression oriented, and has no dependencies on the current |
| 40 // IP address, or DNS resolving of hosts. |
| 41 { "no-ads.pac", |
| 42 { // queries: |
| 43 {"http://www.google.com", "DIRECT"}, |
| 44 {"http://www.imdb.com/photos/cmsicons/x", "PROXY 0.0.0.0:3421"}, |
| 45 {"http://www.imdb.com/x", "DIRECT"}, |
| 46 {"http://www.staples.com/", "DIRECT"}, |
| 47 {"http://www.staples.com/pixeltracker/x", "PROXY 0.0.0.0:3421"}, |
| 48 {"http://www.staples.com/pixel/x", "DIRECT"}, |
| 49 {"http://www.foobar.com", "DIRECT"}, |
| 50 {"http://www.foobarbaz.com/x/y/z", "DIRECT"}, |
| 51 {"http://www.testurl1.com/index.html", "DIRECT"}, |
| 52 {"http://www.testurl2.com", "DIRECT"}, |
| 53 {"https://www.sample/pirate/arrrrrr", "DIRECT"}, |
| 54 {NULL, NULL} |
| 55 }, |
| 56 }, |
| 57 }; |
| 58 |
| 59 int PacPerfTest::NumQueries() const { |
| 60 for (int i = 0; i < arraysize(queries); ++i) { |
| 61 if (queries[i].query_url == NULL) |
| 62 return i; |
| 63 } |
| 64 NOTREACHED(); // Bad definition. |
| 65 return 0; |
| 66 } |
| 67 |
| 68 // The number of URLs to resolve when testing a PAC script. |
| 69 const int kNumIterations = 500; |
| 70 |
| 71 // Helper class to run through all the performance tests using the specified |
| 72 // proxy resolver implementation. |
| 73 class PacPerfSuiteRunner { |
| 74 public: |
| 75 // |resolver_name| is the label used when logging the results. |
| 76 PacPerfSuiteRunner(net::ProxyResolver* resolver, |
| 77 const std::string& resolver_name) |
| 78 : resolver_(resolver), resolver_name_(resolver_name) { |
| 79 } |
| 80 |
| 81 void RunAllTests() { |
| 82 for (size_t i = 0; i < arraysize(kPerfTests); ++i) { |
| 83 const PacPerfTest& test_data = kPerfTests[i]; |
| 84 RunTest(test_data.pac_name, |
| 85 test_data.queries, |
| 86 test_data.NumQueries()); |
| 87 } |
| 88 } |
| 89 |
| 90 private: |
| 91 void RunTest(const std::string& script_name, |
| 92 const PacQuery* queries, |
| 93 int queries_len) { |
| 94 GURL pac_url; |
| 95 |
| 96 if (resolver_->does_fetch()) { |
| 97 InitHttpServer(); |
| 98 pac_url = server_->TestServerPage(std::string("files/") + script_name); |
| 99 } else { |
| 100 LoadPacScriptIntoResolver(script_name); |
| 101 } |
| 102 |
| 103 // Do a query to warm things up. In the case of internal-fetch proxy |
| 104 // resolvers, the first resolve will be slow since it has to download |
| 105 // the PAC script. |
| 106 { |
| 107 net::ProxyInfo proxy_info; |
| 108 int result = resolver_->GetProxyForURL( |
| 109 GURL("http://www.warmup.com"), pac_url, &proxy_info); |
| 110 ASSERT_EQ(net::OK, result); |
| 111 } |
| 112 |
| 113 // Start the perf timer. |
| 114 std::string perf_test_name = resolver_name_ + "_" + script_name; |
| 115 PerfTimeLogger timer(perf_test_name.c_str()); |
| 116 |
| 117 for (int i = 0; i < kNumIterations; ++i) { |
| 118 // Round-robin between URLs to resolve. |
| 119 const PacQuery& query = queries[i % queries_len]; |
| 120 |
| 121 // Resolve. |
| 122 net::ProxyInfo proxy_info; |
| 123 int result = resolver_->GetProxyForURL(GURL(query.query_url), |
| 124 pac_url, |
| 125 &proxy_info); |
| 126 |
| 127 // Check that the result was correct. Note that ToPacString() and |
| 128 // ASSERT_EQ() are fast, so they won't skew the results. |
| 129 ASSERT_EQ(net::OK, result); |
| 130 ASSERT_EQ(query.expected_result, proxy_info.ToPacString()); |
| 131 } |
| 132 |
| 133 // Print how long the test ran for. |
| 134 timer.Done(); |
| 135 } |
| 136 |
| 137 // Lazily startup an HTTP server (to serve the PAC script). |
| 138 void InitHttpServer() { |
| 139 DCHECK(resolver_->does_fetch()); |
| 140 if (!server_) { |
| 141 server_ = HTTPTestServer::CreateServer( |
| 142 L"net/data/proxy_resolver_perftest", NULL); |
| 143 } |
| 144 ASSERT_TRUE(server_.get() != NULL); |
| 145 } |
| 146 |
| 147 // Read the PAC script from disk and initialize the proxy resolver with it. |
| 148 void LoadPacScriptIntoResolver(const std::string& script_name) { |
| 149 FilePath path; |
| 150 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 151 path = path.AppendASCII("net"); |
| 152 path = path.AppendASCII("data"); |
| 153 path = path.AppendASCII("proxy_resolver_perftest"); |
| 154 path = path.AppendASCII(script_name); |
| 155 |
| 156 // Try to read the file from disk. |
| 157 std::string file_contents; |
| 158 bool ok = file_util::ReadFileToString(path, &file_contents); |
| 159 |
| 160 // If we can't load the file from disk, something is misconfigured. |
| 161 LOG_IF(ERROR, !ok) << "Failed to read file: " << path.value(); |
| 162 ASSERT_TRUE(ok); |
| 163 |
| 164 // Load the PAC script into the ProxyResolver. |
| 165 resolver_->SetPacScript(file_contents); |
| 166 } |
| 167 |
| 168 net::ProxyResolver* resolver_; |
| 169 std::string resolver_name_; |
| 170 scoped_refptr<HTTPTestServer> server_; |
| 171 }; |
| 172 |
| 173 #if defined(OS_WIN) |
| 174 TEST(ProxyResolverPerfTest, ProxyResolverWinHttp) { |
| 175 net::ProxyResolverWinHttp resolver; |
| 176 PacPerfSuiteRunner runner(&resolver, "ProxyResolverWinHttp"); |
| 177 runner.RunAllTests(); |
| 178 } |
| 179 #elif defined(OS_MACOSX) |
| 180 TEST(ProxyResolverPerfTest, ProxyResolverMac) { |
| 181 net::ProxyResolverMac resolver; |
| 182 PacPerfSuiteRunner runner(&resolver, "ProxyResolverMac"); |
| 183 runner.RunAllTests(); |
| 184 } |
| 185 #endif |
| 186 |
| 187 TEST(ProxyResolverPerfTest, ProxyResolverV8) { |
| 188 net::ProxyResolverV8 resolver; |
| 189 PacPerfSuiteRunner runner(&resolver, "ProxyResolverV8"); |
| 190 runner.RunAllTests(); |
| 191 } |
| OLD | NEW |