OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
viettrungluu
2014/08/25 20:32:45
nit: 2014
Hajime Morrita
2014/08/26 18:16:26
Done.
| |
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 "build/build_config.h" // TODO(vtl): Remove this. | |
viettrungluu
2014/08/25 20:32:45
I don't think you need this include.
Hajime Morrita
2014/08/26 18:16:26
Done.
| |
21 #include "mojo/common/test/test_utils.h" | |
22 #include "mojo/embedder/scoped_platform_handle.h" | |
23 #include "mojo/system/channel.h" | |
24 #include "mojo/system/local_message_pipe_endpoint.h" | |
25 #include "mojo/system/message_pipe.h" | |
26 #include "mojo/system/message_pipe_test_utils.h" | |
27 #include "mojo/system/proxy_message_pipe_endpoint.h" | |
28 #include "mojo/system/raw_channel.h" | |
29 #include "mojo/system/test_utils.h" | |
30 #include "testing/gtest/include/gtest/gtest.h" | |
31 | |
32 namespace mojo { | |
33 namespace system { | |
34 namespace { | |
35 | |
36 class MultiprocessMessagePipePerfTest | |
37 : public test::MultiprocessMessagePipeTestBase { | |
38 public: | |
39 MultiprocessMessagePipePerfTest() : message_count_(0), message_size_(0) {} | |
40 | |
41 void SetupMeasurement(int message_count, size_t message_size) { | |
viettrungluu
2014/08/25 20:32:46
nit: "Setup" -> "SetUp" (the verb is always "set u
Hajime Morrita
2014/08/26 18:16:26
Done.
| |
42 message_count_ = message_count; | |
43 message_size_ = message_size; | |
44 payload_ = Pickle(); | |
45 payload_.WriteString(std::string(message_size, '*')); | |
46 read_buffer_.resize(message_size * 2); | |
47 } | |
48 | |
49 protected: | |
50 void WriteWaitThenRead(scoped_refptr<MessagePipe> mp) { | |
51 CHECK_EQ(mp->WriteMessage(0, | |
52 UserPointer<const void>(payload_.data()), | |
53 payload_.size(), | |
54 NULL, | |
55 MOJO_WRITE_MESSAGE_FLAG_NONE), | |
56 MOJO_RESULT_OK); | |
57 HandleSignalsState hss; | |
58 CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss), | |
59 MOJO_RESULT_OK); | |
60 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer_.size()); | |
61 CHECK_EQ(mp->ReadMessage(0, | |
62 UserPointer<void>(&read_buffer_[0]), | |
63 MakeUserPointer(&read_buffer_size), | |
64 NULL, | |
65 NULL, | |
66 MOJO_READ_MESSAGE_FLAG_NONE), | |
67 MOJO_RESULT_OK); | |
68 CHECK_EQ(read_buffer_size, payload_.size()); | |
69 } | |
70 | |
71 void SendQuitMessage(scoped_refptr<MessagePipe> mp) { | |
72 CHECK_EQ(mp->WriteMessage(0, | |
73 UserPointer<const void>(""), | |
74 0, | |
75 NULL, | |
76 MOJO_WRITE_MESSAGE_FLAG_NONE), | |
77 MOJO_RESULT_OK); | |
78 } | |
79 | |
80 void Measure(scoped_refptr<MessagePipe> mp) { | |
81 // Have one ping-pong to ensure channel being established. | |
82 WriteWaitThenRead(mp); | |
83 | |
84 std::string test_name = | |
85 base::StringPrintf("IPC_Perf_%dx_%u", | |
86 message_count_, | |
87 static_cast<unsigned>(message_size_)); | |
88 base::PerfTimeLogger logger(test_name.c_str()); | |
89 | |
90 for (int i = 0; i < message_count_; ++i) | |
91 WriteWaitThenRead(mp); | |
92 | |
93 logger.Done(); | |
94 } | |
95 | |
96 private: | |
97 int message_count_; | |
98 size_t message_size_; | |
99 Pickle payload_; | |
100 std::string read_buffer_; | |
101 scoped_ptr<base::PerfTimeLogger> perf_logger_; | |
102 }; | |
103 | |
104 // For each message received, sends a reply message with the same contents | |
105 // repeated twice, until the other end is closed or it receives "quitquitquit" | |
106 // (which it doesn't reply to). It'll return the number of messages received, | |
107 // not including any "quitquitquit" message, modulo 100. | |
108 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PerfClient) { | |
109 embedder::SimplePlatformSupport platform_support; | |
110 test::ChannelThread channel_thread(&platform_support); | |
111 embedder::ScopedPlatformHandle client_platform_handle = | |
112 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | |
113 CHECK(client_platform_handle.is_valid()); | |
114 scoped_refptr<MessagePipe> mp(new MessagePipe( | |
115 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), | |
116 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); | |
117 channel_thread.Start(client_platform_handle.Pass(), mp); | |
118 | |
119 std::string buffer(1000000, '\0'); | |
120 int rv = 0; | |
121 while (true) { | |
122 // Wait for our end of the message pipe to be readable. | |
123 HandleSignalsState hss; | |
124 MojoResult result = | |
125 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss); | |
126 if (result != MOJO_RESULT_OK) { | |
127 rv = result; | |
128 break; | |
129 } | |
130 | |
131 uint32_t read_size = static_cast<uint32_t>(buffer.size()); | |
132 CHECK_EQ(mp->ReadMessage(0, | |
133 UserPointer<void>(&buffer[0]), | |
134 MakeUserPointer(&read_size), | |
135 NULL, | |
136 NULL, | |
137 MOJO_READ_MESSAGE_FLAG_NONE), | |
138 MOJO_RESULT_OK); | |
139 | |
140 // Empty message indicates quitting | |
141 if (0 == read_size) | |
142 break; | |
143 | |
144 CHECK_EQ(mp->WriteMessage(0, | |
145 UserPointer<const void>(&buffer[0]), | |
146 static_cast<uint32_t>(read_size), | |
147 NULL, | |
148 MOJO_WRITE_MESSAGE_FLAG_NONE), | |
149 MOJO_RESULT_OK); | |
150 } | |
151 | |
152 mp->Close(0); | |
153 return rv; | |
154 } | |
155 | |
156 // Sends a bunch of messages to the child. Expects them "repeated" back. Waits | |
viettrungluu
2014/08/25 20:32:45
This description isn't entirely accurate. This tes
Hajime Morrita
2014/08/26 18:16:26
That's true. Updated the comment. I'll add Channel
| |
157 // for the child to close its end before quitting. | |
158 TEST_F(MultiprocessMessagePipePerfTest, Performance) { | |
viettrungluu
2014/08/25 20:32:45
Possibly you should have a slightly more descripti
Hajime Morrita
2014/08/26 18:16:26
Done.
| |
159 helper()->StartChild("PerfClient"); | |
160 | |
161 scoped_refptr<MessagePipe> mp(new MessagePipe( | |
162 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), | |
163 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); | |
164 Init(mp); | |
165 | |
166 // This values are set to align with one at ipc_pertests.cc for comparison. | |
167 const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832}; | |
168 const int kMessageCount[5] = {50000, 50000, 50000, 12000, 1000}; | |
169 | |
170 for (size_t i = 0; i < 5; i++) { | |
171 SetupMeasurement(kMessageCount[i], kMsgSize[i]); | |
172 Measure(mp); | |
173 } | |
174 | |
175 SendQuitMessage(mp); | |
176 mp->Close(0); | |
177 EXPECT_EQ(0, helper()->WaitForChildShutdown()); | |
178 } | |
179 | |
180 } // namespace | |
181 } // namespace system | |
182 } // namespace mojo | |
OLD | NEW |