File: child_call_stack_profile_collector_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (170 lines) | stat: -rw-r--r-- 6,519 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/child_call_stack_profile_collector.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/metrics/public/mojom/call_stack_profile_collector.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/sampled_profile.pb.h"

namespace metrics {

class ChildCallStackProfileCollectorTest : public ::testing::Test {
 protected:
  struct ReceivedProfile {
    base::TimeTicks start_timestamp;
    mojom::ProfileType profile_type;
  };

  class Receiver : public mojom::CallStackProfileCollector {
   public:
    explicit Receiver(
        mojo::PendingReceiver<mojom::CallStackProfileCollector> receiver)
        : receiver_(this, std::move(receiver)) {}

    Receiver(const Receiver&) = delete;
    Receiver& operator=(const Receiver&) = delete;

    ~Receiver() override {}

    void Collect(base::TimeTicks start_timestamp,
                 mojom::ProfileType profile_type,
                 mojom::SampledProfilePtr profile) override {
      profiles_.push_back({start_timestamp, profile_type});
    }

    std::vector<ReceivedProfile>& profiles() { return profiles_; }

   private:
    mojo::Receiver<mojom::CallStackProfileCollector> receiver_;
    std::vector<ReceivedProfile> profiles_;
  };

  ChildCallStackProfileCollectorTest()
      : receiver_impl_(std::make_unique<Receiver>(
            collector_remote_.InitWithNewPipeAndPassReceiver())) {}

  ChildCallStackProfileCollectorTest(
      const ChildCallStackProfileCollectorTest&) = delete;
  ChildCallStackProfileCollectorTest& operator=(
      const ChildCallStackProfileCollectorTest&) = delete;

  // Collects a profile and returns its start timestamp.
  base::TimeTicks CollectProfile(SampledProfile::TriggerEvent trigger_event) {
    base::TimeTicks start_timestamp = task_environment_.NowTicks();
    SampledProfile profile;
    profile.set_trigger_event(trigger_event);
    child_collector_.Collect(start_timestamp, std::move(profile));
    return start_timestamp;
  }

  void ExpectProfile(const ReceivedProfile& profile,
                     base::TimeTicks expected_start_timestamp,
                     mojom::ProfileType expected_profile_type) {
    EXPECT_EQ(expected_start_timestamp, profile.start_timestamp);
    EXPECT_EQ(expected_profile_type, profile.profile_type);
  }

  void ExpectProfile(
      const ChildCallStackProfileCollector::ProfileState& profile,
      base::TimeTicks expected_start_timestamp,
      mojom::ProfileType expected_profile_type) {
    EXPECT_EQ(expected_start_timestamp, profile.start_timestamp);
    EXPECT_EQ(expected_profile_type, profile.profile_type);
  }

  const std::vector<ChildCallStackProfileCollector::ProfileState>& profiles()
      const {
    return child_collector_.profiles_;
  }

  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  mojo::PendingRemote<mojom::CallStackProfileCollector> collector_remote_;
  std::unique_ptr<Receiver> receiver_impl_;
  ChildCallStackProfileCollector child_collector_;
};

// Test the behavior when an interface is provided.
TEST_F(ChildCallStackProfileCollectorTest, InterfaceProvided) {
  EXPECT_EQ(0u, profiles().size());

  // Add a profile before providing the interface.
  base::TimeTicks start_timestamp =
      CollectProfile(SampledProfile::PERIODIC_COLLECTION);
  ASSERT_EQ(1u, profiles().size());
  ExpectProfile(profiles()[0], start_timestamp, mojom::ProfileType::kCPU);

  // Set the interface. The profiles should be passed to it.
  child_collector_.SetParentProfileCollector(std::move(collector_remote_));
  task_environment_.FastForwardBy(base::Milliseconds(1));
  EXPECT_EQ(0u, profiles().size());
  ASSERT_EQ(1u, receiver_impl_->profiles().size());
  ExpectProfile(receiver_impl_->profiles()[0], start_timestamp,
                mojom::ProfileType::kCPU);

  // Add a profile after providing the interface. It should also be passed.
  receiver_impl_->profiles().clear();
  base::TimeTicks start_timestamp2 =
      CollectProfile(SampledProfile::PERIODIC_COLLECTION);
  task_environment_.FastForwardBy(base::Milliseconds(1));
  EXPECT_EQ(0u, profiles().size());
  ASSERT_EQ(1u, receiver_impl_->profiles().size());
  ExpectProfile(receiver_impl_->profiles()[0], start_timestamp2,
                mojom::ProfileType::kCPU);
}

TEST_F(ChildCallStackProfileCollectorTest, InterfaceNotProvided) {
  EXPECT_EQ(0u, profiles().size());

  // Add a profile before providing a null interface.
  base::TimeTicks start_timestamp =
      CollectProfile(SampledProfile::PERIODIC_COLLECTION);
  ASSERT_EQ(1u, profiles().size());
  ExpectProfile(profiles()[0], start_timestamp, mojom::ProfileType::kCPU);

  // Set the null interface. The profile should be flushed.
  child_collector_.SetParentProfileCollector(mojo::NullRemote());
  task_environment_.FastForwardBy(base::Milliseconds(1));
  EXPECT_EQ(0u, profiles().size());
  EXPECT_EQ(0u, receiver_impl_->profiles().size());

  // Add a profile after providing a null interface. They should also be
  // flushed.
  CollectProfile(SampledProfile::PERIODIC_COLLECTION);
  task_environment_.FastForwardBy(base::Milliseconds(1));
  EXPECT_EQ(0u, profiles().size());
  EXPECT_EQ(0u, receiver_impl_->profiles().size());
}

// Tests that the `profile_type` parameter is set correctly when heap profiles
// pass through the interface.
TEST_F(ChildCallStackProfileCollectorTest, HeapProfiles) {
  EXPECT_EQ(0u, profiles().size());

  // Add a profile before providing the interface.
  base::TimeTicks start_timestamp =
      CollectProfile(SampledProfile::PERIODIC_HEAP_COLLECTION);
  ASSERT_EQ(1u, profiles().size());
  ExpectProfile(profiles()[0], start_timestamp, mojom::ProfileType::kHeap);

  // Set the interface. The profile type should pass to it unchanged.
  child_collector_.SetParentProfileCollector(std::move(collector_remote_));
  task_environment_.FastForwardBy(base::Milliseconds(1));
  ASSERT_EQ(1u, receiver_impl_->profiles().size());
  ExpectProfile(receiver_impl_->profiles()[0], start_timestamp,
                mojom::ProfileType::kHeap);
}

}  // namespace metrics