| 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 <numeric> | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/timer/elapsed_timer.h" | |
| 10 #include "base/tracked_objects.h" | |
| 11 #include "mojo/message_pump/message_pump_mojo.h" | |
| 12 #include "mojo/public/cpp/bindings/binding.h" | |
| 13 #include "mojo/public/cpp/environment/environment.h" | |
| 14 #include "mojo/public/cpp/environment/task_tracker.h" | |
| 15 #include "mojo/public/cpp/test_support/test_support.h" | |
| 16 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 namespace mojo { | |
| 20 namespace test { | |
| 21 namespace { | |
| 22 | |
| 23 class ProviderImpl : public sample::Provider { | |
| 24 public: | |
| 25 explicit ProviderImpl(InterfaceRequest<sample::Provider> request) | |
| 26 : binding_(this, request.Pass()) {} | |
| 27 | |
| 28 void EchoString(const String& a, | |
| 29 const Callback<void(String)>& callback) override { | |
| 30 callback.Run(a); | |
| 31 } | |
| 32 | |
| 33 void EchoStrings(const String& a, | |
| 34 const String& b, | |
| 35 const Callback<void(String, String)>& callback) override { | |
| 36 CHECK(false); | |
| 37 } | |
| 38 | |
| 39 void EchoMessagePipeHandle( | |
| 40 ScopedMessagePipeHandle a, | |
| 41 const Callback<void(ScopedMessagePipeHandle)>& callback) override { | |
| 42 CHECK(false); | |
| 43 } | |
| 44 | |
| 45 void EchoEnum(sample::Enum a, | |
| 46 const Callback<void(sample::Enum)>& callback) override { | |
| 47 CHECK(false); | |
| 48 } | |
| 49 | |
| 50 void EchoInt(int32_t a, const EchoIntCallback& callback) override { | |
| 51 CHECK(false); | |
| 52 } | |
| 53 | |
| 54 Binding<sample::Provider> binding_; | |
| 55 }; | |
| 56 | |
| 57 class RequestResponsePerfTest : public testing::Test { | |
| 58 public: | |
| 59 RequestResponsePerfTest() | |
| 60 : loop_(make_scoped_ptr(new common::MessagePumpMojo())) {} | |
| 61 | |
| 62 ~RequestResponsePerfTest() override { loop_.RunUntilIdle(); } | |
| 63 | |
| 64 void Iterate(size_t count); | |
| 65 void Measure(const char* case_nam); | |
| 66 | |
| 67 void SetUp() override { | |
| 68 tracked_objects::ThreadData::InitializeAndSetTrackingStatus( | |
| 69 tracked_objects::ThreadData::PROFILING_ACTIVE); | |
| 70 } | |
| 71 | |
| 72 void TearDown() override { | |
| 73 tracked_objects::ThreadData::InitializeAndSetTrackingStatus( | |
| 74 tracked_objects::ThreadData::DEACTIVATED); | |
| 75 } | |
| 76 | |
| 77 void PumpMessages() { loop_.RunUntilIdle(); } | |
| 78 | |
| 79 private: | |
| 80 base::MessageLoop loop_; | |
| 81 }; | |
| 82 | |
| 83 const size_t kCallsPerIteration = 1000; | |
| 84 const size_t kIterations = 1000; | |
| 85 | |
| 86 void RequestResponsePerfTest::Iterate(size_t count) { | |
| 87 sample::ProviderPtr provider; | |
| 88 ProviderImpl provider_impl(GetProxy(&provider)); | |
| 89 | |
| 90 size_t remaining = count; | |
| 91 Callback<void(const String&)> reply = | |
| 92 [&provider, &reply, &remaining](const String& a) { | |
| 93 if (!remaining) | |
| 94 return; | |
| 95 remaining--; | |
| 96 provider->EchoString(a, reply); | |
| 97 }; | |
| 98 | |
| 99 provider->EchoString(String::From("hello"), reply); | |
| 100 PumpMessages(); | |
| 101 } | |
| 102 | |
| 103 void RequestResponsePerfTest::Measure(const char* case_name) { | |
| 104 std::vector<double> laps; | |
| 105 for (size_t i = 0; i < kIterations; ++i) { | |
| 106 base::ElapsedTimer timer; | |
| 107 Iterate(kCallsPerIteration); | |
| 108 laps.push_back(timer.Elapsed().InMillisecondsF()); | |
| 109 } | |
| 110 | |
| 111 double avg = std::accumulate(laps.begin(), laps.end(), 0.0) / laps.size(); | |
| 112 double var = std::accumulate(laps.begin(), laps.end(), 0.0, [avg](double acc, | |
| 113 double x) { | |
| 114 return acc + (x - avg) * (x - avg); | |
| 115 }) / laps.size(); | |
| 116 | |
| 117 double sd = sqrt(var); | |
| 118 mojo::test::LogPerfResult(case_name, "Avg", avg, "ms/1000call"); | |
| 119 mojo::test::LogPerfResult(case_name, "SD", sd, "ms/1000call"); | |
| 120 } | |
| 121 | |
| 122 TEST_F(RequestResponsePerfTest, TrackingEnabled) { | |
| 123 Environment::GetDefaultTaskTracker()->SetEnabled(true); | |
| 124 Measure(__FUNCTION__); | |
| 125 Environment::GetDefaultTaskTracker()->SetEnabled(false); | |
| 126 } | |
| 127 | |
| 128 TEST_F(RequestResponsePerfTest, TrackingDisabled) { | |
| 129 Measure(__FUNCTION__); | |
| 130 } | |
| 131 | |
| 132 } // namespace | |
| 133 } // namespace test | |
| 134 } // namespace mojo | |
| OLD | NEW |