OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "gtest/gtest.h" | |
6 #include "mojo/public/cpp/bindings/binding.h" | |
7 #include "mojo/public/cpp/test_support/test_support.h" | |
8 #include "mojo/public/cpp/utility/run_loop.h" | |
9 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" | |
10 | |
11 namespace mojo { | |
12 namespace { | |
13 | |
14 const double kMojoTicksPerSecond = 1000000.0; | |
15 | |
16 double MojoTicksToSeconds(MojoTimeTicks ticks) { | |
17 return ticks / kMojoTicksPerSecond; | |
18 } | |
19 | |
20 class PingServiceImpl : public test::PingService { | |
21 public: | |
22 explicit PingServiceImpl() {} | |
23 ~PingServiceImpl() override {} | |
24 | |
25 // |PingService| methods: | |
26 void Ping(const Callback<void()>& callback) override; | |
27 | |
28 private: | |
29 MOJO_DISALLOW_COPY_AND_ASSIGN(PingServiceImpl); | |
30 }; | |
31 | |
32 void PingServiceImpl::Ping(const Callback<void()>& callback) { | |
33 callback.Run(); | |
34 } | |
35 | |
36 class PingPongTest { | |
37 public: | |
38 explicit PingPongTest(test::PingServicePtr service); | |
39 | |
40 void Run(unsigned int iterations); | |
41 | |
42 private: | |
43 void OnPingDone(); | |
44 | |
45 test::PingServicePtr service_; | |
46 unsigned int iterations_to_run_; | |
47 unsigned int current_iterations_; | |
48 | |
49 MOJO_DISALLOW_COPY_AND_ASSIGN(PingPongTest); | |
50 }; | |
51 | |
52 PingPongTest::PingPongTest(test::PingServicePtr service) | |
53 : service_(service.Pass()) {} | |
54 | |
55 void PingPongTest::Run(unsigned int iterations) { | |
56 iterations_to_run_ = iterations; | |
57 current_iterations_ = 0; | |
58 | |
59 service_->Ping([this]() { OnPingDone(); }); | |
60 RunLoop::current()->Run(); | |
61 } | |
62 | |
63 void PingPongTest::OnPingDone() { | |
64 current_iterations_++; | |
65 if (current_iterations_ >= iterations_to_run_) { | |
66 RunLoop::current()->Quit(); | |
67 return; | |
68 } | |
69 | |
70 service_->Ping([this]() { OnPingDone(); }); | |
71 } | |
72 | |
73 struct BoundPingService { | |
74 BoundPingService() : binding(&impl) { binding.Bind(GetProxy(&service)); } | |
75 | |
76 PingServiceImpl impl; | |
77 test::PingServicePtr service; | |
78 Binding<test::PingService> binding; | |
79 }; | |
80 | |
81 class MojoBindingsPerftest : public testing::Test { | |
82 protected: | |
83 RunLoop run_loop_; | |
84 }; | |
85 | |
86 TEST_F(MojoBindingsPerftest, InProcessPingPong) { | |
87 test::PingServicePtr service; | |
88 PingServiceImpl impl; | |
89 Binding<test::PingService> binding(&impl, GetProxy(&service)); | |
90 PingPongTest test(service.Pass()); | |
91 | |
92 { | |
93 const unsigned int kIterations = 100000; | |
94 const MojoTimeTicks start_time = MojoGetTimeTicksNow(); | |
95 test.Run(kIterations); | |
96 const MojoTimeTicks end_time = MojoGetTimeTicksNow(); | |
97 test::LogPerfResult("InProcessPingPong", "0_Inactive", | |
98 kIterations / MojoTicksToSeconds(end_time - start_time), | |
99 "pings/second"); | |
100 } | |
101 | |
102 { | |
103 const size_t kNumInactiveServices = 1000; | |
104 BoundPingService* inactive_services = | |
105 new BoundPingService[kNumInactiveServices]; | |
106 | |
107 const unsigned int kIterations = 10000; | |
108 const MojoTimeTicks start_time = MojoGetTimeTicksNow(); | |
109 test.Run(kIterations); | |
110 const MojoTimeTicks end_time = MojoGetTimeTicksNow(); | |
111 test::LogPerfResult("InProcessPingPong", "1000_Inactive", | |
112 kIterations / MojoTicksToSeconds(end_time - start_time), | |
113 "pings/second"); | |
114 | |
115 delete[] inactive_services; | |
116 } | |
117 } | |
118 | |
119 } // namespace | |
120 } // namespace mojo | |
OLD | NEW |