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

Side by Side Diff: ipc/ipc_perftest_support.cc

Issue 536213002: Add ipc_mojo_perftests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed garbage Created 6 years, 3 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 unified diff | Download patch
« no previous file with comments | « ipc/ipc_perftest_support.h ('k') | ipc/ipc_perftests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "ipc/ipc_perftest_support.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/test/test_io_thread.h"
17 #include "base/threading/thread.h" 17 #include "base/threading/thread.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 #include "build/build_config.h"
19 #include "ipc/ipc_channel.h" 20 #include "ipc/ipc_channel.h"
20 #include "ipc/ipc_channel_proxy.h" 21 #include "ipc/ipc_channel_proxy.h"
21 #include "ipc/ipc_descriptors.h" 22 #include "ipc/ipc_descriptors.h"
22 #include "ipc/ipc_message_utils.h" 23 #include "ipc/ipc_message_utils.h"
23 #include "ipc/ipc_sender.h" 24 #include "ipc/ipc_sender.h"
24 #include "ipc/ipc_test_base.h"
25 25
26 namespace { 26 namespace IPC {
27 27 namespace test {
28 // This test times the roundtrip IPC message cycle.
29 //
30 // TODO(brettw): Make this test run by default.
31
32 class IPCChannelPerfTest : public IPCTestBase {
33 };
34 28
35 // This class simply collects stats about abstract "events" (each of which has a 29 // This class simply collects stats about abstract "events" (each of which has a
36 // start time and an end time). 30 // start time and an end time).
37 class EventTimeTracker { 31 class EventTimeTracker {
38 public: 32 public:
39 explicit EventTimeTracker(const char* name) 33 explicit EventTimeTracker(const char* name)
40 : name_(name), 34 : name_(name),
41 count_(0) { 35 count_(0) {
42 } 36 }
43 37
(...skipping 28 matching lines...) Expand all
72 uint64 count_; 66 uint64 count_;
73 base::TimeDelta total_duration_; 67 base::TimeDelta total_duration_;
74 base::TimeDelta max_duration_; 68 base::TimeDelta max_duration_;
75 69
76 DISALLOW_COPY_AND_ASSIGN(EventTimeTracker); 70 DISALLOW_COPY_AND_ASSIGN(EventTimeTracker);
77 }; 71 };
78 72
79 // This channel listener just replies to all messages with the exact same 73 // This channel listener just replies to all messages with the exact same
80 // message. It assumes each message has one string parameter. When the string 74 // message. It assumes each message has one string parameter. When the string
81 // "quit" is sent, it will exit. 75 // "quit" is sent, it will exit.
82 class ChannelReflectorListener : public IPC::Listener { 76 class ChannelReflectorListener : public Listener {
83 public: 77 public:
84 ChannelReflectorListener() 78 ChannelReflectorListener()
85 : channel_(NULL), 79 : channel_(NULL),
86 latency_tracker_("Client messages") { 80 latency_tracker_("Client messages") {
87 VLOG(1) << "Client listener up"; 81 VLOG(1) << "Client listener up";
88 } 82 }
89 83
90 virtual ~ChannelReflectorListener() { 84 virtual ~ChannelReflectorListener() {
91 VLOG(1) << "Client listener down"; 85 VLOG(1) << "Client listener down";
92 latency_tracker_.ShowResults(); 86 latency_tracker_.ShowResults();
93 } 87 }
94 88
95 void Init(IPC::Channel* channel) { 89 void Init(Channel* channel) {
96 DCHECK(!channel_); 90 DCHECK(!channel_);
97 channel_ = channel; 91 channel_ = channel;
98 } 92 }
99 93
100 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { 94 virtual bool OnMessageReceived(const Message& message) OVERRIDE {
101 CHECK(channel_); 95 CHECK(channel_);
102 96
103 PickleIterator iter(message); 97 PickleIterator iter(message);
104 int64 time_internal; 98 int64 time_internal;
105 EXPECT_TRUE(iter.ReadInt64(&time_internal)); 99 EXPECT_TRUE(iter.ReadInt64(&time_internal));
106 int msgid; 100 int msgid;
107 EXPECT_TRUE(iter.ReadInt(&msgid)); 101 EXPECT_TRUE(iter.ReadInt(&msgid));
108 std::string payload; 102 std::string payload;
109 EXPECT_TRUE(iter.ReadString(&payload)); 103 EXPECT_TRUE(iter.ReadString(&payload));
110 104
111 // Include message deserialization in latency. 105 // Include message deserialization in latency.
112 base::TimeTicks now = base::TimeTicks::Now(); 106 base::TimeTicks now = base::TimeTicks::Now();
113 107
114 if (payload == "hello") { 108 if (payload == "hello") {
115 latency_tracker_.Reset(); 109 latency_tracker_.Reset();
116 } else if (payload == "quit") { 110 } else if (payload == "quit") {
117 latency_tracker_.ShowResults(); 111 latency_tracker_.ShowResults();
118 base::MessageLoop::current()->QuitWhenIdle(); 112 base::MessageLoop::current()->QuitWhenIdle();
119 return true; 113 return true;
120 } else { 114 } else {
121 // Don't track hello and quit messages. 115 // Don't track hello and quit messages.
122 latency_tracker_.AddEvent( 116 latency_tracker_.AddEvent(
123 base::TimeTicks::FromInternalValue(time_internal), now); 117 base::TimeTicks::FromInternalValue(time_internal), now);
124 } 118 }
125 119
126 IPC::Message* msg = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); 120 Message* msg = new Message(0, 2, Message::PRIORITY_NORMAL);
127 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue()); 121 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue());
128 msg->WriteInt(msgid); 122 msg->WriteInt(msgid);
129 msg->WriteString(payload); 123 msg->WriteString(payload);
130 channel_->Send(msg); 124 channel_->Send(msg);
131 return true; 125 return true;
132 } 126 }
133 127
134 private: 128 private:
135 IPC::Channel* channel_; 129 Channel* channel_;
136 EventTimeTracker latency_tracker_; 130 EventTimeTracker latency_tracker_;
137 }; 131 };
138 132
139 class PerformanceChannelListener : public IPC::Listener { 133 class PerformanceChannelListener : public Listener {
140 public: 134 public:
141 explicit PerformanceChannelListener(const std::string& label) 135 explicit PerformanceChannelListener(const std::string& label)
142 : label_(label), 136 : label_(label),
143 sender_(NULL), 137 sender_(NULL),
144 msg_count_(0), 138 msg_count_(0),
145 msg_size_(0), 139 msg_size_(0),
146 count_down_(0), 140 count_down_(0),
147 latency_tracker_("Server messages") { 141 latency_tracker_("Server messages") {
148 VLOG(1) << "Server listener up"; 142 VLOG(1) << "Server listener up";
149 } 143 }
150 144
151 virtual ~PerformanceChannelListener() { 145 virtual ~PerformanceChannelListener() {
152 VLOG(1) << "Server listener down"; 146 VLOG(1) << "Server listener down";
153 } 147 }
154 148
155 void Init(IPC::Sender* sender) { 149 void Init(Sender* sender) {
156 DCHECK(!sender_); 150 DCHECK(!sender_);
157 sender_ = sender; 151 sender_ = sender;
158 } 152 }
159 153
160 // Call this before running the message loop. 154 // Call this before running the message loop.
161 void SetTestParams(int msg_count, size_t msg_size) { 155 void SetTestParams(int msg_count, size_t msg_size) {
162 DCHECK_EQ(0, count_down_); 156 DCHECK_EQ(0, count_down_);
163 msg_count_ = msg_count; 157 msg_count_ = msg_count;
164 msg_size_ = msg_size; 158 msg_size_ = msg_size;
165 count_down_ = msg_count_; 159 count_down_ = msg_count_;
166 payload_ = std::string(msg_size_, 'a'); 160 payload_ = std::string(msg_size_, 'a');
167 } 161 }
168 162
169 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { 163 virtual bool OnMessageReceived(const Message& message) OVERRIDE {
170 CHECK(sender_); 164 CHECK(sender_);
171 165
172 PickleIterator iter(message); 166 PickleIterator iter(message);
173 int64 time_internal; 167 int64 time_internal;
174 EXPECT_TRUE(iter.ReadInt64(&time_internal)); 168 EXPECT_TRUE(iter.ReadInt64(&time_internal));
175 int msgid; 169 int msgid;
176 EXPECT_TRUE(iter.ReadInt(&msgid)); 170 EXPECT_TRUE(iter.ReadInt(&msgid));
177 std::string reflected_payload; 171 std::string reflected_payload;
178 EXPECT_TRUE(iter.ReadString(&reflected_payload)); 172 EXPECT_TRUE(iter.ReadString(&reflected_payload));
179 173
(...skipping 19 matching lines...) Expand all
199 CHECK(count_down_ > 0); 193 CHECK(count_down_ > 0);
200 count_down_--; 194 count_down_--;
201 if (count_down_ == 0) { 195 if (count_down_ == 0) {
202 perf_logger_.reset(); // Stop the perf timer now. 196 perf_logger_.reset(); // Stop the perf timer now.
203 latency_tracker_.ShowResults(); 197 latency_tracker_.ShowResults();
204 base::MessageLoop::current()->QuitWhenIdle(); 198 base::MessageLoop::current()->QuitWhenIdle();
205 return true; 199 return true;
206 } 200 }
207 } 201 }
208 202
209 IPC::Message* msg = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); 203 Message* msg = new Message(0, 2, Message::PRIORITY_NORMAL);
210 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue()); 204 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue());
211 msg->WriteInt(count_down_); 205 msg->WriteInt(count_down_);
212 msg->WriteString(payload_); 206 msg->WriteString(payload_);
213 sender_->Send(msg); 207 sender_->Send(msg);
214 return true; 208 return true;
215 } 209 }
216 210
217 private: 211 private:
218 std::string label_; 212 std::string label_;
219 IPC::Sender* sender_; 213 Sender* sender_;
220 int msg_count_; 214 int msg_count_;
221 size_t msg_size_; 215 size_t msg_size_;
222 216
223 int count_down_; 217 int count_down_;
224 std::string payload_; 218 std::string payload_;
225 EventTimeTracker latency_tracker_; 219 EventTimeTracker latency_tracker_;
226 scoped_ptr<base::PerfTimeLogger> perf_logger_; 220 scoped_ptr<base::PerfTimeLogger> perf_logger_;
227 }; 221 };
228 222
229 TEST_F(IPCChannelPerfTest, ChannelPingPong) { 223 std::vector<PingPongTestParams>
224 IPCChannelPerfTestBase::GetDefaultTestParams() {
225 // Test several sizes. We use 12^N for message size, and limit the message
226 // count to keep the test duration reasonable.
227 std::vector<PingPongTestParams> list;
228 list.push_back(PingPongTestParams(12, 50000));
229 list.push_back(PingPongTestParams(144, 50000));
230 list.push_back(PingPongTestParams(1728, 50000));
231 list.push_back(PingPongTestParams(20736, 12000));
232 list.push_back(PingPongTestParams(248832, 100));
233 return list;
234 }
235
236 void IPCChannelPerfTestBase::RunTestChannelPingPong(
237 const std::vector<PingPongTestParams>& params) {
230 Init("PerformanceClient"); 238 Init("PerformanceClient");
231 239
232 // Set up IPC channel and start client. 240 // Set up IPC channel and start client.
233 PerformanceChannelListener listener("Channel"); 241 PerformanceChannelListener listener("Channel");
234 CreateChannel(&listener); 242 CreateChannel(&listener);
235 listener.Init(channel()); 243 listener.Init(channel());
236 ASSERT_TRUE(ConnectChannel()); 244 ASSERT_TRUE(ConnectChannel());
237 ASSERT_TRUE(StartClient()); 245 ASSERT_TRUE(StartClient());
238 246
239 // Test several sizes. We use 12^N for message size, and limit the message 247 for (size_t i = 0; i < params.size(); i++) {
240 // count to keep the test duration reasonable. 248 listener.SetTestParams(params[i].message_count(),
241 const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832}; 249 params[i].message_size());
242 const int kMessageCount[5] = {50000, 50000, 50000, 12000, 1000};
243
244 for (size_t i = 0; i < 5; i++) {
245 listener.SetTestParams(kMessageCount[i], kMsgSize[i]);
246 250
247 // This initial message will kick-start the ping-pong of messages. 251 // This initial message will kick-start the ping-pong of messages.
248 IPC::Message* message = 252 Message* message =
249 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); 253 new Message(0, 2, Message::PRIORITY_NORMAL);
250 message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); 254 message->WriteInt64(base::TimeTicks::Now().ToInternalValue());
251 message->WriteInt(-1); 255 message->WriteInt(-1);
252 message->WriteString("hello"); 256 message->WriteString("hello");
253 sender()->Send(message); 257 sender()->Send(message);
254 258
255 // Run message loop. 259 // Run message loop.
256 base::MessageLoop::current()->Run(); 260 base::MessageLoop::current()->Run();
257 } 261 }
258 262
259 // Send quit message. 263 // Send quit message.
260 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); 264 Message* message = new Message(0, 2, Message::PRIORITY_NORMAL);
261 message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); 265 message->WriteInt64(base::TimeTicks::Now().ToInternalValue());
262 message->WriteInt(-1); 266 message->WriteInt(-1);
263 message->WriteString("quit"); 267 message->WriteString("quit");
264 sender()->Send(message); 268 sender()->Send(message);
265 269
266 EXPECT_TRUE(WaitForClientShutdown()); 270 EXPECT_TRUE(WaitForClientShutdown());
267 DestroyChannel(); 271 DestroyChannel();
268 } 272 }
269 273
270 // This message loop bounces all messages back to the sender. 274 void IPCChannelPerfTestBase::RunTestChannelProxyPingPong(
271 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(PerformanceClient) { 275 const std::vector<PingPongTestParams>& params) {
272 base::MessageLoopForIO main_message_loop;
273 ChannelReflectorListener listener;
274 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient(
275 IPCTestBase::GetChannelName("PerformanceClient"), &listener));
276 listener.Init(channel.get());
277 CHECK(channel->Connect());
278
279 base::MessageLoop::current()->Run();
280 return 0;
281 }
282
283 TEST_F(IPCChannelPerfTest, ChannelProxyPingPong) {
284 InitWithCustomMessageLoop("PerformanceClient", 276 InitWithCustomMessageLoop("PerformanceClient",
285 make_scoped_ptr(new base::MessageLoop())); 277 make_scoped_ptr(new base::MessageLoop()));
286 278
287 base::TestIOThread io_thread(base::TestIOThread::kAutoStart); 279 base::TestIOThread io_thread(base::TestIOThread::kAutoStart);
288 280
289 // Set up IPC channel and start client. 281 // Set up IPC channel and start client.
290 PerformanceChannelListener listener("ChannelProxy"); 282 PerformanceChannelListener listener("ChannelProxy");
291 CreateChannelProxy(&listener, io_thread.task_runner()); 283 CreateChannelProxy(&listener, io_thread.task_runner());
292 listener.Init(channel_proxy()); 284 listener.Init(channel_proxy());
293 ASSERT_TRUE(StartClient()); 285 ASSERT_TRUE(StartClient());
294 286
295 // Test several sizes. We use 12^N for message size, and limit the message 287 for (size_t i = 0; i < params.size(); i++) {
296 // count to keep the test duration reasonable. 288 listener.SetTestParams(params[i].message_count(),
297 const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832}; 289 params[i].message_size());
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 290
303 // This initial message will kick-start the ping-pong of messages. 291 // This initial message will kick-start the ping-pong of messages.
304 IPC::Message* message = 292 Message* message =
305 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); 293 new Message(0, 2, Message::PRIORITY_NORMAL);
306 message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); 294 message->WriteInt64(base::TimeTicks::Now().ToInternalValue());
307 message->WriteInt(-1); 295 message->WriteInt(-1);
308 message->WriteString("hello"); 296 message->WriteString("hello");
309 sender()->Send(message); 297 sender()->Send(message);
310 298
311 // Run message loop. 299 // Run message loop.
312 base::MessageLoop::current()->Run(); 300 base::MessageLoop::current()->Run();
313 } 301 }
314 302
315 // Send quit message. 303 // Send quit message.
316 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); 304 Message* message = new Message(0, 2, Message::PRIORITY_NORMAL);
317 message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); 305 message->WriteInt64(base::TimeTicks::Now().ToInternalValue());
318 message->WriteInt(-1); 306 message->WriteInt(-1);
319 message->WriteString("quit"); 307 message->WriteString("quit");
320 sender()->Send(message); 308 sender()->Send(message);
321 309
322 EXPECT_TRUE(WaitForClientShutdown()); 310 EXPECT_TRUE(WaitForClientShutdown());
323 DestroyChannelProxy(); 311 DestroyChannelProxy();
324 } 312 }
325 313
326 } // namespace 314
315 PingPongTestClient::PingPongTestClient()
316 : listener_(new ChannelReflectorListener()) {
317 }
318
319 PingPongTestClient::~PingPongTestClient() {
320 }
321
322 scoped_ptr<Channel> PingPongTestClient::CreateChannel(
323 Listener* listener) {
324 return Channel::CreateClient(
325 IPCTestBase::GetChannelName("PerformanceClient"), listener);
326 }
327
328 int PingPongTestClient::RunMain() {
329 scoped_ptr<Channel> channel = CreateChannel(listener_.get());
330 listener_->Init(channel.get());
331 CHECK(channel->Connect());
332
333 base::MessageLoop::current()->Run();
334 return 0;
335 }
336
337 scoped_refptr<base::TaskRunner> PingPongTestClient::task_runner() {
338 return main_message_loop_.message_loop_proxy();
339 }
340
341 } // namespace test
342 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/ipc_perftest_support.h ('k') | ipc/ipc_perftests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698