OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/time/time.h" |
11 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
12 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" | 13 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
13 #include "mojo/public/cpp/bindings/lib/message_builder.h" | 14 #include "mojo/public/cpp/bindings/lib/message_builder.h" |
14 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 15 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
15 #include "mojo/public/cpp/bindings/lib/router.h" | 16 #include "mojo/public/cpp/bindings/lib/router.h" |
16 #include "mojo/public/cpp/bindings/message.h" | 17 #include "mojo/public/cpp/bindings/message.h" |
17 #include "mojo/public/cpp/test_support/test_support.h" | 18 #include "mojo/public/cpp/test_support/test_support.h" |
18 #include "mojo/public/cpp/test_support/test_utils.h" | 19 #include "mojo/public/cpp/test_support/test_utils.h" |
19 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" | 20 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 public: | 143 public: |
143 PingPongPaddle(MessageReceiver* sender) : sender_(sender) {} | 144 PingPongPaddle(MessageReceiver* sender) : sender_(sender) {} |
144 | 145 |
145 void set_sender(MessageReceiver* sender) { sender_ = sender; } | 146 void set_sender(MessageReceiver* sender) { sender_ = sender; } |
146 | 147 |
147 bool Accept(Message* message) override { | 148 bool Accept(Message* message) override { |
148 uint32_t count = message->header()->name; | 149 uint32_t count = message->header()->name; |
149 if (!quit_closure_.is_null()) { | 150 if (!quit_closure_.is_null()) { |
150 count++; | 151 count++; |
151 if (count >= expected_count_) { | 152 if (count >= expected_count_) { |
| 153 end_time_ = base::TimeTicks::Now(); |
152 quit_closure_.Run(); | 154 quit_closure_.Run(); |
153 return true; | 155 return true; |
154 } | 156 } |
155 } | 157 } |
156 | 158 |
157 internal::MessageBuilder builder(count, 8); | 159 internal::MessageBuilder builder(count, 8); |
158 bool result = sender_->Accept(builder.message()); | 160 bool result = sender_->Accept(builder.message()); |
159 DCHECK(result); | 161 DCHECK(result); |
160 return true; | 162 return true; |
161 } | 163 } |
162 | 164 |
163 bool AcceptWithResponder(Message* message, | 165 bool AcceptWithResponder(Message* message, |
164 MessageReceiverWithStatus* responder) override { | 166 MessageReceiverWithStatus* responder) override { |
165 NOTREACHED(); | 167 NOTREACHED(); |
166 return true; | 168 return true; |
167 } | 169 } |
168 | 170 |
169 void Serve(uint32_t expected_count) { | 171 base::TimeDelta Serve(uint32_t expected_count) { |
170 base::RunLoop run_loop; | 172 base::RunLoop run_loop; |
171 | 173 |
172 expected_count_ = expected_count; | 174 expected_count_ = expected_count; |
173 quit_closure_ = run_loop.QuitClosure(); | 175 quit_closure_ = run_loop.QuitClosure(); |
174 | 176 |
| 177 start_time_ = base::TimeTicks::Now(); |
175 internal::MessageBuilder builder(0, 8); | 178 internal::MessageBuilder builder(0, 8); |
176 bool result = sender_->Accept(builder.message()); | 179 bool result = sender_->Accept(builder.message()); |
177 DCHECK(result); | 180 DCHECK(result); |
178 | 181 |
179 run_loop.Run(); | 182 run_loop.Run(); |
| 183 |
| 184 return end_time_ - start_time_; |
180 } | 185 } |
181 | 186 |
182 private: | 187 private: |
| 188 base::TimeTicks start_time_; |
| 189 base::TimeTicks end_time_; |
183 uint32_t expected_count_ = 0; | 190 uint32_t expected_count_ = 0; |
184 MessageReceiver* sender_; | 191 MessageReceiver* sender_; |
185 base::Closure quit_closure_; | 192 base::Closure quit_closure_; |
186 }; | 193 }; |
187 | 194 |
188 TEST_F(MojoBindingsPerftest, RouterPingPong) { | 195 TEST_F(MojoBindingsPerftest, RouterPingPong) { |
189 MessagePipe pipe; | 196 MessagePipe pipe; |
190 internal::Router router0(std::move(pipe.handle0), FilterChain(), false, | 197 internal::Router router0(std::move(pipe.handle0), FilterChain(), false, |
191 base::ThreadTaskRunnerHandle::Get(), 0u); | 198 base::ThreadTaskRunnerHandle::Get(), 0u); |
192 internal::Router router1(std::move(pipe.handle1), FilterChain(), false, | 199 internal::Router router1(std::move(pipe.handle1), FilterChain(), false, |
193 base::ThreadTaskRunnerHandle::Get(), 0u); | 200 base::ThreadTaskRunnerHandle::Get(), 0u); |
194 PingPongPaddle paddle0(&router0); | 201 PingPongPaddle paddle0(&router0); |
195 router0.set_incoming_receiver(&paddle0); | 202 router0.set_incoming_receiver(&paddle0); |
196 PingPongPaddle paddle1(&router1); | 203 PingPongPaddle paddle1(&router1); |
197 router1.set_incoming_receiver(&paddle1); | 204 router1.set_incoming_receiver(&paddle1); |
198 | 205 |
199 static const uint32_t kWarmUpIterations = 1000; | 206 static const uint32_t kWarmUpIterations = 1000; |
200 static const uint32_t kTestIterations = 100000; | 207 static const uint32_t kTestIterations = 1000000; |
201 | 208 |
202 paddle0.Serve(kWarmUpIterations); | 209 paddle0.Serve(kWarmUpIterations); |
203 | 210 |
204 const MojoTimeTicks start_time = MojoGetTimeTicksNow(); | 211 base::TimeDelta duration = paddle0.Serve(kTestIterations); |
205 paddle0.Serve(kTestIterations); | |
206 const MojoTimeTicks end_time = MojoGetTimeTicksNow(); | |
207 | 212 |
208 test::LogPerfResult( | 213 test::LogPerfResult("RouterPingPong", nullptr, |
209 "RouterPingPong", nullptr, | 214 kTestIterations / duration.InSecondsF(), "pings/second"); |
210 kTestIterations / MojoTicksToSeconds(end_time - start_time), | |
211 "pings/second"); | |
212 } | 215 } |
213 | 216 |
214 TEST_F(MojoBindingsPerftest, MultiplexRouterPingPong) { | 217 TEST_F(MojoBindingsPerftest, MultiplexRouterPingPong) { |
215 MessagePipe pipe; | 218 MessagePipe pipe; |
216 scoped_refptr<internal::MultiplexRouter> router0( | 219 scoped_refptr<internal::MultiplexRouter> router0( |
217 new internal::MultiplexRouter(true, std::move(pipe.handle0), | 220 new internal::MultiplexRouter(true, std::move(pipe.handle0), |
218 base::ThreadTaskRunnerHandle::Get())); | 221 base::ThreadTaskRunnerHandle::Get())); |
219 scoped_refptr<internal::MultiplexRouter> router1( | 222 scoped_refptr<internal::MultiplexRouter> router1( |
220 new internal::MultiplexRouter(false, std::move(pipe.handle1), | 223 new internal::MultiplexRouter(false, std::move(pipe.handle1), |
221 base::ThreadTaskRunnerHandle::Get())); | 224 base::ThreadTaskRunnerHandle::Get())); |
222 | 225 |
223 PingPongPaddle paddle0(nullptr); | 226 PingPongPaddle paddle0(nullptr); |
224 PingPongPaddle paddle1(nullptr); | 227 PingPongPaddle paddle1(nullptr); |
225 | 228 |
226 InterfaceEndpointClient client0( | 229 InterfaceEndpointClient client0( |
227 router0->CreateLocalEndpointHandle(kMasterInterfaceId), &paddle0, | 230 router0->CreateLocalEndpointHandle(kMasterInterfaceId), &paddle0, nullptr, |
228 base::MakeUnique<PassThroughFilter>(), false, | 231 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
229 base::ThreadTaskRunnerHandle::Get(), 0u); | |
230 InterfaceEndpointClient client1( | 232 InterfaceEndpointClient client1( |
231 router1->CreateLocalEndpointHandle(kMasterInterfaceId), &paddle1, | 233 router1->CreateLocalEndpointHandle(kMasterInterfaceId), &paddle1, nullptr, |
232 base::MakeUnique<PassThroughFilter>(), false, | 234 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
233 base::ThreadTaskRunnerHandle::Get(), 0u); | |
234 | 235 |
235 paddle0.set_sender(&client0); | 236 paddle0.set_sender(&client0); |
236 paddle1.set_sender(&client1); | 237 paddle1.set_sender(&client1); |
237 | 238 |
238 static const uint32_t kWarmUpIterations = 1000; | 239 static const uint32_t kWarmUpIterations = 1000; |
239 static const uint32_t kTestIterations = 100000; | 240 static const uint32_t kTestIterations = 1000000; |
240 | 241 |
241 paddle0.Serve(kWarmUpIterations); | 242 paddle0.Serve(kWarmUpIterations); |
242 | 243 |
243 const MojoTimeTicks start_time = MojoGetTimeTicksNow(); | 244 base::TimeDelta duration = paddle0.Serve(kTestIterations); |
244 paddle0.Serve(kTestIterations); | |
245 const MojoTimeTicks end_time = MojoGetTimeTicksNow(); | |
246 | 245 |
247 test::LogPerfResult( | 246 test::LogPerfResult("MultiplexRouterPingPong", nullptr, |
248 "MultiplexRouterPingPong", nullptr, | 247 kTestIterations / duration.InSecondsF(), "pings/second"); |
249 kTestIterations / MojoTicksToSeconds(end_time - start_time), | 248 } |
250 "pings/second"); | 249 |
| 250 class CounterReceiver : public MessageReceiverWithResponderStatus { |
| 251 public: |
| 252 bool Accept(Message* message) override { |
| 253 counter_++; |
| 254 return true; |
| 255 } |
| 256 |
| 257 bool AcceptWithResponder(Message* message, |
| 258 MessageReceiverWithStatus* responder) override { |
| 259 NOTREACHED(); |
| 260 return true; |
| 261 } |
| 262 |
| 263 uint32_t counter() const { return counter_; } |
| 264 |
| 265 void Reset() { counter_ = 0; } |
| 266 |
| 267 private: |
| 268 uint32_t counter_ = 0; |
| 269 }; |
| 270 |
| 271 TEST_F(MojoBindingsPerftest, RouterDispatchCost) { |
| 272 MessagePipe pipe; |
| 273 internal::Router router(std::move(pipe.handle0), FilterChain(), false, |
| 274 base::ThreadTaskRunnerHandle::Get(), 0u); |
| 275 CounterReceiver receiver; |
| 276 router.set_incoming_receiver(&receiver); |
| 277 |
| 278 static const uint32_t kIterations[] = {1000, 3000000}; |
| 279 |
| 280 for (size_t i = 0; i < 2; ++i) { |
| 281 receiver.Reset(); |
| 282 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 283 for (size_t j = 0; j < kIterations[i]; ++j) { |
| 284 internal::MessageBuilder builder(0, 8); |
| 285 bool result = |
| 286 router.SimulateReceivingMessageForTesting(builder.message()); |
| 287 DCHECK(result); |
| 288 } |
| 289 |
| 290 base::TimeTicks end_time = base::TimeTicks::Now(); |
| 291 base::TimeDelta duration = end_time - start_time; |
| 292 CHECK_EQ(kIterations[i], receiver.counter()); |
| 293 |
| 294 if (i == 1) { |
| 295 test::LogPerfResult("RouterDispatchCost", nullptr, |
| 296 kIterations[i] / duration.InSecondsF(), |
| 297 "times/second"); |
| 298 } |
| 299 } |
| 300 } |
| 301 |
| 302 TEST_F(MojoBindingsPerftest, MultiplexRouterDispatchCost) { |
| 303 MessagePipe pipe; |
| 304 scoped_refptr<internal::MultiplexRouter> router(new internal::MultiplexRouter( |
| 305 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); |
| 306 CounterReceiver receiver; |
| 307 InterfaceEndpointClient client( |
| 308 router->CreateLocalEndpointHandle(kMasterInterfaceId), &receiver, nullptr, |
| 309 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
| 310 |
| 311 static const uint32_t kIterations[] = {1000, 3000000}; |
| 312 |
| 313 for (size_t i = 0; i < 2; ++i) { |
| 314 receiver.Reset(); |
| 315 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 316 for (size_t j = 0; j < kIterations[i]; ++j) { |
| 317 internal::MessageBuilder builder(0, 8); |
| 318 bool result = |
| 319 router->SimulateReceivingMessageForTesting(builder.message()); |
| 320 DCHECK(result); |
| 321 } |
| 322 |
| 323 base::TimeTicks end_time = base::TimeTicks::Now(); |
| 324 base::TimeDelta duration = end_time - start_time; |
| 325 CHECK_EQ(kIterations[i], receiver.counter()); |
| 326 |
| 327 if (i == 1) { |
| 328 test::LogPerfResult("MultiplexRouterDispatchCost", nullptr, |
| 329 kIterations[i] / duration.InSecondsF(), |
| 330 "times/second"); |
| 331 } |
| 332 } |
251 } | 333 } |
252 | 334 |
253 } // namespace | 335 } // namespace |
254 } // namespace mojo | 336 } // namespace mojo |
OLD | NEW |