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/test/net_test_suite.h"
17 #include "net/udp/udp_client_socket.h"
18 #include "net/udp/udp_server_socket.h"
19 #include "net/udp/udp_socket.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/platform_test.h"
27 class UDPSocketPerfTest
: public PlatformTest
{
30 : buffer_(new IOBufferWithSize(kPacketSize
)), weak_factory_(this) {}
32 void DoneWritePacketsToSocket(UDPClientSocket
* socket
,
34 base::Closure done_callback
,
36 WritePacketsToSocket(socket
, num_of_packets
, done_callback
);
39 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done.
40 void WritePacketsToSocket(UDPClientSocket
* socket
,
42 base::Closure done_callback
);
44 // Use non-blocking IO if |use_nonblocking_io| is true. This variable only
45 // has effect on Windows.
46 void WriteBenchmark(bool use_nonblocking_io
);
49 static const int kPacketSize
= 1024;
50 scoped_refptr
<IOBufferWithSize
> buffer_
;
51 base::WeakPtrFactory
<UDPSocketPerfTest
> weak_factory_
;
54 // Creates and address from an ip/port and returns it in |address|.
55 void CreateUDPAddress(std::string ip_str
, uint16 port
, IPEndPoint
* address
) {
56 IPAddressNumber ip_number
;
57 bool rv
= ParseIPLiteralToNumber(ip_str
, &ip_number
);
60 *address
= IPEndPoint(ip_number
, port
);
63 void UDPSocketPerfTest::WritePacketsToSocket(UDPClientSocket
* socket
,
65 base::Closure done_callback
) {
66 scoped_refptr
<IOBufferWithSize
> io_buffer(new IOBufferWithSize(kPacketSize
));
67 memset(io_buffer
->data(), 'G', kPacketSize
);
69 while (num_of_packets
) {
71 socket
->Write(io_buffer
.get(), io_buffer
->size(),
72 base::Bind(&UDPSocketPerfTest::DoneWritePacketsToSocket
,
73 weak_factory_
.GetWeakPtr(), socket
,
74 num_of_packets
- 1, done_callback
));
75 if (rv
== ERR_IO_PENDING
)
79 if (!num_of_packets
) {
85 void UDPSocketPerfTest::WriteBenchmark(bool use_nonblocking_io
) {
86 base::MessageLoopForIO message_loop
;
87 const uint16 kPort
= 9999;
89 // Setup the server to listen.
90 IPEndPoint bind_address
;
91 CreateUDPAddress("127.0.0.1", kPort
, &bind_address
);
92 scoped_ptr
<UDPServerSocket
> server(
93 new UDPServerSocket(nullptr, NetLog::Source()));
95 if (use_nonblocking_io
)
96 server
->UseNonBlockingIO();
98 int rv
= server
->Listen(bind_address
);
102 IPEndPoint server_address
;
103 CreateUDPAddress("127.0.0.1", kPort
, &server_address
);
104 scoped_ptr
<UDPClientSocket
> client(
105 new UDPClientSocket(DatagramSocket::DEFAULT_BIND
, RandIntCallback(),
106 nullptr, NetLog::Source()));
108 if (use_nonblocking_io
)
109 client
->UseNonBlockingIO();
111 rv
= client
->Connect(server_address
);
114 base::RunLoop run_loop
;
115 base::TimeTicks start_ticks
= base::TimeTicks::Now();
116 int packets
= 100000;
117 client
->SetSendBufferSize(1024);
118 WritePacketsToSocket(client
.get(), packets
, run_loop
.QuitClosure());
121 double elapsed
= (base::TimeTicks::Now() - start_ticks
).InSecondsF();
122 LOG(INFO
) << "Write speed: " << packets
/ 1024 / elapsed
<< " MB/s";
125 TEST_F(UDPSocketPerfTest
, Write
) {
126 base::PerfTimeLogger
timer("UDP_socket_write");
127 WriteBenchmark(false);
131 TEST_F(UDPSocketPerfTest
, WriteNonBlocking
) {
132 base::PerfTimeLogger
timer("UDP_socket_write_nonblocking");
133 WriteBenchmark(true);