OLD | NEW |
| (Empty) |
1 // Copyright 2014 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. | |
4 | |
5 #include <stdint.h> | |
6 #include <stdio.h> | |
7 #include <string.h> | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/location.h" | |
14 #include "base/logging.h" | |
15 #include "base/macros.h" | |
16 #include "base/pickle.h" | |
17 #include "base/strings/stringprintf.h" | |
18 #include "base/test/perf_time_logger.h" | |
19 #include "base/time/time.h" | |
20 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
21 #include "mojo/edk/system/channel.h" | |
22 #include "mojo/edk/system/local_message_pipe_endpoint.h" | |
23 #include "mojo/edk/system/message_pipe.h" | |
24 #include "mojo/edk/system/message_pipe_test_utils.h" | |
25 #include "mojo/edk/system/proxy_message_pipe_endpoint.h" | |
26 #include "mojo/edk/system/raw_channel.h" | |
27 #include "mojo/edk/system/test_utils.h" | |
28 #include "mojo/edk/test/test_utils.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | |
30 | |
31 namespace mojo { | |
32 namespace system { | |
33 namespace { | |
34 | |
35 class MultiprocessMessagePipePerfTest | |
36 : public test::MultiprocessMessagePipeTestBase { | |
37 public: | |
38 MultiprocessMessagePipePerfTest() : message_count_(0), message_size_(0) {} | |
39 | |
40 void SetUpMeasurement(int message_count, size_t message_size) { | |
41 message_count_ = message_count; | |
42 message_size_ = message_size; | |
43 payload_ = Pickle(); | |
44 payload_.WriteString(std::string(message_size, '*')); | |
45 read_buffer_.resize(message_size * 2); | |
46 } | |
47 | |
48 protected: | |
49 void WriteWaitThenRead(scoped_refptr<MessagePipe> mp) { | |
50 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(payload_.data()), | |
51 static_cast<uint32_t>(payload_.size()), nullptr, | |
52 MOJO_WRITE_MESSAGE_FLAG_NONE), | |
53 MOJO_RESULT_OK); | |
54 HandleSignalsState hss; | |
55 CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss), | |
56 MOJO_RESULT_OK); | |
57 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer_.size()); | |
58 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer_[0]), | |
59 MakeUserPointer(&read_buffer_size), nullptr, | |
60 nullptr, MOJO_READ_MESSAGE_FLAG_NONE), | |
61 MOJO_RESULT_OK); | |
62 CHECK_EQ(read_buffer_size, static_cast<uint32_t>(payload_.size())); | |
63 } | |
64 | |
65 void SendQuitMessage(scoped_refptr<MessagePipe> mp) { | |
66 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(""), 0, nullptr, | |
67 MOJO_WRITE_MESSAGE_FLAG_NONE), | |
68 MOJO_RESULT_OK); | |
69 } | |
70 | |
71 void Measure(scoped_refptr<MessagePipe> mp) { | |
72 // Have one ping-pong to ensure channel being established. | |
73 WriteWaitThenRead(mp); | |
74 | |
75 std::string test_name = | |
76 base::StringPrintf("IPC_Perf_%dx_%u", message_count_, | |
77 static_cast<unsigned>(message_size_)); | |
78 base::PerfTimeLogger logger(test_name.c_str()); | |
79 | |
80 for (int i = 0; i < message_count_; ++i) | |
81 WriteWaitThenRead(mp); | |
82 | |
83 logger.Done(); | |
84 } | |
85 | |
86 private: | |
87 int message_count_; | |
88 size_t message_size_; | |
89 Pickle payload_; | |
90 std::string read_buffer_; | |
91 scoped_ptr<base::PerfTimeLogger> perf_logger_; | |
92 }; | |
93 | |
94 // For each message received, sends a reply message with the same contents | |
95 // repeated twice, until the other end is closed or it receives "quitquitquit" | |
96 // (which it doesn't reply to). It'll return the number of messages received, | |
97 // not including any "quitquitquit" message, modulo 100. | |
98 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PingPongClient) { | |
99 embedder::SimplePlatformSupport platform_support; | |
100 test::ChannelThread channel_thread(&platform_support); | |
101 embedder::ScopedPlatformHandle client_platform_handle = | |
102 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | |
103 CHECK(client_platform_handle.is_valid()); | |
104 scoped_refptr<ChannelEndpoint> ep; | |
105 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
106 channel_thread.Start(client_platform_handle.Pass(), ep); | |
107 | |
108 std::string buffer(1000000, '\0'); | |
109 int rv = 0; | |
110 while (true) { | |
111 // Wait for our end of the message pipe to be readable. | |
112 HandleSignalsState hss; | |
113 MojoResult result = | |
114 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss); | |
115 if (result != MOJO_RESULT_OK) { | |
116 rv = result; | |
117 break; | |
118 } | |
119 | |
120 uint32_t read_size = static_cast<uint32_t>(buffer.size()); | |
121 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&buffer[0]), | |
122 MakeUserPointer(&read_size), nullptr, nullptr, | |
123 MOJO_READ_MESSAGE_FLAG_NONE), | |
124 MOJO_RESULT_OK); | |
125 | |
126 // Empty message indicates quitting | |
127 if (0 == read_size) | |
128 break; | |
129 | |
130 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(&buffer[0]), | |
131 static_cast<uint32_t>(read_size), nullptr, | |
132 MOJO_WRITE_MESSAGE_FLAG_NONE), | |
133 MOJO_RESULT_OK); | |
134 } | |
135 | |
136 mp->Close(0); | |
137 return rv; | |
138 } | |
139 | |
140 // Repeatedly sends messages as previous one got replied by the child. | |
141 // Waits for the child to close its end before quitting once specified | |
142 // number of messages has been sent. | |
143 #if defined(OS_ANDROID) | |
144 // Android multi-process tests are not executing the new process. This is flaky. | |
145 #define MAYBE_PingPong DISABLED_PingPong | |
146 #else | |
147 #define MAYBE_PingPong PingPong | |
148 #endif // defined(OS_ANDROID) | |
149 TEST_F(MultiprocessMessagePipePerfTest, MAYBE_PingPong) { | |
150 helper()->StartChild("PingPongClient"); | |
151 | |
152 scoped_refptr<ChannelEndpoint> ep; | |
153 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
154 Init(ep); | |
155 | |
156 // This values are set to align with one at ipc_pertests.cc for comparison. | |
157 const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832}; | |
158 const int kMessageCount[5] = {50000, 50000, 50000, 12000, 1000}; | |
159 | |
160 for (size_t i = 0; i < 5; i++) { | |
161 SetUpMeasurement(kMessageCount[i], kMsgSize[i]); | |
162 Measure(mp); | |
163 } | |
164 | |
165 SendQuitMessage(mp); | |
166 mp->Close(0); | |
167 EXPECT_EQ(0, helper()->WaitForChildShutdown()); | |
168 } | |
169 | |
170 } // namespace | |
171 } // namespace system | |
172 } // namespace mojo | |
OLD | NEW |