Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Unified Diff: net/udp/udp_socket_perftest.cc

Issue 861963002: UDP: Windows implementation using non-blocking IO (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added perf test Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/udp/udp_socket_perftest.cc
diff --git a/net/udp/udp_socket_perftest.cc b/net/udp/udp_socket_perftest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..324169daa1501bb60fc34701fcf28834825ab137
--- /dev/null
+++ b/net/udp/udp_socket_perftest.cc
@@ -0,0 +1,138 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
rvargas (doing something else) 2015/01/22 23:45:13 nit: no (c) and 2015
Alpha Left Google 2015/01/23 02:29:46 Done.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/basictypes.h"
+#include "base/bind.h"
+#include "base/memory/weak_ptr.h"
+#include "base/run_loop.h"
+#include "base/test/perf_time_logger.h"
+#include "net/base/io_buffer.h"
+#include "net/base/ip_endpoint.h"
+#include "net/base/net_errors.h"
+#include "net/base/net_log_unittest.h"
+#include "net/base/net_util.h"
+#include "net/base/test_completion_callback.h"
+#include "net/test/net_test_suite.h"
+#include "net/udp/udp_client_socket.h"
+#include "net/udp/udp_server_socket.h"
+#include "net/udp/udp_socket.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+namespace net {
+
+namespace {
+
+class UDPSocketPerfTest : public PlatformTest {
+ public:
+ UDPSocketPerfTest()
+ : buffer_(new IOBufferWithSize(kPacketSize)), weak_factory_(this) {}
+
+ 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(kPacketSize, '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(&UDPSocketPerfTest::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;
+ }
+ }
+
+ // Creates and address from an ip/port and returns it in |address|.
+ void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) {
+ IPAddressNumber ip_number;
+ bool rv = ParseIPLiteralToNumber(ip_str, &ip_number);
+ if (!rv)
+ return;
+ *address = IPEndPoint(ip_number, port);
+ }
+
+ // Use non-blocking IO if |use_nonblocking_io| is true. This variable only
+ // has effect on Windows.
+ void WriteBenchmark(bool use_nonblocking_io) {
+ 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()));
+#if defined(OS_WIN)
+ if (use_nonblocking_io)
+ server->UseNonBlockingIO();
+#endif
+ 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()));
+#if defined(OS_WIN)
+ if (use_nonblocking_io)
+ client->UseNonBlockingIO();
+#endif
+ 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();
+ }
+
+ protected:
+ static const int kPacketSize = 1024;
+ scoped_refptr<IOBufferWithSize> buffer_;
+ base::WeakPtrFactory<UDPSocketPerfTest> weak_factory_;
+};
+
+TEST_F(UDPSocketPerfTest, Write) {
+ base::PerfTimeLogger timer("UDP_socket_write");
+ WriteBenchmark(false);
+}
+
+#if defined(OS_WIN)
+TEST_F(UDPSocketPerfTest, WriteNonBlocking) {
+ base::PerfTimeLogger timer("UDP_socket_write_nonblocking");
+ WriteBenchmark(true);
+}
+#endif
+
+} // namespace
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698