Skip to content

Commit

Permalink
Make ipc_tests file structure a little saner and add an ipc_perftests…
Browse files Browse the repository at this point in the history
… target.

This means that the (one, semi-manual) IPC perf test that we have will build
without manual hackery (and do so separately from the ipc_tests target).


Review URL: https://chromiumcodereview.appspot.com/11819041

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176341 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
viettrungluu@chromium.org committed Jan 11, 2013
1 parent a3c07c2 commit 0cb7d8c
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 313 deletions.
2 changes: 1 addition & 1 deletion build/android/gtest_filter/ipc_tests_disabled
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ IPCChannelPosixTest.MultiConnection
IPCFuzzingTest.SanityTest
IPCFuzzingTest.MsgBadPayloadArgs
IPCFuzzingTest.MsgBadPayloadShort
IPCChannelTest.DescriptorTest
IPCSendFdsTest.DescriptorTest
IPCChannelTest.ChannelTest
IPCChannelTest.ChannelProxyTest
IPCChannelTest.SendMessageInChannelConnected
Expand Down
49 changes: 47 additions & 2 deletions ipc/ipc.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'test_support_ipc',
'../base/base.gyp:base',
'../base/base.gyp:base_i18n',
'../base/base.gyp:run_all_unittests',
'../base/base.gyp:test_support_base',
'../testing/gtest.gyp:gtest',
],
Expand All @@ -46,15 +47,16 @@
'sources': [
'file_descriptor_set_posix_unittest.cc',
'ipc_channel_posix_unittest.cc',
'ipc_channel_unittest.cc',
'ipc_fuzzing_tests.cc',
'ipc_message_unittest.cc',
'ipc_message_utils_unittest.cc',
'ipc_send_fds_test.cc',
'ipc_sync_channel_unittest.cc',
'ipc_sync_message_unittest.cc',
'ipc_sync_message_unittest.h',
'ipc_tests.cc',
'ipc_tests.h',
'ipc_test_base.cc',
'ipc_test_base.h',
'sync_socket_unittest.cc',
],
'conditions': [
Expand All @@ -79,6 +81,49 @@
}]
],
},
{
'target_name': 'ipc_perftests',
'type': '<(gtest_target_type)',
# TODO(viettrungluu): Figure out which dependencies are really needed.
'dependencies': [
'ipc',
'test_support_ipc',
'../base/base.gyp:base',
'../base/base.gyp:base_i18n',
'../base/base.gyp:test_support_base',
'../base/base.gyp:test_support_perf',
'../testing/gtest.gyp:gtest',
],
'include_dirs': [
'..'
],
'sources': [
'ipc_perftests.cc',
'ipc_test_base.cc',
'ipc_test_base.h',
],
'conditions': [
['toolkit_uses_gtk == 1', {
'dependencies': [
'../build/linux/system.gyp:gtk',
],
}],
['OS == "android" and gtest_target_type == "shared_library"', {
'dependencies': [
'../testing/android/native_test.gyp:native_test_native_code',
],
}],
['os_posix == 1 and OS != "mac" and OS != "android"', {
'conditions': [
['linux_use_tcmalloc==1', {
'dependencies': [
'../base/allocator/allocator.gyp:allocator',
],
}],
],
}]
],
},
{
'target_name': 'test_support_ipc',
'type': 'static_library',
Expand Down
285 changes: 3 additions & 282 deletions ipc/ipc_tests.cc → ipc/ipc_channel_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include <string>
#include <utility>

#include "ipc/ipc_tests.h"

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/debug/debug_on_start_win.h"
Expand All @@ -33,106 +31,14 @@
#include "ipc/ipc_multiprocess_test.h"
#include "ipc/ipc_sender.h"
#include "ipc/ipc_switches.h"
#include "ipc/ipc_test_base.h"
#include "testing/multiprocess_func_list.h"

// Define to enable IPC performance testing instead of the regular unit tests
// #define PERFORMANCE_TEST

const char kTestClientChannel[] = "T1";
const char kReflectorChannel[] = "T2";
const char kFuzzerChannel[] = "F3";
const char kSyncSocketChannel[] = "S4";

const size_t kLongMessageStringNumBytes = 50000;

void IPCChannelTest::SetUp() {
MultiProcessTest::SetUp();

// Construct a fresh IO Message loop for the duration of each test.
message_loop_ = new MessageLoopForIO();
}

void IPCChannelTest::TearDown() {
delete message_loop_;
message_loop_ = NULL;

MultiProcessTest::TearDown();
}

#if defined(OS_WIN)
base::ProcessHandle IPCChannelTest::SpawnChild(ChildType child_type,
IPC::Channel *channel) {
// kDebugChildren support.
bool debug_on_start =
CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren);

switch (child_type) {
case TEST_CLIENT:
return MultiProcessTest::SpawnChild("RunTestClient", debug_on_start);
case TEST_REFLECTOR:
return MultiProcessTest::SpawnChild("RunReflector", debug_on_start);
case FUZZER_SERVER:
return MultiProcessTest::SpawnChild("RunFuzzServer", debug_on_start);
case SYNC_SOCKET_SERVER:
return MultiProcessTest::SpawnChild("RunSyncSocketServer", debug_on_start);
default:
return NULL;
}
}
#elif defined(OS_POSIX)
base::ProcessHandle IPCChannelTest::SpawnChild(ChildType child_type,
IPC::Channel *channel) {
// kDebugChildren support.
bool debug_on_start =
CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren);

