[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / udp / udp_socket_perftest.cc
blob00c6c70577e54f869114b8cbbd06e75740282062
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(std::string ip_str, uint16 port, IPEndPoint* address) {
56 IPAddressNumber ip_number;
57 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number);
58 if (!rv)
59 return;
60 *address = IPEndPoint(ip_number, port);
63 void UDPSocketPerfTest::WritePacketsToSocket(UDPClientSocket* socket,
64 int num_of_packets,
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) {
70 int rv =
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)
76 break;
77 --num_of_packets;
79 if (!num_of_packets) {
80 done_callback.Run();
81 return;
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()));
94 #if defined(OS_WIN)
95 if (use_nonblocking_io)
96 server->UseNonBlockingIO();
97 #endif
98 int rv = server->Listen(bind_address);
99 ASSERT_EQ(OK, rv);
101 // Setup the client.
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()));
107 #if defined(OS_WIN)
108 if (use_nonblocking_io)
109 client->UseNonBlockingIO();
110 #endif
111 rv = client->Connect(server_address);
112 EXPECT_EQ(OK, rv);
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());
119 run_loop.Run();
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);
130 #if defined(OS_WIN)
131 TEST_F(UDPSocketPerfTest, WriteNonBlocking) {
132 base::PerfTimeLogger timer("UDP_socket_write_nonblocking");
133 WriteBenchmark(true);
135 #endif
137 } // namespace
139 } // namespace net