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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
7 #include "base/memory/weak_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/test/perf_time_logger.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "net/base/test_completion_callback.h"
16 #include "net/log/net_log_unittest.h"
17 #include "net/test/net_test_suite.h"
18 #include "net/udp/udp_client_socket.h"
19 #include "net/udp/udp_server_socket.h"
20 #include "net/udp/udp_socket.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "testing/platform_test.h"
26 class UDPSocketPerfTest
: public PlatformTest
{
29 : buffer_(new net::IOBufferWithSize(kPacketSize
)), weak_factory_(this) {}
31 void DoneWritePacketsToSocket(net::UDPClientSocket
* socket
,
33 base::Closure done_callback
,
35 WritePacketsToSocket(socket
, num_of_packets
, done_callback
);
38 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done.
39 void WritePacketsToSocket(net::UDPClientSocket
* socket
,
41 base::Closure done_callback
);
43 // Use non-blocking IO if |use_nonblocking_io| is true. This variable only
44 // has effect on Windows.
45 void WriteBenchmark(bool use_nonblocking_io
);
48 static const int kPacketSize
= 1024;
49 scoped_refptr
<net::IOBufferWithSize
> buffer_
;
50 base::WeakPtrFactory
<UDPSocketPerfTest
> weak_factory_
;
53 // Creates and address from an ip/port and returns it in |address|.
54 void CreateUDPAddress(std::string ip_str
,
56 net::IPEndPoint
* address
) {
57 net::IPAddressNumber ip_number
;
58 bool rv
= net::ParseIPLiteralToNumber(ip_str
, &ip_number
);
61 *address
= net::IPEndPoint(ip_number
, port
);
64 void UDPSocketPerfTest::WritePacketsToSocket(net::UDPClientSocket
* socket
,
66 base::Closure done_callback
) {
67 scoped_refptr
<net::IOBufferWithSize
> io_buffer(
68 new net::IOBufferWithSize(kPacketSize
));
69 memset(io_buffer
->data(), 'G', kPacketSize
);
71 while (num_of_packets
) {
73 socket
->Write(io_buffer
.get(), io_buffer
->size(),
74 base::Bind(&UDPSocketPerfTest::DoneWritePacketsToSocket
,
75 weak_factory_
.GetWeakPtr(), socket
,
76 num_of_packets
- 1, done_callback
));
77 if (rv
== net::ERR_IO_PENDING
)
81 if (!num_of_packets
) {
87 void UDPSocketPerfTest::WriteBenchmark(bool use_nonblocking_io
) {
88 base::MessageLoopForIO message_loop
;
89 const uint16 kPort
= 9999;
91 // Setup the server to listen.
92 net::IPEndPoint bind_address
;
93 CreateUDPAddress("127.0.0.1", kPort
, &bind_address
);
94 net::CapturingNetLog server_log
;
95 scoped_ptr
<net::UDPServerSocket
> server(
96 new net::UDPServerSocket(&server_log
, net::NetLog::Source()));
98 if (use_nonblocking_io
)
99 server
->UseNonBlockingIO();
101 int rv
= server
->Listen(bind_address
);
102 ASSERT_EQ(net::OK
, rv
);
105 net::IPEndPoint server_address
;
106 CreateUDPAddress("127.0.0.1", kPort
, &server_address
);
107 net::CapturingNetLog client_log
;
108 scoped_ptr
<net::UDPClientSocket
> client(new net::UDPClientSocket(
109 net::DatagramSocket::DEFAULT_BIND
, net::RandIntCallback(), &client_log
,
110 net::NetLog::Source()));
112 if (use_nonblocking_io
)
113 client
->UseNonBlockingIO();
115 rv
= client
->Connect(server_address
);
116 EXPECT_EQ(net::OK
, rv
);
118 base::RunLoop run_loop
;
119 base::TimeTicks start_ticks
= base::TimeTicks::Now();
120 int packets
= 100000;
121 client
->SetSendBufferSize(1024);
122 WritePacketsToSocket(client
.get(), packets
, run_loop
.QuitClosure());
125 double elapsed
= (base::TimeTicks::Now() - start_ticks
).InSecondsF();
126 LOG(INFO
) << "Write speed: " << packets
/ 1024 / elapsed
<< " MB/s";
129 TEST_F(UDPSocketPerfTest
, Write
) {
130 base::PerfTimeLogger
timer("UDP_socket_write");
131 WriteBenchmark(false);
135 TEST_F(UDPSocketPerfTest
, WriteNonBlocking
) {
136 base::PerfTimeLogger
timer("UDP_socket_write_nonblocking");
137 WriteBenchmark(true);