base::FileHandleMappingVector fds_to_map;
const int ipcfd = channel->GetClientFileDescriptor();
if (ipcfd > -1) {
fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3));
}

base::ProcessHandle ret = base::kNullProcessHandle;
switch (child_type) {
case TEST_CLIENT:
ret = MultiProcessTest::SpawnChild("RunTestClient",
fds_to_map,
debug_on_start);
break;
case TEST_DESCRIPTOR_CLIENT:
ret = MultiProcessTest::SpawnChild("RunTestDescriptorClient",
fds_to_map,
debug_on_start);
break;
case TEST_DESCRIPTOR_CLIENT_SANDBOXED:
ret = MultiProcessTest::SpawnChild("RunTestDescriptorClientSandboxed",
fds_to_map,
debug_on_start);
break;
case TEST_REFLECTOR:
ret = MultiProcessTest::SpawnChild("RunReflector",
fds_to_map,
debug_on_start);
break;
case FUZZER_SERVER:
ret = MultiProcessTest::SpawnChild("RunFuzzServer",
fds_to_map,
debug_on_start);
break;
case SYNC_SOCKET_SERVER:
ret = MultiProcessTest::SpawnChild("RunSyncSocketServer",
fds_to_map,
debug_on_start);
break;
default:
return base::kNullProcessHandle;
break;
}
return ret;
}
#endif // defined(OS_POSIX)
class IPCChannelTest : public IPCTestBase {
};

