Update Polymer and pull in iron-list
[chromium-blink-merge.git] / net / udp / udp_socket_perftest.cc
blob6816c57cecbd695fa280166200274176715a97a7
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"
6 #include "base/bind.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"
23 namespace net {
25 namespace {
27 class UDPSocketPerfTest : public PlatformTest {
28 public:
29 UDPSocketPerfTest()
30 : buffer_(new IOBufferWithSize(kPacketSize)), weak_factory_(this) {}
32 void DoneWritePacketsToSocket(UDPClientSocket* socket,
33 int num_of_packets,
34 base::Closure done_callback,
35 int error) {
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,
41 int num_of_packets,
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);
48 protected:
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,
56 uint16 port,
57 IPEndPoint* address) {
58 IPAddressNumber ip_number;
59 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number);
60 if (!rv)
61 return;
62 *address = IPEndPoint(ip_number, port);
65 void UDPSocketPerfTest::WritePacketsToSocket(UDPClientSocket* socket,
66 int num_of_packets,
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) {
72 int rv =
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)
78 break;
79 --num_of_packets;
81 if (!num_of_packets) {
82 done_callback.Run();
83 return;
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()));
96 #if defined(OS_WIN)
97 if (use_nonblocking_io)
98 server->UseNonBlockingIO();
99 #endif
100 int rv = server->Listen(bind_address);
101 ASSERT_EQ(OK, rv);
103 // Setup the client.
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()));
109 #if defined(OS_WIN)
110 if (use_nonblocking_io)
111 client->UseNonBlockingIO();
112 #endif
113 rv = client->Connect(server_address);
114 EXPECT_EQ(OK, rv);
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());
121 run_loop.Run();
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);
132 #if defined(OS_WIN)
133 TEST_F(UDPSocketPerfTest, WriteNonBlocking) {
134 base::PerfTimeLogger timer("UDP_socket_write_nonblocking");
135 WriteBenchmark(true);
137 #endif
139 } // namespace
141 } // namespace net