Chromium Code Reviews| Index: net/url_request/url_request_quic_perftest.cc |
| diff --git a/net/url_request/url_request_quic_perftest.cc b/net/url_request/url_request_quic_perftest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..340cd5189fefcc70d89c097bf466c28fc977eaa0 |
| --- /dev/null |
| +++ b/net/url_request/url_request_quic_perftest.cc |
| @@ -0,0 +1,244 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <inttypes.h> |
| + |
| +#include <memory> |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/time/time.h" |
| +#include "base/trace_event/memory_dump_request_args.h" |
| +#include "base/trace_event/process_memory_dump.h" |
| +#include "base/trace_event/trace_event_argument.h" |
| +#include "net/base/load_timing_info.h" |
| +#include "net/cert/mock_cert_verifier.h" |
| +#include "net/dns/mapped_host_resolver.h" |
| +#include "net/dns/mock_host_resolver.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/quic/chromium/crypto/proof_source_chromium.h" |
| +#include "net/quic/test_tools/crypto_test_utils.h" |
| +#include "net/test/cert_test_util.h" |
| +#include "net/test/embedded_test_server/embedded_test_server.h" |
| +#include "net/test/embedded_test_server/http_response.h" |
| +#include "net/test/gtest_util.h" |
| +#include "net/test/test_data_directory.h" |
| +#include "net/tools/quic/quic_http_response_cache.h" |
| +#include "net/tools/quic/quic_simple_server.h" |
| +#include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "testing/perf/perf_test.h" |
| +#include "url/gurl.h" |
| + |
| +namespace net { |
| + |
| +namespace { |
| + |
| +const int kAltSvcPort = 6121; |
| +const char kOriginHost[] = "mail.example.com"; |
| +const char kAltSvcHost[] = "test.example.com"; |
| +// Used as a simple response from the server. |
| +const char kHelloPath[] = "/hello.txt"; |
| +const char kHelloAltSvcResponse[] = "Hello from QUIC Server"; |
| +const char kHelloOriginResponse[] = "Hello from TCP Server"; |
| +const int kHelloStatus = 200; |
| + |
| +std::unique_ptr<test_server::HttpResponse> HandleRequest( |
| + const test_server::HttpRequest& request) { |
| + std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| + new test_server::BasicHttpResponse()); |
| + http_response->AddCustomHeader( |
| + "Alt-Svc", base::StringPrintf( |
| + "quic=\"%s:%d\"; v=\"%u\"", kAltSvcHost, kAltSvcPort, |
| + HttpNetworkSession::Params().quic_supported_versions[0])); |
| + http_response->set_code(HTTP_OK); |
| + http_response->set_content(kHelloOriginResponse); |
| + http_response->set_content_type("text/plain"); |
| + return std::move(http_response); |
| +} |
| + |
| +void PrintPerfTest(const std::string& name, |
| + int value, |
| + const std::string& unit) { |
| + const ::testing::TestInfo* test_info = |
| + ::testing::UnitTest::GetInstance()->current_test_info(); |
| + perf_test::PrintResult(test_info->test_case_name(), |
| + std::string(".") + test_info->name(), name, |
| + static_cast<double>(value), unit, true); |
| +} |
| + |
| +class URLRequestQuicPerfTest : public ::testing::Test { |
| + protected: |
| + URLRequestQuicPerfTest() |
| + : message_loop_(new base::MessageLoopForIO()), |
| + context_(new TestURLRequestContext(true)) { |
| + StartTcpServer(); |
| + StartQuicServer(); |
| + |
| + // Host mapping. |
| + std::unique_ptr<MockHostResolver> resolver(new MockHostResolver()); |
| + resolver->rules()->AddRule(kAltSvcHost, "127.0.0.1"); |
| + host_resolver_.reset(new MappedHostResolver(std::move(resolver))); |
| + std::string map_rule = base::StringPrintf("MAP %s 127.0.0.1:%d", |
| + kOriginHost, tcp_server_->port()); |
| + EXPECT_TRUE(host_resolver_->AddRuleFromString(map_rule)); |
| + |
| + std::unique_ptr<HttpNetworkSession::Params> params( |
| + new HttpNetworkSession::Params); |
| + params->cert_verifier = &cert_verifier_; |
| + params->enable_quic = true; |
| + params->enable_user_alternate_protocol_ports = true; |
| + context_->set_host_resolver(host_resolver_.get()); |
| + context_->set_http_network_session_params(std::move(params)); |
| + context_->set_cert_verifier(&cert_verifier_); |
| + context_->Init(); |
| + } |
| + |
| + void TearDown() override { |
| + if (quic_server_) { |
| + quic_server_->Shutdown(); |
| + // If possible, deliver the conncetion close packet to the client before |
| + // destruct the TestURLRequestContext. |
| + base::RunLoop().RunUntilIdle(); |
| + } |
| + message_loop_.reset(); |
| + } |
| + |
| + std::unique_ptr<URLRequest> CreateRequest(const GURL& url, |
| + RequestPriority priority, |
| + URLRequest::Delegate* delegate) { |
| + return context_->CreateRequest(url, priority, delegate, |
| + TRAFFIC_ANNOTATION_FOR_TESTS); |
| + } |
| + |
| + URLRequestContext* context() const { return context_.get(); } |
| + |
| + private: |
| + void StartQuicServer() { |
| + net::QuicConfig config; |
| + response_cache_.AddSimpleResponse(kOriginHost, kHelloPath, kHelloStatus, |
| + kHelloAltSvcResponse); |
| + quic_server_.reset(new QuicSimpleServer( |
| + test::crypto_test_utils::ProofSourceForTesting(), config, |
| + net::QuicCryptoServerConfig::ConfigOptions(), AllSupportedVersions(), |
| + &response_cache_)); |
| + int rv = quic_server_->Listen( |
| + net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kAltSvcPort)); |
| + ASSERT_GE(rv, 0) << "Quic server fails to start"; |
| + |
| + CertVerifyResult verify_result; |
| + verify_result.verified_cert = ImportCertFromFile( |
| + GetTestCertsDirectory(), "quic_test.example.com.crt"); |
| + cert_verifier_.AddResultForCert(verify_result.verified_cert.get(), |
| + verify_result, OK); |
| + } |
| + |
| + void StartTcpServer() { |
| + tcp_server_ = base::MakeUnique<EmbeddedTestServer>( |
| + net::EmbeddedTestServer::TYPE_HTTPS); |
| + tcp_server_->RegisterRequestHandler(base::Bind(&HandleRequest)); |
| + ASSERT_TRUE(tcp_server_->Start()) << "HTTP/1.1 server fails to start"; |
| + |
| + CertVerifyResult verify_result; |
| + verify_result.verified_cert = tcp_server_->GetCertificate(); |
| + cert_verifier_.AddResultForCert(tcp_server_->GetCertificate(), |
| + verify_result, OK); |
| + } |
| + |
| + std::unique_ptr<MappedHostResolver> host_resolver_; |
| + std::unique_ptr<EmbeddedTestServer> tcp_server_; |
| + std::unique_ptr<QuicSimpleServer> quic_server_; |
| + std::unique_ptr<base::MessageLoop> message_loop_; |
| + std::unique_ptr<TestURLRequestContext> context_; |
| + QuicHttpResponseCache response_cache_; |
| + MockCertVerifier cert_verifier_; |
| +}; |
| + |
| +} // namespace |
| + |
| +TEST_F(URLRequestQuicPerfTest, TestGetRequest) { |
| + bool quic_succeeded = false; |
| + GURL url(base::StringPrintf("https://%s%s", kOriginHost, kHelloPath)); |
| + base::TimeTicks start = base::TimeTicks::Now(); |
| + const int kNumRequest = 1000; |
| + for (int i = 0; i < kNumRequest; ++i) { |
| + TestDelegate delegate; |
| + std::unique_ptr<URLRequest> request = |
| + CreateRequest(url, DEFAULT_PRIORITY, &delegate); |
| + |
| + request->Start(); |
| + EXPECT_TRUE(request->is_pending()); |
| + base::RunLoop().Run(); |
| + |
| + EXPECT_TRUE(request->status().is_success()); |
| + if (delegate.data_received() == kHelloAltSvcResponse) { |
| + quic_succeeded = true; |
| + } else { |
| + EXPECT_EQ(kHelloOriginResponse, delegate.data_received()); |
| + } |
| + } |
| + base::TimeTicks end = base::TimeTicks::Now(); |
| + PrintPerfTest("time", (end - start).InMilliseconds() / kNumRequest, "ms"); |
| + |
| + EXPECT_TRUE(quic_succeeded); |
| + base::trace_event::MemoryDumpArgs dump_args = { |
| + base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
| + std::unique_ptr<base::trace_event::ProcessMemoryDump> process_memory_dump( |
| + new base::trace_event::ProcessMemoryDump(nullptr, dump_args)); |
| + context()->OnMemoryDump(dump_args, process_memory_dump.get()); |
| + const base::trace_event::MemoryAllocatorDump* url_request_context_dump = |
| + process_memory_dump->GetAllocatorDump( |
| + base::StringPrintf("net/url_request_context/unknown/0x%" PRIxPTR, |
| + reinterpret_cast<uintptr_t>(context()))); |
| + ASSERT_NE(nullptr, url_request_context_dump); |
| + auto raw_attrs = |
| + url_request_context_dump->attributes_for_testing()->ToBaseValue(); |
| + base::DictionaryValue* attrs; |
| + ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); |
| + base::DictionaryValue* object_count_attrs; |
| + ASSERT_TRUE(attrs->GetDictionary( |
| + base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
| + &object_count_attrs)); |
| + std::string object_count_str; |
| + ASSERT_TRUE(object_count_attrs->GetString("value", &object_count_str)); |
| + EXPECT_EQ("0", object_count_str); |
| + |
| + // HttpStreamFactory |
| + const base::trace_event::MemoryAllocatorDump* http_stream_factory_dump = |
| + process_memory_dump->GetAllocatorDump(base::StringPrintf( |
| + "net/http_network_session_%p/stream_factory", |
| + context()->http_transaction_factory()->GetSession())); |
| + EXPECT_EQ(nullptr, http_stream_factory_dump); |
| + |
| + // QuicStreamFactory |
| + const base::trace_event::MemoryAllocatorDump* quic_stream_factory_dump = |
| + process_memory_dump->GetAllocatorDump(base::StringPrintf( |
| + "net/http_network_session_%p/quic_stream_factory", |
| + context()->http_transaction_factory()->GetSession())); |
| + ASSERT_NE(nullptr, quic_stream_factory_dump); |
| + |
| + raw_attrs = quic_stream_factory_dump->attributes_for_testing()->ToBaseValue(); |
| + ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); |
| + ASSERT_TRUE(attrs->GetDictionary("active_jobs", &object_count_attrs)); |
| + ASSERT_TRUE(object_count_attrs->GetString("value", &object_count_str)); |
| + EXPECT_EQ("0", object_count_str); |
| + int object_count = -1; |
| + ASSERT_TRUE(base::HexStringToInt(object_count_str, &object_count)); |
|
nednguyen
2017/05/26 18:38:57
If this test can be enable on CQ, why not change t
xunjieli
2017/05/26 19:13:41
Isn't this already an ASSERT? It shouldn't be flak
nednguyen
2017/05/26 19:15:52
Ooops, wrong line. I mean the line that does: "EXP
xunjieli
2017/05/26 23:57:16
I used EXPECT_EQ() there instead of ASSERT_EQ() be
nednguyen
2017/05/27 00:28:38
Ah I see, as long as the final return code is non
|
| + PrintPerfTest("active_quic_jobs", object_count, "count"); |
| + |
| + ASSERT_TRUE(attrs->GetDictionary("all_sessions", &object_count_attrs)); |
| + ASSERT_TRUE(object_count_attrs->GetString("value", &object_count_str)); |
| + EXPECT_EQ("1", object_count_str); |
| + ASSERT_TRUE(base::HexStringToInt(object_count_str, &object_count)); |
| + PrintPerfTest("active_quic_sessions", object_count, "count"); |
| +} |
| + |
| +} // namespace net |