| 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/bind.h" | |
| 6 #include "base/memory/weak_ptr.h" | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/test/perf_time_logger.h" | |
| 10 #include "net/base/io_buffer.h" | |
| 11 #include "net/base/ip_endpoint.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/base/test_completion_callback.h" | |
| 14 #include "net/log/net_log_source.h" | |
| 15 #include "net/test/gtest_util.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/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 #include "testing/platform_test.h" | |
| 23 | |
| 24 using net::test::IsOk; | |
| 25 | |
| 26 namespace net { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 class UDPSocketPerfTest : public PlatformTest { | |
| 31 public: | |
| 32 UDPSocketPerfTest() | |
| 33 : buffer_(new IOBufferWithSize(kPacketSize)), weak_factory_(this) {} | |
| 34 | |
| 35 void DoneWritePacketsToSocket(UDPClientSocket* socket, | |
| 36 int num_of_packets, | |
| 37 base::Closure done_callback, | |
| 38 int error) { | |
| 39 WritePacketsToSocket(socket, num_of_packets, done_callback); | |
| 40 } | |
| 41 | |
| 42 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done. | |
| 43 void WritePacketsToSocket(UDPClientSocket* socket, | |
| 44 int num_of_packets, | |
| 45 base::Closure done_callback); | |
| 46 | |
| 47 // Use non-blocking IO if |use_nonblocking_io| is true. This variable only | |
| 48 // has effect on Windows. | |
| 49 void WriteBenchmark(bool use_nonblocking_io); | |
| 50 | |
| 51 protected: | |
| 52 static const int kPacketSize = 1024; | |
| 53 scoped_refptr<IOBufferWithSize> buffer_; | |
| 54 base::WeakPtrFactory<UDPSocketPerfTest> weak_factory_; | |
| 55 }; | |
| 56 | |
| 57 // Creates and address from an ip/port and returns it in |address|. | |
| 58 void CreateUDPAddress(const std::string& ip_str, | |
| 59 uint16_t port, | |
| 60 IPEndPoint* address) { | |
| 61 IPAddress ip_address; | |
| 62 if (!ip_address.AssignFromIPLiteral(ip_str)) | |
| 63 return; | |
| 64 *address = IPEndPoint(ip_address, port); | |
| 65 } | |
| 66 | |
| 67 void UDPSocketPerfTest::WritePacketsToSocket(UDPClientSocket* socket, | |
| 68 int num_of_packets, | |
| 69 base::Closure done_callback) { | |
| 70 scoped_refptr<IOBufferWithSize> io_buffer(new IOBufferWithSize(kPacketSize)); | |
| 71 memset(io_buffer->data(), 'G', kPacketSize); | |
| 72 | |
| 73 while (num_of_packets) { | |
| 74 int rv = | |
| 75 socket->Write(io_buffer.get(), io_buffer->size(), | |
| 76 base::Bind(&UDPSocketPerfTest::DoneWritePacketsToSocket, | |
| 77 weak_factory_.GetWeakPtr(), socket, | |
| 78 num_of_packets - 1, done_callback)); | |
| 79 if (rv == ERR_IO_PENDING) | |
| 80 break; | |
| 81 --num_of_packets; | |
| 82 } | |
| 83 if (!num_of_packets) { | |
| 84 done_callback.Run(); | |
| 85 return; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void UDPSocketPerfTest::WriteBenchmark(bool use_nonblocking_io) { | |
| 90 base::MessageLoopForIO message_loop; | |
| 91 const uint16_t kPort = 9999; | |
| 92 | |
| 93 // Setup the server to listen. | |
| 94 IPEndPoint bind_address; | |
| 95 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | |
| 96 std::unique_ptr<UDPServerSocket> server( | |
| 97 new UDPServerSocket(nullptr, NetLogSource())); | |
| 98 if (use_nonblocking_io) | |
| 99 server->UseNonBlockingIO(); | |
| 100 int rv = server->Listen(bind_address); | |
| 101 ASSERT_THAT(rv, IsOk()); | |
| 102 | |
| 103 // Setup the client. | |
| 104 IPEndPoint server_address; | |
| 105 CreateUDPAddress("127.0.0.1", kPort, &server_address); | |
| 106 std::unique_ptr<UDPClientSocket> client( | |
| 107 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | |
| 108 nullptr, NetLogSource())); | |
| 109 if (use_nonblocking_io) | |
| 110 client->UseNonBlockingIO(); | |
| 111 rv = client->Connect(server_address); | |
| 112 EXPECT_THAT(rv, IsOk()); | |
| 113 | |
| 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(); | |
| 120 | |
| 121 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); | |
| 122 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; | |
| 123 } | |
| 124 | |
| 125 TEST_F(UDPSocketPerfTest, Write) { | |
| 126 base::PerfTimeLogger timer("UDP_socket_write"); | |
| 127 WriteBenchmark(false); | |
| 128 } | |
| 129 | |
| 130 TEST_F(UDPSocketPerfTest, WriteNonBlocking) { | |
| 131 base::PerfTimeLogger timer("UDP_socket_write_nonblocking"); | |
| 132 WriteBenchmark(true); | |
| 133 } | |
| 134 | |
| 135 } // namespace | |
| 136 | |
| 137 } // namespace net | |
| OLD | NEW |