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/message_loop/message_loop.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/threading/platform_thread.h"
13 #include "components/metrics/compression_utils.h"
14 #include "components/metrics/metrics_log.h"
15 #include "components/metrics/metrics_pref_names.h"
16 #include "components/metrics/metrics_service_observer.h"
17 #include "components/metrics/metrics_state_manager.h"
18 #include "components/metrics/test_metrics_service_client.h"
19 #include "components/variations/metrics_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 using metrics::MetricsLogManager
;
26 class TestMetricsService
: public MetricsService
{
28 TestMetricsService(metrics::MetricsStateManager
* state_manager
,
29 metrics::MetricsServiceClient
* client
,
30 PrefService
* local_state
)
31 : MetricsService(state_manager
, client
, local_state
) {}
32 virtual ~TestMetricsService() {}
34 using MetricsService::log_manager
;
37 DISALLOW_COPY_AND_ASSIGN(TestMetricsService
);
40 class TestMetricsLog
: public MetricsLog
{
42 TestMetricsLog(const std::string
& client_id
,
44 metrics::MetricsServiceClient
* client
,
45 PrefService
* local_state
)
46 : MetricsLog(client_id
,
48 MetricsLog::ONGOING_LOG
,
52 virtual ~TestMetricsLog() {}
55 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog
);
58 class MetricsServiceTest
: public testing::Test
{
60 MetricsServiceTest() : is_metrics_reporting_enabled_(false) {
61 MetricsService::RegisterPrefs(testing_local_state_
.registry());
62 metrics_state_manager_
= metrics::MetricsStateManager::Create(
64 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled
,
65 base::Unretained(this)));
68 virtual ~MetricsServiceTest() {
69 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE
,
73 metrics::MetricsStateManager
* GetMetricsStateManager() {
74 return metrics_state_manager_
.get();
77 PrefService
* GetLocalState() { return &testing_local_state_
; }
79 // Sets metrics reporting as enabled for testing.
80 void EnableMetricsReporting() {
81 is_metrics_reporting_enabled_
= true;
84 // Waits until base::TimeTicks::Now() no longer equals |value|. This should
85 // take between 1-15ms per the documented resolution of base::TimeTicks.
86 void WaitUntilTimeChanges(const base::TimeTicks
& value
) {
87 while (base::TimeTicks::Now() == value
) {
88 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
92 // Returns true if there is a synthetic trial in the given vector that matches
93 // the given trial name and trial group; returns false otherwise.
94 bool HasSyntheticTrial(
95 const std::vector
<variations::ActiveGroupId
>& synthetic_trials
,
96 const std::string
& trial_name
,
97 const std::string
& trial_group
) {
98 uint32 trial_name_hash
= metrics::HashName(trial_name
);
99 uint32 trial_group_hash
= metrics::HashName(trial_group
);
100 for (std::vector
<variations::ActiveGroupId
>::const_iterator it
=
101 synthetic_trials
.begin();
102 it
!= synthetic_trials
.end(); ++it
) {
103 if ((*it
).name
== trial_name_hash
&& (*it
).group
== trial_group_hash
)
110 bool is_metrics_reporting_enabled() const {
111 return is_metrics_reporting_enabled_
;
114 bool is_metrics_reporting_enabled_
;
115 TestingPrefServiceSimple testing_local_state_
;
116 scoped_ptr
<metrics::MetricsStateManager
> metrics_state_manager_
;
117 base::MessageLoop message_loop
;
119 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest
);
122 class TestMetricsServiceObserver
: public MetricsServiceObserver
{
124 TestMetricsServiceObserver(): observed_(0) {}
125 virtual ~TestMetricsServiceObserver() {}
127 virtual void OnDidCreateMetricsLog() OVERRIDE
{
130 int observed() const { return observed_
; }
135 DISALLOW_COPY_AND_ASSIGN(TestMetricsServiceObserver
);
140 TEST_F(MetricsServiceTest
, InitialStabilityLogAfterCleanShutDown
) {
141 EnableMetricsReporting();
142 GetLocalState()->SetBoolean(metrics::prefs::kStabilityExitedCleanly
, true);
144 metrics::TestMetricsServiceClient client
;
145 TestMetricsService
service(
146 GetMetricsStateManager(), &client
, GetLocalState());
147 service
.InitializeMetricsRecordingState();
148 // No initial stability log should be generated.
149 EXPECT_FALSE(service
.log_manager()->has_unsent_logs());
150 EXPECT_FALSE(service
.log_manager()->has_staged_log());
153 TEST_F(MetricsServiceTest
, InitialStabilityLogAfterCrash
) {
154 EnableMetricsReporting();
155 GetLocalState()->ClearPref(metrics::prefs::kStabilityExitedCleanly
);
157 // Set up prefs to simulate restarting after a crash.
159 // Save an existing system profile to prefs, to correspond to what would be
160 // saved from a previous session.
161 metrics::TestMetricsServiceClient client
;
162 TestMetricsLog
log("client", 1, &client
, GetLocalState());
163 log
.RecordEnvironment(std::vector
<metrics::MetricsProvider
*>(),
164 std::vector
<variations::ActiveGroupId
>());
166 // Record stability build time and version from previous session, so that
167 // stability metrics (including exited cleanly flag) won't be cleared.
168 GetLocalState()->SetInt64(metrics::prefs::kStabilityStatsBuildTime
,
169 MetricsLog::GetBuildTime());
170 GetLocalState()->SetString(metrics::prefs::kStabilityStatsVersion
,
171 client
.GetVersionString());
173 GetLocalState()->SetBoolean(metrics::prefs::kStabilityExitedCleanly
, false);
175 TestMetricsService
service(
176 GetMetricsStateManager(), &client
, GetLocalState());
177 service
.InitializeMetricsRecordingState();
179 // The initial stability log should be generated and persisted in unsent logs.
180 MetricsLogManager
* log_manager
= service
.log_manager();
181 EXPECT_TRUE(log_manager
->has_unsent_logs());
182 EXPECT_FALSE(log_manager
->has_staged_log());
184 // Stage the log and retrieve it.
185 log_manager
->StageNextLogForUpload();
186 EXPECT_TRUE(log_manager
->has_staged_log());
188 std::string uncompressed_log
;
189 EXPECT_TRUE(metrics::GzipUncompress(log_manager
->staged_log(),
192 metrics::ChromeUserMetricsExtension uma_log
;
193 EXPECT_TRUE(uma_log
.ParseFromString(uncompressed_log
));
195 EXPECT_TRUE(uma_log
.has_client_id());
196 EXPECT_TRUE(uma_log
.has_session_id());
197 EXPECT_TRUE(uma_log
.has_system_profile());
198 EXPECT_EQ(0, uma_log
.user_action_event_size());
199 EXPECT_EQ(0, uma_log
.omnibox_event_size());
200 EXPECT_EQ(0, uma_log
.histogram_event_size());
201 EXPECT_EQ(0, uma_log
.profiler_event_size());
202 EXPECT_EQ(0, uma_log
.perf_data_size());
204 EXPECT_EQ(1, uma_log
.system_profile().stability().crash_count());
207 TEST_F(MetricsServiceTest
, RegisterSyntheticTrial
) {
208 metrics::TestMetricsServiceClient client
;
209 MetricsService
service(GetMetricsStateManager(), &client
, GetLocalState());
211 // Add two synthetic trials and confirm that they show up in the list.
212 SyntheticTrialGroup
trial1(metrics::HashName("TestTrial1"),
213 metrics::HashName("Group1"));
214 service
.RegisterSyntheticFieldTrial(trial1
);
216 SyntheticTrialGroup
trial2(metrics::HashName("TestTrial2"),
217 metrics::HashName("Group2"));
218 service
.RegisterSyntheticFieldTrial(trial2
);
219 // Ensure that time has advanced by at least a tick before proceeding.
220 WaitUntilTimeChanges(base::TimeTicks::Now());
222 service
.log_manager_
.BeginLoggingWithLog(scoped_ptr
<MetricsLog
>(
223 new MetricsLog("clientID",
225 MetricsLog::INITIAL_STABILITY_LOG
,
228 // Save the time when the log was started (it's okay for this to be greater
229 // than the time recorded by the above call since it's used to ensure the
231 const base::TimeTicks begin_log_time
= base::TimeTicks::Now();
233 std::vector
<variations::ActiveGroupId
> synthetic_trials
;
234 service
.GetCurrentSyntheticFieldTrials(&synthetic_trials
);
235 EXPECT_EQ(2U, synthetic_trials
.size());
236 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial1", "Group1"));
237 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial2", "Group2"));
239 // Ensure that time has advanced by at least a tick before proceeding.
240 WaitUntilTimeChanges(begin_log_time
);
242 // Change the group for the first trial after the log started.
243 SyntheticTrialGroup
trial3(metrics::HashName("TestTrial1"),
244 metrics::HashName("Group2"));
245 service
.RegisterSyntheticFieldTrial(trial3
);
246 service
.GetCurrentSyntheticFieldTrials(&synthetic_trials
);
247 EXPECT_EQ(1U, synthetic_trials
.size());
248 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial2", "Group2"));
250 // Add a new trial after the log started and confirm that it doesn't show up.
251 SyntheticTrialGroup
trial4(metrics::HashName("TestTrial3"),
252 metrics::HashName("Group3"));
253 service
.RegisterSyntheticFieldTrial(trial4
);
254 service
.GetCurrentSyntheticFieldTrials(&synthetic_trials
);
255 EXPECT_EQ(1U, synthetic_trials
.size());
256 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial2", "Group2"));
258 // Ensure that time has advanced by at least a tick before proceeding.
259 WaitUntilTimeChanges(base::TimeTicks::Now());
261 // Start a new log and ensure all three trials appear in it.
262 service
.log_manager_
.FinishCurrentLog();
263 service
.log_manager_
.BeginLoggingWithLog(
264 scoped_ptr
<MetricsLog
>(new MetricsLog(
265 "clientID", 1, MetricsLog::ONGOING_LOG
, &client
, GetLocalState())));
266 service
.GetCurrentSyntheticFieldTrials(&synthetic_trials
);
267 EXPECT_EQ(3U, synthetic_trials
.size());
268 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial1", "Group2"));
269 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial2", "Group2"));
270 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials
, "TestTrial3", "Group3"));
271 service
.log_manager_
.FinishCurrentLog();
274 TEST_F(MetricsServiceTest
, MetricsServiceObserver
) {
275 metrics::TestMetricsServiceClient client
;
276 MetricsService
service(GetMetricsStateManager(), &client
, GetLocalState());
277 TestMetricsServiceObserver observer1
;
278 TestMetricsServiceObserver observer2
;
280 service
.AddObserver(&observer1
);
281 EXPECT_EQ(0, observer1
.observed());
282 EXPECT_EQ(0, observer2
.observed());
284 service
.OpenNewLog();
285 EXPECT_EQ(1, observer1
.observed());
286 EXPECT_EQ(0, observer2
.observed());
287 service
.log_manager_
.FinishCurrentLog();
289 service
.AddObserver(&observer2
);
291 service
.OpenNewLog();
292 EXPECT_EQ(2, observer1
.observed());
293 EXPECT_EQ(1, observer2
.observed());
294 service
.log_manager_
.FinishCurrentLog();
296 service
.RemoveObserver(&observer1
);
298 service
.OpenNewLog();
299 EXPECT_EQ(2, observer1
.observed());
300 EXPECT_EQ(2, observer2
.observed());
301 service
.log_manager_
.FinishCurrentLog();
303 service
.RemoveObserver(&observer2
);