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"
9 #elif defined(OS_POSIX)
10 #include <sys/types.h>
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"
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
{
52 explicit ChannelReflectorListener(IPC::Channel
*channel
) :
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()
64 virtual bool OnMessageReceived(const IPC::Message
& message
) {
66 PickleIterator
iter(message
);
68 EXPECT_TRUE(iter
.ReadInt64(&time_internal
));
70 EXPECT_TRUE(iter
.ReadInt(&msgid
));
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());
86 msg
->WriteString(payload
);
92 IPC::Channel
*channel_
;
94 base::TimeDelta latency_messages_
;
97 class ChannelPerfListener
: public IPC::Listener
{
99 ChannelPerfListener(IPC::Channel
* channel
, int msg_count
, int msg_size
) :
100 count_down_(msg_count
),
103 payload_
.resize(msg_size
);
104 for (int i
= 0; i
< static_cast<int>(payload_
.size()); i
++)
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()
115 virtual bool OnMessageReceived(const IPC::Message
& message
) {
117 // Decode the string so this gets counted in the total time.
118 PickleIterator
iter(message
);
120 EXPECT_TRUE(iter
.ReadInt64(&time_internal
));
122 EXPECT_TRUE(iter
.ReadInt(&msgid
));
124 EXPECT_TRUE(iter
.ReadString(&cur
));
125 latency_messages_
+= base::TimeTicks::Now() -
126 base::TimeTicks::FromInternalValue(time_internal
);
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");
138 MessageLoop::current()->QuitWhenIdle();
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_
);
154 std::string payload_
;
155 IPC::Channel
*channel_
;
157 base::TimeDelta latency_messages_
;
160 TEST_F(IPCChannelPerfTest
, Performance
) {
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");
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();