File: child_call_stack_profile_collector_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (177 lines) | stat: -rw-r--r-- 7,069 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
171
172
173
174
175
176
177
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "components/metrics/call_stack_profile_params.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace metrics {

namespace {


}  // namespace

class ChildCallStackProfileCollectorTest : public testing::Test {
 protected:
  class Receiver : public mojom::CallStackProfileCollector {
   public:
    using CallStackProfile = base::StackSamplingProfiler::CallStackProfile;

    Receiver(mojom::CallStackProfileCollectorRequest request)
        : binding_(this, std::move(request)) {}
    ~Receiver() override {}

    void Collect(const CallStackProfileParams& params,
                 base::TimeTicks start_timestamp,
                 std::vector<CallStackProfile> profiles) override {
      this->profiles.push_back(ChildCallStackProfileCollector::ProfilesState(
          params,
          start_timestamp,
          std::move(profiles)));
    }

    std::vector<ChildCallStackProfileCollector::ProfilesState> profiles;

   private:
    mojo::Binding<mojom::CallStackProfileCollector> binding_;

    DISALLOW_COPY_AND_ASSIGN(Receiver);
  };

  ChildCallStackProfileCollectorTest()
      : receiver_impl_(new Receiver(MakeRequest(&receiver_))) {}

  void CollectEmptyProfiles(
      const CallStackProfileParams& params,
      size_t profile_count) {
    base::StackSamplingProfiler::CallStackProfiles profiles;
    for (size_t i = 0; i < profile_count; ++i)
      profiles.push_back(base::StackSamplingProfiler::CallStackProfile());
    child_collector_.GetProfilerCallback(params).Run(std::move(profiles));
  }

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

  base::MessageLoop loop_;
  mojom::CallStackProfileCollectorPtr receiver_;
  std::unique_ptr<Receiver> receiver_impl_;
  ChildCallStackProfileCollector child_collector_;

  DISALLOW_COPY_AND_ASSIGN(ChildCallStackProfileCollectorTest);
};

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

  // Add profiles before providing the interface.
  CollectEmptyProfiles(
      CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                             CallStackProfileParams::UI_THREAD,
                             CallStackProfileParams::JANKY_TASK,
                             CallStackProfileParams::PRESERVE_ORDER),
      2);
  ASSERT_EQ(1u, profiles().size());
  EXPECT_EQ(CallStackProfileParams::BROWSER_PROCESS,
            profiles()[0].params.process);
  EXPECT_EQ(CallStackProfileParams::UI_THREAD, profiles()[0].params.thread);
  EXPECT_EQ(CallStackProfileParams::JANKY_TASK, profiles()[0].params.trigger);
  EXPECT_EQ(CallStackProfileParams::PRESERVE_ORDER,
            profiles()[0].params.ordering_spec);
  base::TimeTicks start_timestamp = profiles()[0].start_timestamp;
  EXPECT_GE(base::TimeDelta::FromMilliseconds(10),
            base::TimeTicks::Now() - start_timestamp);
  EXPECT_EQ(2u, profiles()[0].profiles.size());

  // Set the interface. The profiles should be passed to it.
  child_collector_.SetParentProfileCollector(std::move(receiver_));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(0u, profiles().size());
  ASSERT_EQ(1u, receiver_impl_->profiles.size());
  EXPECT_EQ(CallStackProfileParams::JANKY_TASK,
            receiver_impl_->profiles[0].params.trigger);
  EXPECT_EQ(CallStackProfileParams::PRESERVE_ORDER,
            receiver_impl_->profiles[0].params.ordering_spec);
  EXPECT_EQ(start_timestamp, receiver_impl_->profiles[0].start_timestamp);
  EXPECT_EQ(2u, receiver_impl_->profiles[0].profiles.size());

  // Add profiles after providing the interface. They should also be passed to
  // it.
  receiver_impl_->profiles.clear();
  CollectEmptyProfiles(
      CallStackProfileParams(CallStackProfileParams::GPU_PROCESS,
                             CallStackProfileParams::GPU_MAIN_THREAD,
                             CallStackProfileParams::THREAD_HUNG,
                             CallStackProfileParams::PRESERVE_ORDER),
      1);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(0u, profiles().size());
  ASSERT_EQ(1u, receiver_impl_->profiles.size());
  EXPECT_EQ(CallStackProfileParams::GPU_PROCESS,
            receiver_impl_->profiles[0].params.process);
  EXPECT_EQ(CallStackProfileParams::GPU_MAIN_THREAD,
            receiver_impl_->profiles[0].params.thread);
  EXPECT_EQ(CallStackProfileParams::THREAD_HUNG,
            receiver_impl_->profiles[0].params.trigger);
  EXPECT_EQ(CallStackProfileParams::PRESERVE_ORDER,
            receiver_impl_->profiles[0].params.ordering_spec);
  EXPECT_GE(base::TimeDelta::FromMilliseconds(10),
            (base::TimeTicks::Now() -
             receiver_impl_->profiles[0].start_timestamp));
  EXPECT_EQ(1u, receiver_impl_->profiles[0].profiles.size());
}

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

  // Add profiles before providing a null interface.
  CollectEmptyProfiles(
      CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                             CallStackProfileParams::UI_THREAD,
                             CallStackProfileParams::JANKY_TASK,
                             CallStackProfileParams::PRESERVE_ORDER),
      2);
  ASSERT_EQ(1u, profiles().size());
  EXPECT_EQ(CallStackProfileParams::BROWSER_PROCESS,
            profiles()[0].params.process);
  EXPECT_EQ(CallStackProfileParams::UI_THREAD, profiles()[0].params.thread);
  EXPECT_EQ(CallStackProfileParams::JANKY_TASK, profiles()[0].params.trigger);
  EXPECT_EQ(CallStackProfileParams::PRESERVE_ORDER,
            profiles()[0].params.ordering_spec);
  EXPECT_GE(base::TimeDelta::FromMilliseconds(10),
            base::TimeTicks::Now() - profiles()[0].start_timestamp);
  EXPECT_EQ(2u, profiles()[0].profiles.size());

  // Set the null interface. The profiles should be flushed.
  child_collector_.SetParentProfileCollector(
      mojom::CallStackProfileCollectorPtr());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(0u, profiles().size());

  // Add profiles after providing a null interface. They should also be flushed.
  CollectEmptyProfiles(
      CallStackProfileParams(CallStackProfileParams::GPU_PROCESS,
                             CallStackProfileParams::GPU_MAIN_THREAD,
                             CallStackProfileParams::THREAD_HUNG,
                             CallStackProfileParams::PRESERVE_ORDER),
      1);
  EXPECT_EQ(0u, profiles().size());
}

}  // namespace metrics