OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/test/perf_time_logger.h" | 9 #include "base/test/perf_time_logger.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
11 #include "net/base/ip_endpoint.h" | 11 #include "net/base/ip_endpoint.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
| 14 #include "net/test/gtest_util.h" |
14 #include "net/test/net_test_suite.h" | 15 #include "net/test/net_test_suite.h" |
15 #include "net/udp/udp_client_socket.h" | 16 #include "net/udp/udp_client_socket.h" |
16 #include "net/udp/udp_server_socket.h" | 17 #include "net/udp/udp_server_socket.h" |
17 #include "net/udp/udp_socket.h" | 18 #include "net/udp/udp_socket.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
20 | 22 |
| 23 using net::test::IsOk; |
| 24 |
21 namespace net { | 25 namespace net { |
22 | 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 class UDPSocketPerfTest : public PlatformTest { | 29 class UDPSocketPerfTest : public PlatformTest { |
26 public: | 30 public: |
27 UDPSocketPerfTest() | 31 UDPSocketPerfTest() |
28 : buffer_(new IOBufferWithSize(kPacketSize)), weak_factory_(this) {} | 32 : buffer_(new IOBufferWithSize(kPacketSize)), weak_factory_(this) {} |
29 | 33 |
30 void DoneWritePacketsToSocket(UDPClientSocket* socket, | 34 void DoneWritePacketsToSocket(UDPClientSocket* socket, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 // Setup the server to listen. | 92 // Setup the server to listen. |
89 IPEndPoint bind_address; | 93 IPEndPoint bind_address; |
90 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | 94 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
91 std::unique_ptr<UDPServerSocket> server( | 95 std::unique_ptr<UDPServerSocket> server( |
92 new UDPServerSocket(nullptr, NetLog::Source())); | 96 new UDPServerSocket(nullptr, NetLog::Source())); |
93 #if defined(OS_WIN) | 97 #if defined(OS_WIN) |
94 if (use_nonblocking_io) | 98 if (use_nonblocking_io) |
95 server->UseNonBlockingIO(); | 99 server->UseNonBlockingIO(); |
96 #endif | 100 #endif |
97 int rv = server->Listen(bind_address); | 101 int rv = server->Listen(bind_address); |
98 ASSERT_EQ(OK, rv); | 102 ASSERT_THAT(rv, IsOk()); |
99 | 103 |
100 // Setup the client. | 104 // Setup the client. |
101 IPEndPoint server_address; | 105 IPEndPoint server_address; |
102 CreateUDPAddress("127.0.0.1", kPort, &server_address); | 106 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
103 std::unique_ptr<UDPClientSocket> client( | 107 std::unique_ptr<UDPClientSocket> client( |
104 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 108 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
105 nullptr, NetLog::Source())); | 109 nullptr, NetLog::Source())); |
106 #if defined(OS_WIN) | 110 #if defined(OS_WIN) |
107 if (use_nonblocking_io) | 111 if (use_nonblocking_io) |
108 client->UseNonBlockingIO(); | 112 client->UseNonBlockingIO(); |
109 #endif | 113 #endif |
110 rv = client->Connect(server_address); | 114 rv = client->Connect(server_address); |
111 EXPECT_EQ(OK, rv); | 115 EXPECT_THAT(rv, IsOk()); |
112 | 116 |
113 base::RunLoop run_loop; | 117 base::RunLoop run_loop; |
114 base::TimeTicks start_ticks = base::TimeTicks::Now(); | 118 base::TimeTicks start_ticks = base::TimeTicks::Now(); |
115 int packets = 100000; | 119 int packets = 100000; |
116 client->SetSendBufferSize(1024); | 120 client->SetSendBufferSize(1024); |
117 WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure()); | 121 WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure()); |
118 run_loop.Run(); | 122 run_loop.Run(); |
119 | 123 |
120 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); | 124 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); |
121 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; | 125 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; |
122 } | 126 } |
123 | 127 |
124 TEST_F(UDPSocketPerfTest, Write) { | 128 TEST_F(UDPSocketPerfTest, Write) { |
125 base::PerfTimeLogger timer("UDP_socket_write"); | 129 base::PerfTimeLogger timer("UDP_socket_write"); |
126 WriteBenchmark(false); | 130 WriteBenchmark(false); |
127 } | 131 } |
128 | 132 |
129 #if defined(OS_WIN) | 133 #if defined(OS_WIN) |
130 TEST_F(UDPSocketPerfTest, WriteNonBlocking) { | 134 TEST_F(UDPSocketPerfTest, WriteNonBlocking) { |
131 base::PerfTimeLogger timer("UDP_socket_write_nonblocking"); | 135 base::PerfTimeLogger timer("UDP_socket_write_nonblocking"); |
132 WriteBenchmark(true); | 136 WriteBenchmark(true); |
133 } | 137 } |
134 #endif | 138 #endif |
135 | 139 |
136 } // namespace | 140 } // namespace |
137 | 141 |
138 } // namespace net | 142 } // namespace net |
OLD | NEW |