child_call_stack_profile_collector_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2016 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. #include "components/metrics/child_call_stack_profile_collector.h"
  5. #include <memory>
  6. #include <utility>
  7. #include <vector>
  8. #include "base/bind.h"
  9. #include "base/run_loop.h"
  10. #include "base/test/task_environment.h"
  11. #include "base/time/time.h"
  12. #include "components/metrics/public/mojom/call_stack_profile_collector.mojom.h"
  13. #include "mojo/public/cpp/bindings/pending_receiver.h"
  14. #include "mojo/public/cpp/bindings/receiver.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "third_party/metrics_proto/sampled_profile.pb.h"
  17. namespace metrics {
  18. class ChildCallStackProfileCollectorTest : public ::testing::Test {
  19. protected:
  20. struct ReceivedProfile {
  21. base::TimeTicks start_timestamp;
  22. mojom::ProfileType profile_type;
  23. };
  24. class Receiver : public mojom::CallStackProfileCollector {
  25. public:
  26. explicit Receiver(
  27. mojo::PendingReceiver<mojom::CallStackProfileCollector> receiver)
  28. : receiver_(this, std::move(receiver)) {}
  29. Receiver(const Receiver&) = delete;
  30. Receiver& operator=(const Receiver&) = delete;
  31. ~Receiver() override {}
  32. void Collect(base::TimeTicks start_timestamp,
  33. mojom::ProfileType profile_type,
  34. mojom::SampledProfilePtr profile) override {
  35. profiles_.push_back({start_timestamp, profile_type});
  36. }
  37. std::vector<ReceivedProfile>& profiles() { return profiles_; }
  38. private:
  39. mojo::Receiver<mojom::CallStackProfileCollector> receiver_;
  40. std::vector<ReceivedProfile> profiles_;
  41. };
  42. ChildCallStackProfileCollectorTest()
  43. : receiver_impl_(std::make_unique<Receiver>(
  44. collector_remote_.InitWithNewPipeAndPassReceiver())) {}
  45. ChildCallStackProfileCollectorTest(
  46. const ChildCallStackProfileCollectorTest&) = delete;
  47. ChildCallStackProfileCollectorTest& operator=(
  48. const ChildCallStackProfileCollectorTest&) = delete;
  49. // Collects a profile and returns its start timestamp.
  50. base::TimeTicks CollectProfile(SampledProfile::TriggerEvent trigger_event) {
  51. base::TimeTicks start_timestamp = task_environment_.NowTicks();
  52. SampledProfile profile;
  53. profile.set_trigger_event(trigger_event);
  54. child_collector_.Collect(start_timestamp, std::move(profile));
  55. return start_timestamp;
  56. }
  57. void ExpectProfile(const ReceivedProfile& profile,
  58. base::TimeTicks expected_start_timestamp,
  59. mojom::ProfileType expected_profile_type) {
  60. EXPECT_EQ(expected_start_timestamp, profile.start_timestamp);
  61. EXPECT_EQ(expected_profile_type, profile.profile_type);
  62. }
  63. void ExpectProfile(
  64. const ChildCallStackProfileCollector::ProfileState& profile,
  65. base::TimeTicks expected_start_timestamp,
  66. mojom::ProfileType expected_profile_type) {
  67. EXPECT_EQ(expected_start_timestamp, profile.start_timestamp);
  68. EXPECT_EQ(expected_profile_type, profile.profile_type);
  69. }
  70. const std::vector<ChildCallStackProfileCollector::ProfileState>& profiles()
  71. const {
  72. return child_collector_.profiles_;
  73. }
  74. base::test::SingleThreadTaskEnvironment task_environment_{
  75. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  76. mojo::PendingRemote<mojom::CallStackProfileCollector> collector_remote_;
  77. std::unique_ptr<Receiver> receiver_impl_;
  78. ChildCallStackProfileCollector child_collector_;
  79. };
  80. // Test the behavior when an interface is provided.
  81. TEST_F(ChildCallStackProfileCollectorTest, InterfaceProvided) {
  82. EXPECT_EQ(0u, profiles().size());
  83. // Add a profile before providing the interface.
  84. base::TimeTicks start_timestamp =
  85. CollectProfile(SampledProfile::PERIODIC_COLLECTION);
  86. ASSERT_EQ(1u, profiles().size());
  87. ExpectProfile(profiles()[0], start_timestamp, mojom::ProfileType::kCPU);
  88. // Set the interface. The profiles should be passed to it.
  89. child_collector_.SetParentProfileCollector(std::move(collector_remote_));
  90. task_environment_.FastForwardBy(base::Milliseconds(1));
  91. EXPECT_EQ(0u, profiles().size());
  92. ASSERT_EQ(1u, receiver_impl_->profiles().size());
  93. ExpectProfile(receiver_impl_->profiles()[0], start_timestamp,
  94. mojom::ProfileType::kCPU);
  95. // Add a profile after providing the interface. It should also be passed.
  96. receiver_impl_->profiles().clear();
  97. base::TimeTicks start_timestamp2 =
  98. CollectProfile(SampledProfile::PERIODIC_COLLECTION);
  99. task_environment_.FastForwardBy(base::Milliseconds(1));
  100. EXPECT_EQ(0u, profiles().size());
  101. ASSERT_EQ(1u, receiver_impl_->profiles().size());
  102. ExpectProfile(receiver_impl_->profiles()[0], start_timestamp2,
  103. mojom::ProfileType::kCPU);
  104. }
  105. TEST_F(ChildCallStackProfileCollectorTest, InterfaceNotProvided) {
  106. EXPECT_EQ(0u, profiles().size());
  107. // Add a profile before providing a null interface.
  108. base::TimeTicks start_timestamp =
  109. CollectProfile(SampledProfile::PERIODIC_COLLECTION);
  110. ASSERT_EQ(1u, profiles().size());
  111. ExpectProfile(profiles()[0], start_timestamp, mojom::ProfileType::kCPU);
  112. // Set the null interface. The profile should be flushed.
  113. child_collector_.SetParentProfileCollector(mojo::NullRemote());
  114. task_environment_.FastForwardBy(base::Milliseconds(1));
  115. EXPECT_EQ(0u, profiles().size());
  116. EXPECT_EQ(0u, receiver_impl_->profiles().size());
  117. // Add a profile after providing a null interface. They should also be
  118. // flushed.
  119. CollectProfile(SampledProfile::PERIODIC_COLLECTION);
  120. task_environment_.FastForwardBy(base::Milliseconds(1));
  121. EXPECT_EQ(0u, profiles().size());
  122. EXPECT_EQ(0u, receiver_impl_->profiles().size());
  123. }
  124. // Tests that the `profile_type` parameter is set correctly when heap profiles
  125. // pass through the interface.
  126. TEST_F(ChildCallStackProfileCollectorTest, HeapProfiles) {
  127. EXPECT_EQ(0u, profiles().size());
  128. // Add a profile before providing the interface.
  129. base::TimeTicks start_timestamp =
  130. CollectProfile(SampledProfile::PERIODIC_HEAP_COLLECTION);
  131. ASSERT_EQ(1u, profiles().size());
  132. ExpectProfile(profiles()[0], start_timestamp, mojom::ProfileType::kHeap);
  133. // Set the interface. The profile type should pass to it unchanged.
  134. child_collector_.SetParentProfileCollector(std::move(collector_remote_));
  135. task_environment_.FastForwardBy(base::Milliseconds(1));
  136. ASSERT_EQ(1u, receiver_impl_->profiles().size());
  137. ExpectProfile(receiver_impl_->profiles()[0], start_timestamp,
  138. mojom::ProfileType::kHeap);
  139. }
  140. } // namespace metrics