Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / udp / udp_socket_perftest.cc
blob8d237b8941bf107e1314479d7090107f43a5e86b
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/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"
24 namespace net {
26 namespace {
28 class UDPSocketPerfTest : public PlatformTest {
29 public:
30 UDPSocketPerfTest()
31 : buffer_(new IOBufferWithSize(kPacketSize)), weak_factory_(this) {}
33 void DoneWritePacketsToSocket(UDPClientSocket* socket,
34 int num_of_packets,
35 base::Closure done_callback,
36 int error) {
37 WritePacketsToSocket(socket, num_of_packets, done_callback);
40 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done.
41 void WritePacketsToSocket(UDPClientSocket* socket,
42 int num_of_packets,
43 base::Closure done_callback);
45 // Use non-blocking IO if |use_nonblocking_io| is true. This variable only
46 // has effect on Windows.
47 void WriteBenchmark(bool use_nonblocking_io);
49 protected:
50 static const int kPacketSize = 1024;
51 scoped_refptr<IOBufferWithSize> buffer_;
52 base::WeakPtrFactory<UDPSocketPerfTest> weak_factory_;
55 // Creates and address from an ip/port and returns it in |address|.
56 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) {
57 IPAddressNumber ip_number;
58 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number);
59 if (!rv)
60 return;
61 *address = IPEndPoint(ip_number, port);
64 void UDPSocketPerfTest::WritePacketsToSocket(UDPClientSocket* socket,
65 int num_of_packets,
66 base::Closure done_callback) {
67 scoped_refptr<IOBufferWithSize> io_buffer(new IOBufferWithSize(kPacketSize));
68 memset(io_buffer->data(), 'G', kPacketSize);
70 while (num_of_packets) {
71 int rv =
72 socket->Write(io_buffer.get(), io_buffer->size(),
73 base::Bind(&UDPSocketPerfTest::DoneWritePacketsToSocket,
74 weak_factory_.GetWeakPtr(), socket,
75 num_of_packets - 1, done_callback));
76 if (rv == ERR_IO_PENDING)
77 break;
78 --num_of_packets;
80 if (!num_of_packets) {
81 done_callback.Run();
82 return;
86 void UDPSocketPerfTest::WriteBenchmark(bool use_nonblocking_io) {
87 base::MessageLoopForIO message_loop;
88 const uint16 kPort = 9999;
90 // Setup the server to listen.
91 IPEndPoint bind_address;
92 CreateUDPAddress("127.0.0.1", kPort, &bind_address);
93 TestNetLog server_log;
94 scoped_ptr<UDPServerSocket> server(
95 new UDPServerSocket(&server_log, 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 TestNetLog client_log;
107 scoped_ptr<UDPClientSocket> client(
108 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(),
109 &client_log, NetLog::Source()));
110 #if defined(OS_WIN)
111 if (use_nonblocking_io)
112 client->UseNonBlockingIO();
113 #endif
114 rv = client->Connect(server_address);
115 EXPECT_EQ(OK, rv);
117 base::RunLoop run_loop;
118 base::TimeTicks start_ticks = base::TimeTicks::Now();
119 int packets = 100000;
120 client->SetSendBufferSize(1024);
121 WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure());
122 run_loop.Run();
124 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF();
125 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s";
128 TEST_F(UDPSocketPerfTest, Write) {
129 base::PerfTimeLogger timer("UDP_socket_write");
130 WriteBenchmark(false);
133 #if defined(OS_WIN)
134 TEST_F(UDPSocketPerfTest, WriteNonBlocking) {
135 base::PerfTimeLogger timer("UDP_socket_write_nonblocking");
136 WriteBenchmark(true);
138 #endif
140 } // namespace
142 } // namespace net