OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/test/perf_time_logger.h" | 15 #include "base/test/perf_time_logger.h" |
| 16 #include "base/test/test_io_thread.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "ipc/ipc_channel.h" | 19 #include "ipc/ipc_channel.h" |
19 #include "ipc/ipc_channel_proxy.h" | 20 #include "ipc/ipc_channel_proxy.h" |
20 #include "ipc/ipc_descriptors.h" | 21 #include "ipc/ipc_descriptors.h" |
21 #include "ipc/ipc_message_utils.h" | 22 #include "ipc/ipc_message_utils.h" |
22 #include "ipc/ipc_sender.h" | 23 #include "ipc/ipc_sender.h" |
23 #include "ipc/ipc_test_base.h" | 24 #include "ipc/ipc_test_base.h" |
24 | 25 |
25 namespace { | 26 namespace { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 return true; | 131 return true; |
131 } | 132 } |
132 | 133 |
133 private: | 134 private: |
134 IPC::Channel* channel_; | 135 IPC::Channel* channel_; |
135 EventTimeTracker latency_tracker_; | 136 EventTimeTracker latency_tracker_; |
136 }; | 137 }; |
137 | 138 |
138 class PerformanceChannelListener : public IPC::Listener { | 139 class PerformanceChannelListener : public IPC::Listener { |
139 public: | 140 public: |
140 PerformanceChannelListener() | 141 explicit PerformanceChannelListener(const std::string& label) |
141 : channel_(NULL), | 142 : label_(label), |
| 143 sender_(NULL), |
142 msg_count_(0), | 144 msg_count_(0), |
143 msg_size_(0), | 145 msg_size_(0), |
144 count_down_(0), | 146 count_down_(0), |
145 latency_tracker_("Server messages") { | 147 latency_tracker_("Server messages") { |
146 VLOG(1) << "Server listener up"; | 148 VLOG(1) << "Server listener up"; |
147 } | 149 } |
148 | 150 |
149 virtual ~PerformanceChannelListener() { | 151 virtual ~PerformanceChannelListener() { |
150 VLOG(1) << "Server listener down"; | 152 VLOG(1) << "Server listener down"; |
151 } | 153 } |
152 | 154 |
153 void Init(IPC::Channel* channel) { | 155 void Init(IPC::Sender* sender) { |
154 DCHECK(!channel_); | 156 DCHECK(!sender_); |
155 channel_ = channel; | 157 sender_ = sender; |
156 } | 158 } |
157 | 159 |
158 // Call this before running the message loop. | 160 // Call this before running the message loop. |
159 void SetTestParams(int msg_count, size_t msg_size) { | 161 void SetTestParams(int msg_count, size_t msg_size) { |
160 DCHECK_EQ(0, count_down_); | 162 DCHECK_EQ(0, count_down_); |
161 msg_count_ = msg_count; | 163 msg_count_ = msg_count; |
162 msg_size_ = msg_size; | 164 msg_size_ = msg_size; |
163 count_down_ = msg_count_; | 165 count_down_ = msg_count_; |
164 payload_ = std::string(msg_size_, 'a'); | 166 payload_ = std::string(msg_size_, 'a'); |
165 } | 167 } |
166 | 168 |
167 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 169 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
168 CHECK(channel_); | 170 CHECK(sender_); |
169 | 171 |
170 PickleIterator iter(message); | 172 PickleIterator iter(message); |
171 int64 time_internal; | 173 int64 time_internal; |
172 EXPECT_TRUE(iter.ReadInt64(&time_internal)); | 174 EXPECT_TRUE(iter.ReadInt64(&time_internal)); |
173 int msgid; | 175 int msgid; |
174 EXPECT_TRUE(iter.ReadInt(&msgid)); | 176 EXPECT_TRUE(iter.ReadInt(&msgid)); |
175 std::string reflected_payload; | 177 std::string reflected_payload; |
176 EXPECT_TRUE(iter.ReadString(&reflected_payload)); | 178 EXPECT_TRUE(iter.ReadString(&reflected_payload)); |
177 | 179 |
178 // Include message deserialization in latency. | 180 // Include message deserialization in latency. |
179 base::TimeTicks now = base::TimeTicks::Now(); | 181 base::TimeTicks now = base::TimeTicks::Now(); |
180 | 182 |
181 if (reflected_payload == "hello") { | 183 if (reflected_payload == "hello") { |
182 // Start timing on hello. | 184 // Start timing on hello. |
183 latency_tracker_.Reset(); | 185 latency_tracker_.Reset(); |
184 DCHECK(!perf_logger_.get()); | 186 DCHECK(!perf_logger_.get()); |
185 std::string test_name = base::StringPrintf( | 187 std::string test_name = |
186 "IPC_Perf_%dx_%u", msg_count_, static_cast<unsigned>(msg_size_)); | 188 base::StringPrintf("IPC_%s_Perf_%dx_%u", |
| 189 label_.c_str(), |
| 190 msg_count_, |
| 191 static_cast<unsigned>(msg_size_)); |
187 perf_logger_.reset(new base::PerfTimeLogger(test_name.c_str())); | 192 perf_logger_.reset(new base::PerfTimeLogger(test_name.c_str())); |
188 } else { | 193 } else { |
189 DCHECK_EQ(payload_.size(), reflected_payload.size()); | 194 DCHECK_EQ(payload_.size(), reflected_payload.size()); |
190 | 195 |
191 latency_tracker_.AddEvent( | 196 latency_tracker_.AddEvent( |
192 base::TimeTicks::FromInternalValue(time_internal), now); | 197 base::TimeTicks::FromInternalValue(time_internal), now); |
193 | 198 |
194 CHECK(count_down_ > 0); | 199 CHECK(count_down_ > 0); |
195 count_down_--; | 200 count_down_--; |
196 if (count_down_ == 0) { | 201 if (count_down_ == 0) { |
197 perf_logger_.reset(); // Stop the perf timer now. | 202 perf_logger_.reset(); // Stop the perf timer now. |
198 latency_tracker_.ShowResults(); | 203 latency_tracker_.ShowResults(); |
199 base::MessageLoop::current()->QuitWhenIdle(); | 204 base::MessageLoop::current()->QuitWhenIdle(); |
200 return true; | 205 return true; |
201 } | 206 } |
202 } | 207 } |
203 | 208 |
204 IPC::Message* msg = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); | 209 IPC::Message* msg = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
205 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue()); | 210 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
206 msg->WriteInt(count_down_); | 211 msg->WriteInt(count_down_); |
207 msg->WriteString(payload_); | 212 msg->WriteString(payload_); |
208 channel_->Send(msg); | 213 sender_->Send(msg); |
209 return true; | 214 return true; |
210 } | 215 } |
211 | 216 |
212 private: | 217 private: |
213 IPC::Channel* channel_; | 218 std::string label_; |
| 219 IPC::Sender* sender_; |
214 int msg_count_; | 220 int msg_count_; |
215 size_t msg_size_; | 221 size_t msg_size_; |
216 | 222 |
217 int count_down_; | 223 int count_down_; |
218 std::string payload_; | 224 std::string payload_; |
219 EventTimeTracker latency_tracker_; | 225 EventTimeTracker latency_tracker_; |
220 scoped_ptr<base::PerfTimeLogger> perf_logger_; | 226 scoped_ptr<base::PerfTimeLogger> perf_logger_; |
221 }; | 227 }; |
222 | 228 |
223 TEST_F(IPCChannelPerfTest, Performance) { | 229 TEST_F(IPCChannelPerfTest, ChannelPingPong) { |
224 Init("PerformanceClient"); | 230 Init("PerformanceClient"); |
225 | 231 |
226 // Set up IPC channel and start client. | 232 // Set up IPC channel and start client. |
227 PerformanceChannelListener listener; | 233 PerformanceChannelListener listener("Channel"); |
228 CreateChannel(&listener); | 234 CreateChannel(&listener); |
229 listener.Init(channel()); | 235 listener.Init(channel()); |
230 ASSERT_TRUE(ConnectChannel()); | 236 ASSERT_TRUE(ConnectChannel()); |
231 ASSERT_TRUE(StartClient()); | 237 ASSERT_TRUE(StartClient()); |
232 | 238 |
233 // Test several sizes. We use 12^N for message size, and limit the message | 239 // Test several sizes. We use 12^N for message size, and limit the message |
234 // count to keep the test duration reasonable. | 240 // count to keep the test duration reasonable. |
235 const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832}; | 241 const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832}; |
236 const int kMessageCount[5] = {50000, 50000, 50000, 12000, 1000}; | 242 const int kMessageCount[5] = {50000, 50000, 50000, 12000, 1000}; |
237 | 243 |
(...skipping 29 matching lines...) Expand all Loading... |
267 ChannelReflectorListener listener; | 273 ChannelReflectorListener listener; |
268 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( | 274 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
269 IPCTestBase::GetChannelName("PerformanceClient"), &listener)); | 275 IPCTestBase::GetChannelName("PerformanceClient"), &listener)); |
270 listener.Init(channel.get()); | 276 listener.Init(channel.get()); |
271 CHECK(channel->Connect()); | 277 CHECK(channel->Connect()); |
272 | 278 |
273 base::MessageLoop::current()->Run(); | 279 base::MessageLoop::current()->Run(); |
274 return 0; | 280 return 0; |
275 } | 281 } |
276 | 282 |
| 283 TEST_F(IPCChannelPerfTest, ChannelProxyPingPong) { |
| 284 set_message_loop(make_scoped_ptr(new base::MessageLoop())); |
| 285 Init("PerformanceClient"); |
| 286 |
| 287 base::TestIOThread io_thread(base::TestIOThread::kAutoStart); |
| 288 |
| 289 // Set up IPC channel and start client. |
| 290 PerformanceChannelListener listener("ChannelProxy"); |
| 291 CreateChannelProxy(&listener, io_thread.task_runner()); |
| 292 listener.Init(channel_proxy()); |
| 293 ASSERT_TRUE(StartClient()); |
| 294 |
| 295 // Test several sizes. We use 12^N for message size, and limit the message |
| 296 // count to keep the test duration reasonable. |
| 297 const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832}; |
| 298 const int kMessageCount[5] = {50000, 50000, 50000, 12000, 1000}; |
| 299 |
| 300 for (size_t i = 0; i < 5; i++) { |
| 301 listener.SetTestParams(kMessageCount[i], kMsgSize[i]); |
| 302 |
| 303 // This initial message will kick-start the ping-pong of messages. |
| 304 IPC::Message* message = |
| 305 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 306 message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
| 307 message->WriteInt(-1); |
| 308 message->WriteString("hello"); |
| 309 sender()->Send(message); |
| 310 |
| 311 // Run message loop. |
| 312 base::MessageLoop::current()->Run(); |
| 313 } |
| 314 |
| 315 // Send quit message. |
| 316 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 317 message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
| 318 message->WriteInt(-1); |
| 319 message->WriteString("quit"); |
| 320 sender()->Send(message); |
| 321 |
| 322 EXPECT_TRUE(WaitForClientShutdown()); |
| 323 DestroyChannelProxy(); |
| 324 } |
| 325 |
277 } // namespace | 326 } // namespace |
OLD | NEW |