WebKit Roll 139512:139548
[chromium-blink-merge.git] / ipc / ipc_perftests.cc
blobd6473cb0713ebcdf3c0c91d57dd951c7a42c61ff
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
3 // found in the LICENSE file.
5 #include "build/build_config.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #elif defined(OS_POSIX)
10 #include <sys/types.h>
11 #include <unistd.h>
12 #endif
14 #include <string>
16 #include "base/base_switches.h"
17 #include "base/command_line.h"
18 #include "base/debug/debug_on_start_win.h"
19 #include "base/perftimer.h"
20 #include "base/pickle.h"
21 #include "base/threading/thread.h"
22 #include "base/time.h"
23 #include "ipc/ipc_descriptors.h"
24 #include "ipc/ipc_channel.h"
25 #include "ipc/ipc_channel_proxy.h"
26 #include "ipc/ipc_message_utils.h"
27 #include "ipc/ipc_multiprocess_test.h"
28 #include "ipc/ipc_sender.h"
29 #include "ipc/ipc_test_base.h"
30 #include "testing/multiprocess_func_list.h"
32 namespace {
34 // This test times the roundtrip IPC message cycle. It is enabled with a
35 // special preprocessor define to enable it instead of the standard IPC
36 // unit tests. This works around some funny termination conditions in the
37 // regular unit tests.
39 // This test is not automated. To test, you will want to vary the message
40 // count and message size in TEST to get the numbers you want.
42 // FIXME(brettw): Automate this test and have it run by default.
44 class IPCChannelPerfTest : public IPCTestBase {
47 // This channel listener just replies to all messages with the exact same
48 // message. It assumes each message has one string parameter. When the string
49 // "quit" is sent, it will exit.
50 class ChannelReflectorListener : public IPC::Listener {
51 public:
52 explicit ChannelReflectorListener(IPC::Channel *channel) :
53 channel_(channel),
54 count_messages_(0) {
55 std::cout << "Reflector up" << std::endl;
58 ~ChannelReflectorListener() {
59 std::cout << "Client Messages: " << count_messages_ << std::endl;
60 std::cout << "Client Latency: " << latency_messages_.InMilliseconds()
61 << std::endl;
64 virtual bool OnMessageReceived(const IPC::Message& message) {
65 count_messages_++;
66 PickleIterator iter(message);
67 int64 time_internal;
68 EXPECT_TRUE(iter.ReadInt64(&time_internal));
69 int msgid;
70 EXPECT_TRUE(iter.ReadInt(&msgid));
71 std::string payload;
72 EXPECT_TRUE(iter.ReadString(&payload));
73 // TODO(vtl): Should we use |HighResNow()| instead of |Now()|?
74 latency_messages_ += base::TimeTicks::Now() -
75 base::TimeTicks::FromInternalValue(time_internal);
77 // cout << "reflector msg received: " << msgid << endl;
78 if (payload == "quit")
79 MessageLoop::current()->Quit();
81 IPC::Message* msg = new IPC::Message(0,
83 IPC::Message::PRIORITY_NORMAL);
84 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue());
85 msg->WriteInt(msgid);
86 msg->WriteString(payload);
87 channel_->Send(msg);
88 return true;
91 private:
92 IPC::Channel *channel_;
93 int count_messages_;
94 base::TimeDelta latency_messages_;
97 class ChannelPerfListener : public IPC::Listener {
98 public:
99 ChannelPerfListener(IPC::Channel* channel, int msg_count, int msg_size) :
100 count_down_(msg_count),
101 channel_(channel),
102 count_messages_(0) {
103 payload_.resize(msg_size);
104 for (int i = 0; i < static_cast<int>(payload_.size()); i++)
105 payload_[i] = 'a';
106 std::cout << "perflistener up" << std::endl;
109 ~ChannelPerfListener() {
110 std::cout << "Server Messages: " << count_messages_ << std::endl;
111 std::cout << "Server Latency: " << latency_messages_.InMilliseconds()
112 << std::endl;
115 virtual bool OnMessageReceived(const IPC::Message& message) {
116 count_messages_++;
117 // Decode the string so this gets counted in the total time.
118 PickleIterator iter(message);
119 int64 time_internal;
120 EXPECT_TRUE(iter.ReadInt64(&time_internal));
121 int msgid;
122 EXPECT_TRUE(iter.ReadInt(&msgid));
123 std::string cur;
124 EXPECT_TRUE(iter.ReadString(&cur));
125 latency_messages_ += base::TimeTicks::Now() -
126 base::TimeTicks::FromInternalValue(time_internal);
128 count_down_--;
129 if (count_down_ == 0) {
130 IPC::Message* msg = new IPC::Message(0,
132 IPC::Message::PRIORITY_NORMAL);
133 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue());
134 msg->WriteInt(count_down_);
135 msg->WriteString("quit");
136 channel_->Send(msg);
138 MessageLoop::current()->QuitWhenIdle();
139 return true;
142 IPC::Message* msg = new IPC::Message(0,
144 IPC::Message::PRIORITY_NORMAL);
145 msg->WriteInt64(base::TimeTicks::Now().ToInternalValue());
146 msg->WriteInt(count_down_);
147 msg->WriteString(payload_);
148 channel_->Send(msg);
149 return true;
152 private:
153 int count_down_;
154 std::string payload_;
155 IPC::Channel *channel_;
156 int count_messages_;
157 base::TimeDelta latency_messages_;
160 TEST_F(IPCChannelPerfTest, Performance) {
161 // setup IPC channel
162 IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_SERVER, NULL);
163 ChannelPerfListener perf_listener(&chan, 10000, 100000);
164 chan.set_listener(&perf_listener);
165 ASSERT_TRUE(chan.Connect());
167 base::ProcessHandle process_handle = SpawnChild(TEST_REFLECTOR, &chan);
168 ASSERT_TRUE(process_handle);
170 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
172 PerfTimeLogger logger("IPC_Perf");
174 // this initial message will kick-start the ping-pong of messages
175 IPC::Message* message = new IPC::Message(0,
177 IPC::Message::PRIORITY_NORMAL);
178 message->WriteInt64(base::TimeTicks::Now().ToInternalValue());
179 message->WriteInt(-1);
180 message->WriteString("Hello");
181 chan.Send(message);
183 // run message loop
184 MessageLoop::current()->Run();
186 // Clean up child process.
187 EXPECT_TRUE(base::WaitForSingleProcess(
188 process_handle, base::TimeDelta::FromSeconds(5)));
189 base::CloseProcessHandle(process_handle);
192 // This message loop bounces all messages back to the sender
193 MULTIPROCESS_IPC_TEST_MAIN(RunReflector) {
194 MessageLoopForIO main_message_loop;
195 IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_CLIENT, NULL);
196 ChannelReflectorListener channel_reflector_listener(&chan);
197 chan.set_listener(&channel_reflector_listener);
198 CHECK(chan.Connect());
200 MessageLoop::current()->Run();
201 return 0;
204 } // namespace