1 // Copyright 2014 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.
5 #include "components/metrics/metrics_service.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/testing_pref_service.h"
13 #include "base/threading/platform_thread.h"
14 #include "components/metrics/client_info.h"
15 #include "components/metrics/compression_utils.h"
16 #include "components/metrics/metrics_log.h"
17 #include "components/metrics/metrics_pref_names.h"
18 #include "components/metrics/metrics_state_manager.h"
19 #include "components/metrics/test_metrics_service_client.h"
20 #include "components/variations/metrics_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
27 void StoreNoClientInfoBackup(const ClientInfo
& /* client_info */) {
30 scoped_ptr
<ClientInfo
> ReturnNoBackup() {
31 return scoped_ptr
<ClientInfo
>();
34 class TestMetricsProvider
: public MetricsProvider
{
36 explicit TestMetricsProvider(bool has_stability_metrics
) :
37 has_stability_metrics_(has_stability_metrics
),
38 provide_stability_metrics_called_(false) {
41 bool HasStabilityMetrics() override
{ return has_stability_metrics_
; }
42 void ProvideStabilityMetrics(
43 SystemProfileProto
* system_profile_proto
) override
{
44 provide_stability_metrics_called_
= true;
47 bool provide_stability_metrics_called() const {
48 return provide_stability_metrics_called_
;
52 bool has_stability_metrics_
;
53 bool provide_stability_metrics_called_
;
55 DISALLOW_COPY_AND_ASSIGN(TestMetricsProvider
);
58 class TestMetricsService
: public MetricsService
{
60 TestMetricsService(MetricsStateManager
* state_manager
,
61 MetricsServiceClient
* client
,
62 PrefService
* local_state
)
63 : MetricsService(state_manager
, client
, local_state
) {}
64 ~TestMetricsService() override
{}
66 using MetricsService::log_manager
;
69 DISALLOW_COPY_AND_ASSIGN(TestMetricsService
);
72 class TestMetricsLog
: public MetricsLog
{
74 TestMetricsLog(const std::string
& client_id
,
76 MetricsServiceClient
* client
,
77 PrefService
* local_state
)
78 : MetricsLog(client_id
,
80 MetricsLog::ONGOING_LOG
,
84 ~TestMetricsLog() override
{}
87 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog
);
90 class MetricsServiceTest
: public testing::Test
{
92 MetricsServiceTest() : is_metrics_reporting_enabled_(false) {
93 MetricsService::RegisterPrefs(testing_local_state_
.registry());
94 metrics_state_manager_
= MetricsStateManager::Create(
96 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled
,
97 base::Unretained(this)),
98 base::Bind(&StoreNoClientInfoBackup
),
99 base::Bind(&ReturnNoBackup
));
102 ~MetricsServiceTest() override
{
103 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE
,
107 MetricsStateManager
* GetMetricsStateManager() {
108 return metrics_state_manager_
.get();
111 PrefService
* GetLocalState() { return &testing_local_state_
; }
113 // Sets metrics reporting as enabled for testing.
114 void EnableMetricsReporting() {
115 is_metrics_reporting_enabled_
= true;
118 // Waits until base::TimeTicks::Now() no longer equals |value|. This should
119 // take between 1-15ms per the documented resolution of base::TimeTicks.
120 void WaitUntilTimeChanges(const base::TimeTicks
& value
) {
121 while (base::TimeTicks::Now() == value
) {
122 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
126 // Returns true if there is a synthetic trial in the given vector that matches
127 // the given trial name and trial group; returns false otherwise.
128 bool HasSyntheticTrial(
129 const std::vector
<variations::ActiveGroupId
>& synthetic_trials
,
130 const std::string
& trial_name
,
131 const std::string
& trial_group
) {
132 uint32 trial_name_hash
= HashName(trial_name
);
133 uint32 trial_group_hash
= HashName(trial_group
);
134 for (std::vector
<variations::ActiveGroupId
>::const_iterator it
=
135 synthetic_trials
.begin();
136 it
!= synthetic_trials
.end(); ++it
) {
137 if ((*it
).name
== trial_name_hash
&& (*it
).group
== trial_group_hash
)
144 bool is_metrics_reporting_enabled() const {
145 return is_metrics_reporting_enabled_
;
148 bool is_metrics_reporting_enabled_
;
149 TestingPrefServiceSimple testing_local_state_
;
150 scoped_ptr
<MetricsStateManager
> metrics_state_manager_
;
151 base::MessageLoop message_loop
;
153 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest
);
158 TEST_F(MetricsServiceTest
, InitialStabilityLogAfterCleanShutDown
) {
159 EnableMetricsReporting();
160 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly
, true);
162 TestMetricsServiceClient client
;
163 TestMetricsService
service(
164 GetMetricsStateManager(), &client
, GetLocalState());
166 TestMetricsProvider
* test_provider
= new TestMetricsProvider(false);
167 service
.RegisterMetricsProvider(scoped_ptr
<MetricsProvider
>(test_provider
));
169 service
.InitializeMetricsRecordingState();
170 // No initial stability log should be generated.
171 EXPECT_FALSE(service
.log_manager()->has_unsent_logs());
172 EXPECT_FALSE(service
.log_manager()->has_staged_log());
174 // The test provider should not have been called upon to provide stability
176 EXPECT_FALSE(test_provider
->provide_stability_metrics_called());
179 TEST_F(MetricsServiceTest
, InitialStabilityLogAtProviderRequest
) {
180 EnableMetricsReporting();
182 // Save an existing system profile to prefs, to correspond to what would be
183 // saved from a previous session.
184 TestMetricsServiceClient client
;
185 TestMetricsLog
log("client", 1, &client
, GetLocalState());
186 log
.RecordEnvironment(std::vector
<MetricsProvider
*>(),
187 std::vector
<variations::ActiveGroupId
>(),
190 // Record stability build time and version from previous session, so that
191 // stability metrics (including exited cleanly flag) won't be cleared.
192 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime
,
193 MetricsLog::GetBuildTime());
194 GetLocalState()->SetString(prefs::kStabilityStatsVersion
,
195 client
.GetVersionString());
197 // Set the clean exit flag, as that will otherwise cause a stabilty
198 // log to be produced, irrespective provider requests.
199 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly
, true);
201 TestMetricsService
service(
202 GetMetricsStateManager(), &client
, GetLocalState());
203 // Add a metrics provider that requests a stability log.
204 TestMetricsProvider
* test_provider
= new TestMetricsProvider(true);
205 service
.RegisterMetricsProvider(
206 scoped_ptr
<MetricsProvider
>(test_provider
));
208 service
.InitializeMetricsRecordingState();
210 // The initial stability log should be generated and persisted in unsent logs.
211 MetricsLogManager
* log_manager
= service
.log_manager();
212 EXPECT_TRUE(log_manager
->has_unsent_logs());
213 EXPECT_FALSE(log_manager
->has_staged_log());
215 // The test provider should have been called upon to provide stability
217 EXPECT_TRUE(test_provider
->provide_stability_metrics_called());
219 // Stage the log and retrieve it.
220 log_manager
->StageNextLogForUpload();
221 EXPECT_TRUE(log_manager
->has_staged_log());
223 std::string uncompressed_log
;
224 EXPECT_TRUE(GzipUncompress(log_manager
->staged_log(),
227 ChromeUserMetricsExtension uma_log
;
228 EXPECT_TRUE(uma_log
.ParseFromString(uncompressed_log
));
230 EXPECT_TRUE(uma_log
.has_client_id());
231 EXPECT_TRUE(uma_log
.has_session_id());
232 EXPECT_TRUE(uma_log
.has_system_profile());
233 EXPECT_EQ(0, uma_log
.user_action_event_size());
234 EXPECT_EQ(0, uma_log
.omnibox_event_size());
235 EXPECT_EQ(0, uma_log
.histogram_event_size());
236 EXPECT_EQ(0, uma_log
.profiler_event_size());
237 EXPECT_EQ(0, uma_log
.perf_data_size());
239 // As there wasn't an unclean shutdown, this log has zero crash count.
240 EXPECT_EQ(0, uma_log
.system_profile().stability().crash_count());
243 TEST_F(MetricsServiceTest
, InitialStabilityLogAfterCrash
) {
244 EnableMetricsReporting();
245 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly
);
247 // Set up prefs to simulate restarting after a crash.
249 // Save an existing system profile to prefs, to correspond to what would be
250 // saved from a previous session.
251 TestMetricsServiceClient client
;
252 TestMetricsLog
log("client", 1, &client
, GetLocalState());
253 log
.RecordEnvironment(std::vector
<MetricsProvider
*>(),
254 std::vector
<variations::ActiveGroupId
>(),
257 // Record stability build time and version from previous session, so that
258 // stability metrics (including exited cleanly flag) won't be cleared.
259 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime
,
260 MetricsLog::GetBuildTime());
261 GetLocalState()->SetString(prefs::kStabilityStatsVersion
,
262 client
.GetVersionString());
264 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly
, false);
266 TestMetricsService
service(
267 GetMetricsStateManager(), &client
, GetLocalState());
268 service
.InitializeMetricsRecordingState();
270 // The initial stability log should be generated and persisted in unsent logs.
271 MetricsLogManager
* log_manager
= service
.log_manager();
272 EXPECT_TRUE(log_manager
->has_unsent_logs());
273 EXPECT_FALSE(log_manager
->has_staged_log());
275 // Stage the log and retrieve it.
276 log_manager
->StageNextLogForUpload();
277 EXPECT_TRUE(log_manager
->has_staged_log());
279 std::string uncompressed_log
;
280 EXPECT_TRUE(GzipUncompress(log_manager
->staged_log(), &uncompressed_log
));
282 ChromeUserMetricsExtension uma_log
;
283 EXPECT_TRUE(uma_log
.ParseFromString(uncompressed_log
));
285 EXPECT_TRUE(uma_log
.has_client_id());
286 EXPECT_TRUE(uma_log
.has_session_id());
287 EXPECT_TRUE(uma_log
.has_system_profile());
288 EXPECT_EQ(0, uma_log
.user_action_event_size());
289 EXPECT_EQ(0, uma_log
.omnibox_event_size());
290 EXPECT_EQ(0, uma_log
.histogram_event_size());
291 EXPECT_EQ(0, uma_log
.profiler_event_size());
292 EXPECT_EQ(0, uma_log
.perf_data_size());
294 EXPECT_EQ(1, uma_log
.system_profile().stability().crash_count());
297 TEST_F(MetricsServiceTest
, RegisterSyntheticTrial
) {
298 TestMetricsServiceClient client
;
299 MetricsService
service(GetMetricsStateManager(), &client
, GetLocalState());
301 // Add two synthetic trials and confirm that they show up in the list.
302 SyntheticTrialGroup
trial1(HashName("TestTrial1"), HashName("Group1"));
303 service
.RegisterSyntheticFieldTrial(trial1
);
305 SyntheticTrialGroup
trial2(HashName("TestTrial2"), HashName("Group2"));
306 service
.RegisterSyntheticFieldTrial(trial2
);
307 // Ensure that time has advanced by at least a tick before proceeding.
308 WaitUntilTimeChanges(base::TimeTicks::Now());
310 service
.log_manager_
.BeginLoggingWithLog(scoped_ptr
<MetricsLog
>(
311 new MetricsLog("clientID",
313 MetricsLog::INITIAL_STABILITY_LOG
,
316 // Save the time when the log was started (it's okay for this to be greater
317 // than the time recorded by the above call since it's used to ensure the
319 const base::TimeTicks begin_log_time
= base::TimeTicks::Now();
321 std::vector
<variations::ActiveGroupId
> synthetic_trials
;
322 service
.GetCurrentSyntheticFieldTrials(&synthetic_trials
);
323 EXPECT_EQ(2U, synthetic_trials
.size());
324 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial1", "Group1"));
325 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial2", "Group2"));
327 // Ensure that time has advanced by at least a tick before proceeding.
328 WaitUntilTimeChanges(begin_log_time
);
330 // Change the group for the first trial after the log started.
331 SyntheticTrialGroup
trial3(HashName("TestTrial1"), HashName("Group2"));
332 service
.RegisterSyntheticFieldTrial(trial3
);
333 service
.GetCurrentSyntheticFieldTrials(&synthetic_trials
);
334 EXPECT_EQ(1U, synthetic_trials
.size());
335 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial2", "Group2"));
337 // Add a new trial after the log started and confirm that it doesn't show up.
338 SyntheticTrialGroup
trial4(HashName("TestTrial3"), HashName("Group3"));
339 service
.RegisterSyntheticFieldTrial(trial4
);
340 service
.GetCurrentSyntheticFieldTrials(&synthetic_trials
);
341 EXPECT_EQ(1U, synthetic_trials
.size());
342 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial2", "Group2"));
344 // Ensure that time has advanced by at least a tick before proceeding.
345 WaitUntilTimeChanges(base::TimeTicks::Now());
347 // Start a new log and ensure all three trials appear in it.
348 service
.log_manager_
.FinishCurrentLog();
349 service
.log_manager_
.BeginLoggingWithLog(
350 scoped_ptr
<MetricsLog
>(new MetricsLog(
351 "clientID", 1, MetricsLog::ONGOING_LOG
, &client
, GetLocalState())));
352 service
.GetCurrentSyntheticFieldTrials(&synthetic_trials
);
353 EXPECT_EQ(3U, synthetic_trials
.size());
354 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial1", "Group2"));
355 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial2", "Group2"));
356 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial3", "Group3"));
357 service
.log_manager_
.FinishCurrentLog();
360 } // namespace metrics