#ifndef PERFORMANCE_TEST
TEST_F(IPCChannelTest, BasicMessageTest) {
int v1 = 10;
std::string v2("foobar");
Expand Down Expand Up @@ -428,188 +334,3 @@ MULTIPROCESS_IPC_TEST_MAIN(RunTestClient) {
MessageLoop::current()->Run();
return 0;
}

#endif // !PERFORMANCE_TEST

#ifdef PERFORMANCE_TEST

//-----------------------------------------------------------------------------
// Manually performance test
//
// This test times the roundtrip IPC message cycle. It is enabled with a
// special preprocessor define to enable it instead of the standard IPC
// unit tests. This works around some funny termination conditions in the
// regular unit tests.
//
// This test is not automated. To test, you will want to vary the message
// count and message size in TEST to get the numbers you want.
//
// FIXME(brettw): Automate this test and have it run by default.

// This channel listener just replies to all messages with the exact same
// message. It assumes each message has one string parameter. When the string
// "quit" is sent, it will exit.
class ChannelReflectorListener : public IPC::Listener {
public:
explicit ChannelReflectorListener(IPC::Channel *channel) :
channel_(channel),
count_messages_(0) {
std::cout << "Reflector up" << std::endl;
}

~ChannelReflectorListener() {
std::cout << "Client Messages: " << count_messages_ << std::endl;
std::cout << "Client Latency: " << latency_messages_.InMilliseconds()
<< std::endl;
}

virtual bool OnMessageReceived(const IPC::Message& message) {
count_messages_++;
PickleIterator iter(message);
int64 time_internal;
EXPECT_TRUE(iter.ReadInt64(&time_internal));
int msgid;
EXPECT_TRUE(iter.ReadInt(&msgid));
std::string payload;
EXPECT_TRUE(iter.ReadString(&payload));
// TODO(vtl): Should we use |HighResNow()| instead of |Now()|?
latency_messages_ += base::TimeTicks::Now() -
base::TimeTicks::FromInternalValue(time_internal);

// cout << "reflector msg received: " << msgid << endl;
if (payload == "quit")
MessageLoop::current()->Quit();

IPC::Message* msg = new IPC::Message(0,
2,
IPC::Message::PRIORITY_NORMAL);
msg->WriteInt64(base::TimeTicks::Now().ToInternalValue());
msg->WriteInt(msgid);
msg->WriteString(payload);
channel_->Send(msg);
return true;
}

private:
IPC::Channel *channel_;
int count_messages_;
base::TimeDelta latency_messages_;
};

class ChannelPerfListener : public IPC::Listener {
public:
ChannelPerfListener(IPC::Channel* channel, int msg_count, int msg_size) :
count_down_(msg_count),
channel_(channel),
count_messages_(0) {
payload_.resize(msg_size);
for (int i = 0; i < static_cast<int>(payload_.size()); i++)
payload_[i] = 'a';
std::cout << "perflistener up" << std::endl;
}

~ChannelPerfListener() {
std::cout << "Server Messages: " << count_messages_ << std::endl;
std::cout << "Server Latency: " << latency_messages_.InMilliseconds()
<< std::endl;
}

virtual bool OnMessageReceived(const IPC::Message& message) {
count_messages_++;
// Decode the string so this gets counted in the total time.
PickleIterator iter(message);
int64 time_internal;
EXPECT_TRUE(iter.ReadInt64(&time_internal));
int msgid;
EXPECT_TRUE(iter.ReadInt(&msgid));
std::string cur;
EXPECT_TRUE(iter.ReadString(&cur));
latency_messages_ += base::TimeTicks::Now() -
base::TimeTicks::FromInternalValue(time_internal);

count_down_--;
if (count_down_ == 0) {
IPC::Message* msg = new IPC::Message(0,
2,
IPC::Message::PRIORITY_NORMAL);
msg->WriteInt64(base::TimeTicks::Now().ToInternalValue());
msg->WriteInt(count_down_);
msg->WriteString("quit");
channel_->Send(msg);

MessageLoop::current()->QuitWhenIdle();
return true;
}

IPC::Message* msg = new IPC::Message(0,
2,
IPC::Message::PRIORITY_NORMAL);
msg->WriteInt64(base::TimeTicks::Now().ToInternalValue());
msg->WriteInt(count_down_);
msg->WriteString(payload_);
channel_->Send(msg);
return true;
}

private:
int count_down_;
std::string payload_;
IPC::Channel *channel_;
int count_messages_;
base::TimeDelta latency_messages_;
};

TEST_F(IPCChannelTest, Performance) {
// setup IPC channel
IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_SERVER, NULL);
ChannelPerfListener perf_listener(&chan, 10000, 100000);
chan.set_listener(&perf_listener);
ASSERT_TRUE(chan.Connect());

base::ProcessHandle process_handle = SpawnChild(TEST_REFLECTOR, &chan);
ASSERT_TRUE(process_handle);

base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));

PerfTimeLogger logger("IPC_Perf");

// this initial message will kick-start the ping-pong of messages
IPC::Message* message = new IPC::Message(0,
2,
IPC::Message::PRIORITY_NORMAL);
message->WriteInt64(base::TimeTicks::Now().ToInternalValue());
message->WriteInt(-1);
message->WriteString("Hello");
chan.Send(message);

// run message loop
MessageLoop::current()->Run();

// Clean up child process.
EXPECT_TRUE(base::WaitForSingleProcess(
process_handle, base::TimeDelta::FromSeconds(5)));
base::CloseProcessHandle(process_handle);
}

// This message loop bounces all messages back to the sender
MULTIPROCESS_IPC_TEST_MAIN(RunReflector) {
MessageLoopForIO main_message_loop;
IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_CLIENT, NULL);
ChannelReflectorListener channel_reflector_listener(&chan);
chan.set_listener(&channel_reflector_listener);
CHECK(chan.Connect());

MessageLoop::current()->Run();
return 0;
}

#endif // PERFORMANCE_TEST

int main(int argc, char** argv) {
#ifdef PERFORMANCE_TEST
int retval = base::PerfTestSuite(argc, argv).Run();
#else
int retval = base::TestSuite(argc, argv).Run();
#endif
return retval;
}
Loading

0 comments on commit 0cb7d8c

Please sign in to comment.