Chromium Code Reviews| 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/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/net_log_unittest.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" | |
| 22 | |
| 23 namespace net { | |
|
rvargas (doing something else)
2015/01/23 03:05:35
Please take the test out of the net namespace. The
Alpha Left Google
2015/01/23 03:52:22
Done.
| |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class UDPSocketPerfTest : public PlatformTest { | |
| 28 public: | |
| 29 UDPSocketPerfTest() | |
| 30 : buffer_(new IOBufferWithSize(kPacketSize)), weak_factory_(this) {} | |
| 31 | |
| 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); | |
| 37 } | |
| 38 | |
| 39 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done. | |
| 40 void WritePacketsToSocket(UDPClientSocket* socket, | |
|
rvargas (doing something else)
2015/01/23 03:05:35
This class is too long to use inline definitions.
Alpha Left Google
2015/01/23 03:52:22
Defined it out of class.
| |
| 41 int num_of_packets, | |
| 42 base::Closure done_callback) { | |
| 43 std::string msg(kPacketSize, 'G'); | |
| 44 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); | |
|
rvargas (doing something else)
2015/01/23 03:05:35
I know this is just a test, but having a memcpy he
Alpha Left Google
2015/01/23 03:52:22
Done.
| |
| 45 | |
| 46 while (num_of_packets) { | |
|
rvargas (doing something else)
2015/01/23 03:05:34
for ?
Alpha Left Google
2015/01/23 03:52:22
I need a counter to track how many packets are sen
| |
| 47 int rv = | |
| 48 socket->Write(io_buffer.get(), io_buffer->size(), | |
| 49 base::Bind(&UDPSocketPerfTest::DoneWritePacketsToSocket, | |
| 50 weak_factory_.GetWeakPtr(), socket, | |
| 51 num_of_packets - 1, done_callback)); | |
| 52 if (rv == ERR_IO_PENDING) | |
| 53 break; | |
| 54 --num_of_packets; | |
| 55 } | |
| 56 if (!num_of_packets) { | |
| 57 done_callback.Run(); | |
| 58 return; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 // Creates and address from an ip/port and returns it in |address|. | |
| 63 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) { | |
| 64 IPAddressNumber ip_number; | |
| 65 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); | |
| 66 if (!rv) | |
| 67 return; | |
| 68 *address = IPEndPoint(ip_number, port); | |
| 69 } | |
| 70 | |
| 71 // Use non-blocking IO if |use_nonblocking_io| is true. This variable only | |
| 72 // has effect on Windows. | |
| 73 void WriteBenchmark(bool use_nonblocking_io) { | |
| 74 const uint16 kPort = 9999; | |
| 75 std::string simple_message("hello world!"); | |
| 76 | |
| 77 // Setup the server to listen. | |
| 78 IPEndPoint bind_address; | |
| 79 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | |
| 80 CapturingNetLog server_log; | |
| 81 scoped_ptr<UDPServerSocket> server( | |
| 82 new UDPServerSocket(&server_log, NetLog::Source())); | |
| 83 #if defined(OS_WIN) | |
| 84 if (use_nonblocking_io) | |
| 85 server->UseNonBlockingIO(); | |
| 86 #endif | |
| 87 server->AllowAddressReuse(); | |
|
rvargas (doing something else)
2015/01/23 03:05:35
This generates unreliable behavior on Windows.
Alpha Left Google
2015/01/23 03:52:22
Done.
| |
| 88 int rv = server->Listen(bind_address); | |
| 89 ASSERT_EQ(OK, rv); | |
| 90 | |
| 91 // Setup the client. | |
| 92 IPEndPoint server_address; | |
| 93 CreateUDPAddress("127.0.0.1", kPort, &server_address); | |
| 94 CapturingNetLog client_log; | |
| 95 scoped_ptr<UDPClientSocket> client( | |
| 96 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | |
| 97 &client_log, NetLog::Source())); | |
| 98 #if defined(OS_WIN) | |
| 99 if (use_nonblocking_io) | |
| 100 client->UseNonBlockingIO(); | |
| 101 #endif | |
| 102 rv = client->Connect(server_address); | |
| 103 EXPECT_EQ(OK, rv); | |
| 104 | |
| 105 base::RunLoop run_loop; | |
| 106 base::TimeTicks start_ticks = base::TimeTicks::Now(); | |
| 107 int packets = 100000; | |
| 108 client->SetSendBufferSize(1024 * 128); | |
| 109 WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure()); | |
| 110 run_loop.Run(); | |
| 111 | |
| 112 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); | |
| 113 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; | |
| 114 server.reset(); | |
|
rvargas (doing something else)
2015/01/23 03:05:35
Don't really need these, do you?
Alpha Left Google
2015/01/23 03:52:22
Done.
| |
| 115 client.reset(); | |
| 116 } | |
| 117 | |
| 118 protected: | |
| 119 static const int kPacketSize = 1024; | |
| 120 scoped_refptr<IOBufferWithSize> buffer_; | |
| 121 base::WeakPtrFactory<UDPSocketPerfTest> weak_factory_; | |
| 122 }; | |
| 123 | |
| 124 TEST_F(UDPSocketPerfTest, Write) { | |
| 125 base::PerfTimeLogger timer("UDP_socket_write"); | |
| 126 WriteBenchmark(false); | |
| 127 } | |
| 128 | |
| 129 #if defined(OS_WIN) | |
| 130 TEST_F(UDPSocketPerfTest, WriteNonBlocking) { | |
| 131 base::PerfTimeLogger timer("UDP_socket_write_nonblocking"); | |
| 132 WriteBenchmark(true); | |
| 133 } | |
| 134 #endif | |
| 135 | |
| 136 } // namespace | |
| 137 | |
| 138 } // namespace net | |
| OLD | NEW |