Chromium Code Reviews| Index: net/udp/udp_socket_unittest.cc |
| diff --git a/net/udp/udp_socket_unittest.cc b/net/udp/udp_socket_unittest.cc |
| index c92bf2b35d100c4466a5ee262f2f1665bf065e46..d34e6f1d59d00196d30d2dde17379934914d6e41 100644 |
| --- a/net/udp/udp_socket_unittest.cc |
| +++ b/net/udp/udp_socket_unittest.cc |
| @@ -9,7 +9,9 @@ |
| #include "base/basictypes.h" |
| #include "base/bind.h" |
| +#include "base/memory/weak_ptr.h" |
| #include "base/metrics/histogram.h" |
| +#include "base/run_loop.h" |
| #include "base/stl_util.h" |
| #include "net/base/io_buffer.h" |
| #include "net/base/ip_endpoint.h" |
| @@ -28,8 +30,7 @@ namespace { |
| class UDPSocketTest : public PlatformTest { |
| public: |
| UDPSocketTest() |
| - : buffer_(new IOBufferWithSize(kMaxRead)) { |
| - } |
| + : buffer_(new IOBufferWithSize(kMaxRead)), weak_factory_(this) {} |
| // Blocks until data is read from the socket. |
| std::string RecvFromSocket(UDPServerSocket* socket) { |
| @@ -44,6 +45,36 @@ class UDPSocketTest : public PlatformTest { |
| return std::string(buffer_->data(), rv); |
| } |
| + void DoneWritePacketsToSocket(UDPClientSocket* socket, |
| + int num_of_packets, |
| + base::Closure done_callback, |
| + int error) { |
| + WritePacketsToSocket(socket, num_of_packets, done_callback); |
| + } |
| + |
| + // Send |num_of_packets| to |socket|. Invoke |done_callback| when done. |
| + void WritePacketsToSocket(UDPClientSocket* socket, |
| + int num_of_packets, |
| + base::Closure done_callback) { |
| + std::string msg(kMaxRead, 'G'); |
| + scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); |
| + |
| + while (num_of_packets) { |
| + int rv = |
| + socket->Write(io_buffer.get(), io_buffer->size(), |
| + base::Bind(&UDPSocketTest::DoneWritePacketsToSocket, |
| + weak_factory_.GetWeakPtr(), socket, |
| + num_of_packets - 1, done_callback)); |
| + if (rv == ERR_IO_PENDING) |
| + break; |
| + --num_of_packets; |
| + } |
| + if (!num_of_packets) { |
| + done_callback.Run(); |
| + return; |
| + } |
| + } |
| + |
| // Loop until |msg| has been written to the socket or until an |
| // error occurs. |
| // If |address| is specified, then it is used for the destination |
| @@ -116,6 +147,7 @@ class UDPSocketTest : public PlatformTest { |
| static const int kMaxRead = 1024; |
| scoped_refptr<IOBufferWithSize> buffer_; |
| IPEndPoint recv_from_address_; |
| + base::WeakPtrFactory<UDPSocketTest> weak_factory_; |
| }; |
| // Creates and address from an ip/port and returns it in |address|. |
| @@ -622,6 +654,43 @@ TEST_F(UDPSocketTest, SetDSCP) { |
| client.Close(); |
| } |
| +TEST_F(UDPSocketTest, DISABLED_WriteBenchmark) { |
|
rvargas (doing something else)
2015/01/21 22:10:09
benchmarks belong to net_perftests
Alpha Left Google
2015/01/22 01:01:25
Doing this in a later patchset.
|
| + const uint16 kPort = 9999; |
| + std::string simple_message("hello world!"); |
| + |
| + // Setup the server to listen. |
| + IPEndPoint bind_address; |
| + CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
| + CapturingNetLog server_log; |
| + scoped_ptr<UDPServerSocket> server( |
| + new UDPServerSocket(&server_log, NetLog::Source())); |
| + server->AllowAddressReuse(); |
| + int rv = server->Listen(bind_address); |
| + ASSERT_EQ(OK, rv); |
| + |
| + // Setup the client. |
| + IPEndPoint server_address; |
| + CreateUDPAddress("127.0.0.1", kPort, &server_address); |
| + CapturingNetLog client_log; |
| + scoped_ptr<UDPClientSocket> client( |
| + new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| + &client_log, NetLog::Source())); |
| + rv = client->Connect(server_address); |
| + EXPECT_EQ(OK, rv); |
| + |
| + base::RunLoop run_loop; |
| + base::TimeTicks start_ticks = base::TimeTicks::Now(); |
| + int packets = 100000; |
| + client->SetSendBufferSize(1024 * 128); |
| + WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure()); |
| + run_loop.Run(); |
| + |
| + double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); |
| + LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; |
| + server.reset(); |
| + client.reset(); |
| +} |
| + |
| } // namespace |
| #if defined(OS_WIN) |