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(const std::string
& ip_str
,
57 IPEndPoint
* address
) {
58 IPAddressNumber ip_number
;
59 bool rv
= ParseIPLiteralToNumber(ip_str
, &ip_number
);
62 *address
= IPEndPoint(ip_number
, port
);
65 void UDPSocketPerfTest::WritePacketsToSocket(UDPClientSocket
* socket
,
67 base::Closure done_callback
) {
68 scoped_refptr
<IOBufferWithSize
> io_buffer(new 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
== 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 IPEndPoint bind_address
;
93 CreateUDPAddress("127.0.0.1", kPort
, &bind_address
);
94 scoped_ptr
<UDPServerSocket
> server(
95 new UDPServerSocket(nullptr, NetLog::Source()));
97 if (use_nonblocking_io
)
98 server
->UseNonBlockingIO();
100 int rv
= server
->Listen(bind_address
);
104 IPEndPoint server_address
;
105 CreateUDPAddress("127.0.0.1", kPort
, &server_address
);
106 scoped_ptr
<UDPClientSocket
> client(
107 new UDPClientSocket(DatagramSocket::DEFAULT_BIND
, RandIntCallback(),
108 nullptr, NetLog::Source()));
110 if (use_nonblocking_io
)
111 client
->UseNonBlockingIO();
113 rv
= client
->Connect(server_address
);
116 base::RunLoop run_loop
;
117 base::TimeTicks start_ticks
= base::TimeTicks::Now();
118 int packets
= 100000;
119 client
->SetSendBufferSize(1024);
120 WritePacketsToSocket(client
.get(), packets
, run_loop
.QuitClosure());
123 double elapsed
= (base::TimeTicks::Now() - start_ticks
).InSecondsF();
124 LOG(INFO
) << "Write speed: " << packets
/ 1024 / elapsed
<< " MB/s";
127 TEST_F(UDPSocketPerfTest
, Write
) {
128 base::PerfTimeLogger
timer("UDP_socket_write");
129 WriteBenchmark(false);
133 TEST_F(UDPSocketPerfTest
, WriteNonBlocking
) {
134 base::PerfTimeLogger
timer("UDP_socket_write_nonblocking");
135 WriteBenchmark(true);