Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(392)

Side by Side Diff: net/url_request/url_request_quic_perftest.cc

Issue 2875083002: Add url_request_quic_perftest.cc (Closed)
Patch Set: fix deps Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« net/BUILD.gn ('K') | « net/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 <inttypes.h>
6
7 #include <memory>
8
9 #include "base/files/file_path.h"
10 #include "base/json/json_writer.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/time/time.h"
17 #include "base/trace_event/memory_dump_request_args.h"
18 #include "base/trace_event/process_memory_dump.h"
19 #include "base/trace_event/trace_event_argument.h"
20 #include "net/base/load_timing_info.h"
21 #include "net/cert/mock_cert_verifier.h"
22 #include "net/dns/mapped_host_resolver.h"
23 #include "net/dns/mock_host_resolver.h"
24 #include "net/http/http_status_code.h"
25 #include "net/quic/chromium/crypto/proof_source_chromium.h"
26 #include "net/quic/test_tools/crypto_test_utils.h"
27 #include "net/test/cert_test_util.h"
28 #include "net/test/embedded_test_server/embedded_test_server.h"
29 #include "net/test/embedded_test_server/http_response.h"
30 #include "net/test/gtest_util.h"
31 #include "net/test/test_data_directory.h"
32 #include "net/tools/quic/quic_dispatcher.h"
33 #include "net/tools/quic/quic_http_response_cache.h"
34 #include "net/tools/quic/quic_simple_server.h"
35 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
36 #include "net/url_request/url_request.h"
37 #include "net/url_request/url_request_test_util.h"
38 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "testing/perf/perf_test.h"
41 #include "url/gurl.h"
42
43 namespace net {
44
45 namespace {
46
47 const int kAltSvcPort = 6121;
48 const char kOriginHost[] = "mail.example.com";
49 const char kAltSvcHost[] = "test.example.com";
50 // Used as a simple response from the server.
51 const char kHelloPath[] = "/hello.txt";
52 const char kHelloAltSvcResponse[] = "Hello from QUIC Server";
53 const char kHelloOriginResponse[] = "Hello from TCP Server";
54 const int kHelloStatus = 200;
55
56 std::unique_ptr<test_server::HttpResponse> HandleRequest(
57 const test_server::HttpRequest& request) {
58 std::unique_ptr<test_server::BasicHttpResponse> http_response(
59 new test_server::BasicHttpResponse());
60 http_response->AddCustomHeader(
61 "Alt-Svc", base::StringPrintf(
62 "quic=\"%s:%d\"; v=\"%u\"", kAltSvcHost, kAltSvcPort,
63 HttpNetworkSession::Params().quic_supported_versions[0]));
64 http_response->set_code(HTTP_OK);
65 http_response->set_content(kHelloOriginResponse);
66 http_response->set_content_type("text/plain");
67 return std::move(http_response);
68 }
69
70 void PrintPerfTest(const std::string& name,
71 int value,
72 const std::string& unit) {
73 const ::testing::TestInfo* test_info =
74 ::testing::UnitTest::GetInstance()->current_test_info();
75 perf_test::PrintResult(test_info->test_case_name(),
76 std::string(".") + test_info->name(), name,
77 static_cast<double>(value), unit, true);
78 }
79
80 class URLRequestQuicPerfTest : public ::testing::Test {
81 protected:
82 URLRequestQuicPerfTest()
83 : message_loop_(new base::MessageLoopForIO()),
84 context_(new TestURLRequestContext(true)) {
85 StartTcpServer();
86 StartQuicServer();
87
88 // Host mapping.
89 std::unique_ptr<MockHostResolver> resolver(new MockHostResolver());
90 resolver->rules()->AddRule(kAltSvcHost, "127.0.0.1");
91 host_resolver_.reset(new MappedHostResolver(std::move(resolver)));
92 std::string map_rule = base::StringPrintf("MAP %s 127.0.0.1:%d",
93 kOriginHost, tcp_server_->port());
94 EXPECT_TRUE(host_resolver_->AddRuleFromString(map_rule));
95
96 std::unique_ptr<HttpNetworkSession::Params> params(
97 new HttpNetworkSession::Params);
98 params->cert_verifier = &cert_verifier_;
99 params->enable_quic = true;
100 params->enable_user_alternate_protocol_ports = true;
101 context_->set_host_resolver(host_resolver_.get());
102 context_->set_http_network_session_params(std::move(params));
103 context_->set_cert_verifier(&cert_verifier_);
104 context_->Init();
105 }
106
107 void TearDown() override {
108 if (quic_server_) {
109 quic_server_->Shutdown();
110 // If possible, deliver the conncetion close packet to the client before
111 // destruct the TestURLRequestContext.
112 base::RunLoop().RunUntilIdle();
113 }
114 message_loop_.reset();
115 }
116
117 std::unique_ptr<URLRequest> CreateRequest(const GURL& url,
118 RequestPriority priority,
119 URLRequest::Delegate* delegate) {
120 return context_->CreateRequest(url, priority, delegate,
121 TRAFFIC_ANNOTATION_FOR_TESTS);
122 }
123
124 URLRequestContext* context() const { return context_.get(); }
125
126 private:
127 void StartQuicServer() {
128 net::QuicConfig config;
129 response_cache_.AddSimpleResponse(kOriginHost, kHelloPath, kHelloStatus,
130 kHelloAltSvcResponse);
131 quic_server_.reset(new QuicSimpleServer(
132 test::crypto_test_utils::ProofSourceForTesting(), config,
133 net::QuicCryptoServerConfig::ConfigOptions(), AllSupportedVersions(),
134 &response_cache_));
135 int rv = quic_server_->Listen(
136 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kAltSvcPort));
137 ASSERT_GE(rv, 0) << "Quic server fails to start";
138
139 CertVerifyResult verify_result;
140 verify_result.verified_cert = ImportCertFromFile(
141 GetTestCertsDirectory(), "quic_test.example.com.crt");
142 cert_verifier_.AddResultForCert(verify_result.verified_cert.get(),
143 verify_result, OK);
144 }
145
146 void StartTcpServer() {
147 tcp_server_ = base::MakeUnique<EmbeddedTestServer>(
148 net::EmbeddedTestServer::TYPE_HTTPS);
149 tcp_server_->RegisterRequestHandler(base::Bind(&HandleRequest));
150 ASSERT_TRUE(tcp_server_->Start()) << "HTTP/1.1 server fails to start";
151
152 CertVerifyResult verify_result;
153 verify_result.verified_cert = tcp_server_->GetCertificate();
154 cert_verifier_.AddResultForCert(tcp_server_->GetCertificate(),
155 verify_result, OK);
156 }
157
158 std::unique_ptr<MappedHostResolver> host_resolver_;
159 std::unique_ptr<EmbeddedTestServer> tcp_server_;
160 std::unique_ptr<QuicSimpleServer> quic_server_;
161 std::unique_ptr<base::MessageLoop> message_loop_;
162 std::unique_ptr<TestURLRequestContext> context_;
163 QuicHttpResponseCache response_cache_;
164 MockCertVerifier cert_verifier_;
165 };
166
167 } // namespace
168
169 TEST_F(URLRequestQuicPerfTest, TestGetRequest) {
170 bool quic_succeeded = false;
171 GURL url(base::StringPrintf("https://%s%s", kOriginHost, kHelloPath));
172 base::TimeTicks start = base::TimeTicks::Now();
173 for (int i = 0; i < 1000; ++i) {
174 TestDelegate delegate;
175 std::unique_ptr<URLRequest> request =
176 CreateRequest(url, DEFAULT_PRIORITY, &delegate);
177
178 request->Start();
179 EXPECT_TRUE(request->is_pending());
180 base::RunLoop().Run();
181
182 EXPECT_TRUE(request->status().is_success());
183 if (delegate.data_received() == kHelloAltSvcResponse) {
184 quic_succeeded = true;
185 } else {
186 EXPECT_EQ(kHelloOriginResponse, delegate.data_received());
187 }
188 }
189 base::TimeTicks end = base::TimeTicks::Now();
190 PrintPerfTest("time", (end - start).InMilliseconds(), "count");
nednguyen 2017/05/25 23:57:40 umm, shouldn't the unit here be "ms"? Also what is
xunjieli 2017/05/26 01:15:58 Done. The result ranges from 3600ms - 4500ms for
191
192 EXPECT_TRUE(quic_succeeded);
193 base::trace_event::MemoryDumpArgs dump_args = {
194 base::trace_event::MemoryDumpLevelOfDetail::DETAILED};
195 std::unique_ptr<base::trace_event::ProcessMemoryDump> process_memory_dump(
nednguyen 2017/05/25 23:57:40 not sure this is the good API to get memory dump i
196 new base::trace_event::ProcessMemoryDump(nullptr, dump_args));
197 context()->OnMemoryDump(dump_args, process_memory_dump.get());
198 const base::trace_event::MemoryAllocatorDump* url_request_context_dump =
199 process_memory_dump->GetAllocatorDump(
200 base::StringPrintf("net/url_request_context/unknown/0x%" PRIxPTR,
201 reinterpret_cast<uintptr_t>(context())));
202 ASSERT_NE(nullptr, url_request_context_dump);
203 auto raw_attrs =
204 url_request_context_dump->attributes_for_testing()->ToBaseValue();
205 std::string json;
206 base::JSONWriter::Write(*raw_attrs.get(), &json);
207 DVLOG(1) << json;
208 base::DictionaryValue* attrs;
209 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs));
210 base::DictionaryValue* object_count_attrs;
211 ASSERT_TRUE(attrs->GetDictionary(
212 base::trace_event::MemoryAllocatorDump::kNameObjectCount,
213 &object_count_attrs));
214 std::string object_count_str;
215 ASSERT_TRUE(object_count_attrs->GetString("value", &object_count_str));
216 EXPECT_EQ("0", object_count_str);
217
218 // HttpStreamFactory
219 const base::trace_event::MemoryAllocatorDump* http_stream_factory_dump =
220 process_memory_dump->GetAllocatorDump(base::StringPrintf(
221 "net/http_network_session_%p/stream_factory",
222 context()->http_transaction_factory()->GetSession()));
223 EXPECT_EQ(nullptr, http_stream_factory_dump);
224
225 // QuicStreamFactory
226 const base::trace_event::MemoryAllocatorDump* quic_stream_factory_dump =
227 process_memory_dump->GetAllocatorDump(base::StringPrintf(
228 "net/http_network_session_%p/quic_stream_factory",
229 context()->http_transaction_factory()->GetSession()));
230 ASSERT_NE(nullptr, quic_stream_factory_dump);
231
232 raw_attrs = quic_stream_factory_dump->attributes_for_testing()->ToBaseValue();
233 base::JSONWriter::Write(*raw_attrs.get(), &json);
nednguyen 2017/05/25 23:57:40 I think we shouldn't need to manually do these jso
xunjieli 2017/05/26 01:15:58 This JSON parsing was mostly for debugging. Remove
234 DVLOG(1) << json;
235 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs));
236 ASSERT_TRUE(attrs->GetDictionary("active_jobs", &object_count_attrs));
237 ASSERT_TRUE(object_count_attrs->GetString("value", &object_count_str));
238 EXPECT_EQ("0", object_count_str);
239 int object_count = -1;
240 ASSERT_TRUE(base::HexStringToInt(object_count_str, &object_count));
nednguyen 2017/05/25 23:57:40 This is a mixed of perf tests & correctness test?
xunjieli 2017/05/26 01:15:58 Yes. I used ASSERT for fatal errors -- when we fai
241 PrintPerfTest("active_quic_jobs", object_count, "count");
242
243 ASSERT_TRUE(attrs->GetDictionary("all_sessions", &object_count_attrs));
244 ASSERT_TRUE(object_count_attrs->GetString("value", &object_count_str));
245 EXPECT_EQ("1", object_count_str);
246 ASSERT_TRUE(base::HexStringToInt(object_count_str, &object_count));
247 PrintPerfTest("active_quic_sessions", object_count, "count");
248 }
249
250 } // namespace net
OLDNEW
« net/BUILD.gn ('K') | « net/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698