Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(392)

Side by Side Diff: components/metrics/single_sample_metrics_factory_impl_unittest.cc

Issue 2687583002: Add support for single sample metrics. (Closed)
Patch Set: Fix linker errors. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "components/metrics/single_sample_metrics_factory_impl.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/test/gtest_util.h"
10 #include "base/test/histogram_tester.h"
11 #include "base/threading/thread.h"
12 #include "components/metrics/single_sample_metrics.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace metrics {
16
17 namespace {
18
19 const base::HistogramBase::Sample kMin = 1;
20 const base::HistogramBase::Sample kMax = 10;
21 const uint32_t kBucketCount = 10;
22 const char kMetricName[] = "Single.Sample.Metric";
23
24 class SingleSampleMetricsFactoryImplTest : public testing::Test {
25 public:
26 SingleSampleMetricsFactoryImplTest() : thread_("TestThread") {
27 InitializeSingleSampleMetricsFactory(
28 base::BindRepeating(&SingleSampleMetricsFactoryImplTest::CreateProvider,
29 base::Unretained(this)));
30 factory_ = static_cast<SingleSampleMetricsFactoryImpl*>(
31 base::SingleSampleMetricsFactory::Get());
32 }
33
34 ~SingleSampleMetricsFactoryImplTest() override {
35 factory_->DestroyProviderForTesting();
36 if (thread_.IsRunning()) {
37 thread_.task_runner()->PostTask(
38 FROM_HERE,
39 base::Bind(&SingleSampleMetricsFactoryImpl::DestroyProviderForTesting,
40 base::Unretained(factory_)));
41 thread_.Stop();
42 }
43 base::SingleSampleMetricsFactory::DeleteFactoryForTesting();
44 }
45
46 protected:
47 void StartThread() { ASSERT_TRUE(thread_.Start()); }
48
49 void CreateProvider(mojom::SingleSampleMetricsProviderRequest request) {
50 CreateSingleSampleMetricsProvider(service_manager::BindSourceInfo(),
51 std::move(request));
52 provider_count_++;
53 }
54
55 std::unique_ptr<base::SingleSampleMetric> CreateMetricOnThread() {
56 std::unique_ptr<base::SingleSampleMetric> metric;
57 base::RunLoop run_loop;
58 thread_.task_runner()->PostTaskAndReply(
59 FROM_HERE,
60 base::Bind(&SingleSampleMetricsFactoryImplTest::CreateAndStoreMetric,
61 base::Unretained(this), &metric),
62 run_loop.QuitClosure());
63 run_loop.Run();
64 return metric;
65 }
66
67 void CreateAndStoreMetric(std::unique_ptr<base::SingleSampleMetric>* metric) {
68 *metric = factory_->CreateCustomCountsMetric(kMetricName, kMin, kMax,
69 kBucketCount);
70 }
71
72 base::MessageLoop message_looqp_;
73 SingleSampleMetricsFactoryImpl* factory_;
74 base::Thread thread_;
75 size_t provider_count_ = 0;
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(SingleSampleMetricsFactoryImplTest);
79 };
80
81 } // namespace
82
83 TEST_F(SingleSampleMetricsFactoryImplTest, SingleProvider) {
84 std::unique_ptr<base::SingleSampleMetric> metric1 =
85 factory_->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
86
87 std::unique_ptr<base::SingleSampleMetric> metric2 =
88 factory_->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
89
90 // Verify that only a single provider is created for multiple metrics.
91 base::RunLoop().RunUntilIdle();
92 EXPECT_EQ(1u, provider_count_);
93 }
94
95 TEST_F(SingleSampleMetricsFactoryImplTest, DoesNothing) {
96 base::HistogramTester tester;
97
98 std::unique_ptr<base::SingleSampleMetric> metric =
99 factory_->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
100 metric.reset();
101
102 // Verify that no sample is recorded if SetSample() is never called.
103 base::RunLoop().RunUntilIdle();
104 tester.ExpectTotalCount(kMetricName, 0);
105 }
106
107 TEST_F(SingleSampleMetricsFactoryImplTest, DefaultSingleSampleMetricWithValue) {
108 base::HistogramTester tester;
109 std::unique_ptr<base::SingleSampleMetric> metric =
110 factory_->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
111
112 const base::HistogramBase::Sample kLastSample = 9;
113 metric->SetSample(1);
114 metric->SetSample(3);
115 metric->SetSample(5);
116 metric->SetSample(kLastSample);
117 metric.reset();
118
119 // Verify only the last sample sent to SetSample() is recorded.
120 base::RunLoop().RunUntilIdle();
121 tester.ExpectUniqueSample(kMetricName, kLastSample, 1);
122
123 // Verify construction implicitly by requesting a histogram with the same
124 // parameters; this test relies on the fact that histogram objects are unique
125 // per name. Different parameters will result in a nullptr being returned.
126 EXPECT_FALSE(base::Histogram::FactoryGet(kMetricName, 1, 3, 3,
127 base::HistogramBase::kNoFlags));
128 EXPECT_TRUE(base::Histogram::FactoryGet(
129 kMetricName, kMin, kMax, kBucketCount,
130 base::HistogramBase::kUmaTargetedHistogramFlag));
131 }
132
133 TEST_F(SingleSampleMetricsFactoryImplTest, MultithreadedMetrics) {
134 base::HistogramTester tester;
135 std::unique_ptr<base::SingleSampleMetric> metric =
136 factory_->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount);
137 EXPECT_EQ(1u, provider_count_);
138
139 StartThread();
140
141 std::unique_ptr<base::SingleSampleMetric> threaded_metric =
142 CreateMetricOnThread();
143 ASSERT_TRUE(threaded_metric);
144
145 // A second provider should be created to handle requests on our new thread.
146 EXPECT_EQ(2u, provider_count_);
147
148 // Calls from the wrong thread should DCHECK.
149 EXPECT_DCHECK_DEATH(threaded_metric->SetSample(5));
150 EXPECT_DCHECK_DEATH(threaded_metric.reset());
151
152 // Test that samples are set on each thread correctly.
153 const base::HistogramBase::Sample kSample = 7;
154
155 {
156 metric->SetSample(kSample);
157
158 base::RunLoop run_loop;
159 thread_.task_runner()->PostTaskAndReply(
160 FROM_HERE,
161 base::Bind(&base::SingleSampleMetric::SetSample,
162 base::Unretained(threaded_metric.get()), kSample),
163 run_loop.QuitClosure());
164 run_loop.Run();
165 }
166
167 // Release metrics and cycle threads to ensure destruction completes.
168 {
169 thread_.task_runner()->DeleteSoon(FROM_HERE, threaded_metric.release());
170
171 base::RunLoop run_loop;
172 thread_.task_runner()->PostTaskAndReply(
173 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure());
174 run_loop.Run();
175 }
176
177 metric.reset();
178 base::RunLoop().RunUntilIdle();
179 tester.ExpectUniqueSample(kMetricName, kSample, 2);
180 }
181
182 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698