This source file includes following definitions.
- NumQueries
- test_server_
- RunAllTests
- RunTest
- LoadPacScriptIntoResolver
- TEST
- TEST
- Alert
- ResolveDns
- OnError
- TEST
#include "base/base_paths.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/test/perf_time_logger.h"
#include "net/base/net_errors.h"
#include "net/dns/mock_host_resolver.h"
#include "net/proxy/proxy_info.h"
#include "net/proxy/proxy_resolver_v8.h"
#include "net/test/spawned_test_server/spawned_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN)
#include "net/proxy/proxy_resolver_winhttp.h"
#elif defined(OS_MACOSX)
#include "net/proxy/proxy_resolver_mac.h"
#endif
struct PacQuery {
const char* query_url;
const char* expected_result;
};
struct PacPerfTest {
const char* pac_name;
PacQuery queries[100];
int NumQueries() const;
};
static PacPerfTest kPerfTests[] = {
{ "no-ads.pac",
{
{"http://www.google.com", "DIRECT"},
{"http://www.imdb.com/photos/cmsicons/x", "PROXY 0.0.0.0:3421"},
{"http://www.imdb.com/x", "DIRECT"},
{"http://www.staples.com/", "DIRECT"},
{"http://www.staples.com/pixeltracker/x", "PROXY 0.0.0.0:3421"},
{"http://www.staples.com/pixel/x", "DIRECT"},
{"http://www.foobar.com", "DIRECT"},
{"http://www.foobarbaz.com/x/y/z", "DIRECT"},
{"http://www.testurl1.com/index.html", "DIRECT"},
{"http://www.testurl2.com", "DIRECT"},
{"https://www.sample/pirate/arrrrrr", "DIRECT"},
{NULL, NULL}
},
},
};
int PacPerfTest::NumQueries() const {
for (size_t i = 0; i < arraysize(queries); ++i) {
if (queries[i].query_url == NULL)
return i;
}
NOTREACHED();
return 0;
}
const int kNumIterations = 500;
class PacPerfSuiteRunner {
public:
PacPerfSuiteRunner(net::ProxyResolver* resolver,
const std::string& resolver_name)
: resolver_(resolver),
resolver_name_(resolver_name),
test_server_(
net::SpawnedTestServer::TYPE_HTTP,
net::SpawnedTestServer::kLocalhost,
base::FilePath(
FILE_PATH_LITERAL("net/data/proxy_resolver_perftest"))) {
}
void RunAllTests() {
ASSERT_TRUE(test_server_.Start());
for (size_t i = 0; i < arraysize(kPerfTests); ++i) {
const PacPerfTest& test_data = kPerfTests[i];
RunTest(test_data.pac_name,
test_data.queries,
test_data.NumQueries());
}
}
private:
void RunTest(const std::string& script_name,
const PacQuery* queries,
int queries_len) {
if (!resolver_->expects_pac_bytes()) {
GURL pac_url =
test_server_.GetURL(std::string("files/") + script_name);
int rv = resolver_->SetPacScript(
net::ProxyResolverScriptData::FromURL(pac_url),
net::CompletionCallback());
EXPECT_EQ(net::OK, rv);
} else {
LoadPacScriptIntoResolver(script_name);
}
{
net::ProxyInfo proxy_info;
int result = resolver_->GetProxyForURL(
GURL("http://www.warmup.com"), &proxy_info, net::CompletionCallback(),
NULL, net::BoundNetLog());
ASSERT_EQ(net::OK, result);
}
std::string perf_test_name = resolver_name_ + "_" + script_name;
base::PerfTimeLogger timer(perf_test_name.c_str());
for (int i = 0; i < kNumIterations; ++i) {
const PacQuery& query = queries[i % queries_len];
net::ProxyInfo proxy_info;
int result = resolver_->GetProxyForURL(
GURL(query.query_url), &proxy_info, net::CompletionCallback(), NULL,
net::BoundNetLog());
ASSERT_EQ(net::OK, result);
ASSERT_EQ(query.expected_result, proxy_info.ToPacString());
}
timer.Done();
}
void LoadPacScriptIntoResolver(const std::string& script_name) {
base::FilePath path;
PathService::Get(base::DIR_SOURCE_ROOT, &path);
path = path.AppendASCII("net");
path = path.AppendASCII("data");
path = path.AppendASCII("proxy_resolver_perftest");
path = path.AppendASCII(script_name);
std::string file_contents;
bool ok = base::ReadFileToString(path, &file_contents);
LOG_IF(ERROR, !ok) << "Failed to read file: " << path.value();
ASSERT_TRUE(ok);
int rv = resolver_->SetPacScript(
net::ProxyResolverScriptData::FromUTF8(file_contents),
net::CompletionCallback());
EXPECT_EQ(net::OK, rv);
}
net::ProxyResolver* resolver_;
std::string resolver_name_;
net::SpawnedTestServer test_server_;
};
#if defined(OS_WIN)
TEST(ProxyResolverPerfTest, ProxyResolverWinHttp) {
net::ProxyResolverWinHttp resolver;
PacPerfSuiteRunner runner(&resolver, "ProxyResolverWinHttp");
runner.RunAllTests();
}
#elif defined(OS_MACOSX)
TEST(ProxyResolverPerfTest, ProxyResolverMac) {
net::ProxyResolverMac resolver;
PacPerfSuiteRunner runner(&resolver, "ProxyResolverMac");
runner.RunAllTests();
}
#endif
class MockJSBindings : public net::ProxyResolverV8::JSBindings {
public:
MockJSBindings() {}
virtual void Alert(const base::string16& message) OVERRIDE {
CHECK(false);
}
virtual bool ResolveDns(const std::string& host,
ResolveDnsOperation op,
std::string* output,
bool* terminate) OVERRIDE {
CHECK(false);
return false;
}
virtual void OnError(int line_number,
const base::string16& message) OVERRIDE {
CHECK(false);
}
};
TEST(ProxyResolverPerfTest, ProxyResolverV8) {
net::ProxyResolverV8::RememberDefaultIsolate();
MockJSBindings js_bindings;
net::ProxyResolverV8 resolver;
resolver.set_js_bindings(&js_bindings);
PacPerfSuiteRunner runner(&resolver, "ProxyResolverV8");
runner.RunAllTests();
}