OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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_log_unittest.h" |
| 15 #include "net/base/net_util.h" |
| 16 #include "net/base/test_completion_callback.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" |
| 23 |
| 24 namespace { |
| 25 |
| 26 class UDPSocketPerfTest : public PlatformTest { |
| 27 public: |
| 28 UDPSocketPerfTest() |
| 29 : buffer_(new net::IOBufferWithSize(kPacketSize)), weak_factory_(this) {} |
| 30 |
| 31 void DoneWritePacketsToSocket(net::UDPClientSocket* socket, |
| 32 int num_of_packets, |
| 33 base::Closure done_callback, |
| 34 int error) { |
| 35 WritePacketsToSocket(socket, num_of_packets, done_callback); |
| 36 } |
| 37 |
| 38 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done. |
| 39 void WritePacketsToSocket(net::UDPClientSocket* socket, |
| 40 int num_of_packets, |
| 41 base::Closure done_callback); |
| 42 |
| 43 // Use non-blocking IO if |use_nonblocking_io| is true. This variable only |
| 44 // has effect on Windows. |
| 45 void WriteBenchmark(bool use_nonblocking_io); |
| 46 |
| 47 protected: |
| 48 static const int kPacketSize = 1024; |
| 49 scoped_refptr<net::IOBufferWithSize> buffer_; |
| 50 base::WeakPtrFactory<UDPSocketPerfTest> weak_factory_; |
| 51 }; |
| 52 |
| 53 // Creates and address from an ip/port and returns it in |address|. |
| 54 void CreateUDPAddress(std::string ip_str, |
| 55 uint16 port, |
| 56 net::IPEndPoint* address) { |
| 57 net::IPAddressNumber ip_number; |
| 58 bool rv = net::ParseIPLiteralToNumber(ip_str, &ip_number); |
| 59 if (!rv) |
| 60 return; |
| 61 *address = net::IPEndPoint(ip_number, port); |
| 62 } |
| 63 |
| 64 void UDPSocketPerfTest::WritePacketsToSocket(net::UDPClientSocket* socket, |
| 65 int num_of_packets, |
| 66 base::Closure done_callback) { |
| 67 scoped_refptr<net::IOBufferWithSize> io_buffer( |
| 68 new net::IOBufferWithSize(kPacketSize)); |
| 69 memset(io_buffer->data(), 'G', kPacketSize); |
| 70 |
| 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 == net::ERR_IO_PENDING) |
| 78 break; |
| 79 --num_of_packets; |
| 80 } |
| 81 if (!num_of_packets) { |
| 82 done_callback.Run(); |
| 83 return; |
| 84 } |
| 85 } |
| 86 |
| 87 void UDPSocketPerfTest::WriteBenchmark(bool use_nonblocking_io) { |
| 88 base::MessageLoopForIO message_loop; |
| 89 const uint16 kPort = 9999; |
| 90 |
| 91 // Setup the server to listen. |
| 92 net::IPEndPoint bind_address; |
| 93 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
| 94 net::CapturingNetLog server_log; |
| 95 scoped_ptr<net::UDPServerSocket> server( |
| 96 new net::UDPServerSocket(&server_log, net::NetLog::Source())); |
| 97 #if defined(OS_WIN) |
| 98 if (use_nonblocking_io) |
| 99 server->UseNonBlockingIO(); |
| 100 #endif |
| 101 int rv = server->Listen(bind_address); |
| 102 ASSERT_EQ(net::OK, rv); |
| 103 |
| 104 // Setup the client. |
| 105 net::IPEndPoint server_address; |
| 106 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
| 107 net::CapturingNetLog client_log; |
| 108 scoped_ptr<net::UDPClientSocket> client(new net::UDPClientSocket( |
| 109 net::DatagramSocket::DEFAULT_BIND, net::RandIntCallback(), &client_log, |
| 110 net::NetLog::Source())); |
| 111 #if defined(OS_WIN) |
| 112 if (use_nonblocking_io) |
| 113 client->UseNonBlockingIO(); |
| 114 #endif |
| 115 rv = client->Connect(server_address); |
| 116 EXPECT_EQ(net::OK, rv); |
| 117 |
| 118 base::RunLoop run_loop; |
| 119 base::TimeTicks start_ticks = base::TimeTicks::Now(); |
| 120 int packets = 100000; |
| 121 client->SetSendBufferSize(1024); |
| 122 WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure()); |
| 123 run_loop.Run(); |
| 124 |
| 125 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); |
| 126 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; |
| 127 } |
| 128 |
| 129 TEST_F(UDPSocketPerfTest, Write) { |
| 130 base::PerfTimeLogger timer("UDP_socket_write"); |
| 131 WriteBenchmark(false); |
| 132 } |
| 133 |
| 134 #if defined(OS_WIN) |
| 135 TEST_F(UDPSocketPerfTest, WriteNonBlocking) { |
| 136 base::PerfTimeLogger timer("UDP_socket_write_nonblocking"); |
| 137 WriteBenchmark(true); |
| 138 } |
| 139 #endif |
| 140 |
| 141 } // namespace |
OLD | NEW |