OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/test/perf_time_logger.h" | 9 #include "base/test/perf_time_logger.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 base::Closure done_callback, | 37 base::Closure done_callback, |
38 int error) { | 38 int error) { |
39 WritePacketsToSocket(socket, num_of_packets, done_callback); | 39 WritePacketsToSocket(socket, num_of_packets, done_callback); |
40 } | 40 } |
41 | 41 |
42 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done. | 42 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done. |
43 void WritePacketsToSocket(UDPClientSocket* socket, | 43 void WritePacketsToSocket(UDPClientSocket* socket, |
44 int num_of_packets, | 44 int num_of_packets, |
45 base::Closure done_callback); | 45 base::Closure done_callback); |
46 | 46 |
47 // Use non-blocking IO if |use_nonblocking_io| is true. This variable only | 47 void WriteBenchmark(); |
48 // has effect on Windows. | |
49 void WriteBenchmark(bool use_nonblocking_io); | |
50 | 48 |
51 protected: | 49 protected: |
52 static const int kPacketSize = 1024; | 50 static const int kPacketSize = 1024; |
53 scoped_refptr<IOBufferWithSize> buffer_; | 51 scoped_refptr<IOBufferWithSize> buffer_; |
54 base::WeakPtrFactory<UDPSocketPerfTest> weak_factory_; | 52 base::WeakPtrFactory<UDPSocketPerfTest> weak_factory_; |
55 }; | 53 }; |
56 | 54 |
57 // Creates and address from an ip/port and returns it in |address|. | 55 // Creates and address from an ip/port and returns it in |address|. |
58 void CreateUDPAddress(const std::string& ip_str, | 56 void CreateUDPAddress(const std::string& ip_str, |
59 uint16_t port, | 57 uint16_t port, |
(...skipping 19 matching lines...) Expand all Loading... |
79 if (rv == ERR_IO_PENDING) | 77 if (rv == ERR_IO_PENDING) |
80 break; | 78 break; |
81 --num_of_packets; | 79 --num_of_packets; |
82 } | 80 } |
83 if (!num_of_packets) { | 81 if (!num_of_packets) { |
84 done_callback.Run(); | 82 done_callback.Run(); |
85 return; | 83 return; |
86 } | 84 } |
87 } | 85 } |
88 | 86 |
89 void UDPSocketPerfTest::WriteBenchmark(bool use_nonblocking_io) { | 87 TEST_F(UDPSocketPerfTest, Write) { |
| 88 base::PerfTimeLogger timer("UDP_socket_write"); |
90 base::MessageLoopForIO message_loop; | 89 base::MessageLoopForIO message_loop; |
91 const uint16_t kPort = 9999; | 90 const uint16_t kPort = 9999; |
92 | 91 |
93 // Setup the server to listen. | 92 // Setup the server to listen. |
94 IPEndPoint bind_address; | 93 IPEndPoint bind_address; |
95 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | 94 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
96 std::unique_ptr<UDPServerSocket> server( | 95 std::unique_ptr<UDPServerSocket> server( |
97 new UDPServerSocket(nullptr, NetLogSource())); | 96 new UDPServerSocket(nullptr, NetLogSource())); |
98 if (use_nonblocking_io) | |
99 server->UseNonBlockingIO(); | |
100 int rv = server->Listen(bind_address); | 97 int rv = server->Listen(bind_address); |
101 ASSERT_THAT(rv, IsOk()); | 98 ASSERT_THAT(rv, IsOk()); |
102 | 99 |
103 // Setup the client. | 100 // Setup the client. |
104 IPEndPoint server_address; | 101 IPEndPoint server_address; |
105 CreateUDPAddress("127.0.0.1", kPort, &server_address); | 102 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
106 std::unique_ptr<UDPClientSocket> client( | 103 std::unique_ptr<UDPClientSocket> client( |
107 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 104 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
108 nullptr, NetLogSource())); | 105 nullptr, NetLogSource())); |
109 if (use_nonblocking_io) | |
110 client->UseNonBlockingIO(); | |
111 rv = client->Connect(server_address); | 106 rv = client->Connect(server_address); |
112 EXPECT_THAT(rv, IsOk()); | 107 EXPECT_THAT(rv, IsOk()); |
113 | 108 |
114 base::RunLoop run_loop; | 109 base::RunLoop run_loop; |
115 base::TimeTicks start_ticks = base::TimeTicks::Now(); | 110 base::TimeTicks start_ticks = base::TimeTicks::Now(); |
116 int packets = 100000; | 111 int packets = 100000; |
117 client->SetSendBufferSize(1024); | 112 client->SetSendBufferSize(1024); |
118 WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure()); | 113 WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure()); |
119 run_loop.Run(); | 114 run_loop.Run(); |
120 | 115 |
121 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); | 116 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); |
122 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; | 117 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; |
123 } | 118 } |
124 | 119 |
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 | 120 } // namespace |
136 | 121 |
137 } // namespace net | 122 } // namespace net |
OLD | NEW |