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

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

Issue 636363004: Make remaining parts of components/metrics use metrics namespace. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/metrics/metrics_service.h" 5 #include "components/metrics/metrics_service.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 13 matching lines...) Expand all
24 24
25 namespace { 25 namespace {
26 26
27 void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) { 27 void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) {
28 } 28 }
29 29
30 scoped_ptr<ClientInfo> ReturnNoBackup() { 30 scoped_ptr<ClientInfo> ReturnNoBackup() {
31 return scoped_ptr<ClientInfo>(); 31 return scoped_ptr<ClientInfo>();
32 } 32 }
33 33
34 class TestMetricsProvider : public metrics::MetricsProvider { 34 class TestMetricsProvider : public MetricsProvider {
35 public: 35 public:
36 explicit TestMetricsProvider(bool has_stability_metrics) : 36 explicit TestMetricsProvider(bool has_stability_metrics) :
37 has_stability_metrics_(has_stability_metrics), 37 has_stability_metrics_(has_stability_metrics),
38 provide_stability_metrics_called_(false) { 38 provide_stability_metrics_called_(false) {
39 } 39 }
40 40
41 bool HasStabilityMetrics() override { return has_stability_metrics_; } 41 bool HasStabilityMetrics() override { return has_stability_metrics_; }
42 void ProvideStabilityMetrics( 42 void ProvideStabilityMetrics(
43 SystemProfileProto* system_profile_proto) override { 43 SystemProfileProto* system_profile_proto) override {
44 provide_stability_metrics_called_ = true; 44 provide_stability_metrics_called_ = true;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 157
158 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { 158 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) {
159 EnableMetricsReporting(); 159 EnableMetricsReporting();
160 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); 160 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true);
161 161
162 TestMetricsServiceClient client; 162 TestMetricsServiceClient client;
163 TestMetricsService service( 163 TestMetricsService service(
164 GetMetricsStateManager(), &client, GetLocalState()); 164 GetMetricsStateManager(), &client, GetLocalState());
165 165
166 TestMetricsProvider* test_provider = new TestMetricsProvider(false); 166 TestMetricsProvider* test_provider = new TestMetricsProvider(false);
167 service.RegisterMetricsProvider( 167 service.RegisterMetricsProvider(scoped_ptr<MetricsProvider>(test_provider));
168 scoped_ptr<metrics::MetricsProvider>(test_provider));
169 168
170 service.InitializeMetricsRecordingState(); 169 service.InitializeMetricsRecordingState();
171 // No initial stability log should be generated. 170 // No initial stability log should be generated.
172 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); 171 EXPECT_FALSE(service.log_manager()->has_unsent_logs());
173 EXPECT_FALSE(service.log_manager()->has_staged_log()); 172 EXPECT_FALSE(service.log_manager()->has_staged_log());
174 173
175 // The test provider should not have been called upon to provide stability 174 // The test provider should not have been called upon to provide stability
176 // metrics. 175 // metrics.
177 EXPECT_FALSE(test_provider->provide_stability_metrics_called()); 176 EXPECT_FALSE(test_provider->provide_stability_metrics_called());
178 } 177 }
179 178
180 TEST_F(MetricsServiceTest, InitialStabilityLogAtProviderRequest) { 179 TEST_F(MetricsServiceTest, InitialStabilityLogAtProviderRequest) {
181 EnableMetricsReporting(); 180 EnableMetricsReporting();
182 181
183 // Save an existing system profile to prefs, to correspond to what would be 182 // Save an existing system profile to prefs, to correspond to what would be
184 // saved from a previous session. 183 // saved from a previous session.
185 TestMetricsServiceClient client; 184 TestMetricsServiceClient client;
186 TestMetricsLog log("client", 1, &client, GetLocalState()); 185 TestMetricsLog log("client", 1, &client, GetLocalState());
187 log.RecordEnvironment(std::vector<metrics::MetricsProvider*>(), 186 log.RecordEnvironment(std::vector<MetricsProvider*>(),
188 std::vector<variations::ActiveGroupId>(), 187 std::vector<variations::ActiveGroupId>(),
189 0); 188 0);
190 189
191 // Record stability build time and version from previous session, so that 190 // Record stability build time and version from previous session, so that
192 // stability metrics (including exited cleanly flag) won't be cleared. 191 // stability metrics (including exited cleanly flag) won't be cleared.
193 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, 192 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime,
194 MetricsLog::GetBuildTime()); 193 MetricsLog::GetBuildTime());
195 GetLocalState()->SetString(prefs::kStabilityStatsVersion, 194 GetLocalState()->SetString(prefs::kStabilityStatsVersion,
196 client.GetVersionString()); 195 client.GetVersionString());
197 196
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 // The initial stability log should be generated and persisted in unsent logs. 270 // The initial stability log should be generated and persisted in unsent logs.
272 MetricsLogManager* log_manager = service.log_manager(); 271 MetricsLogManager* log_manager = service.log_manager();
273 EXPECT_TRUE(log_manager->has_unsent_logs()); 272 EXPECT_TRUE(log_manager->has_unsent_logs());
274 EXPECT_FALSE(log_manager->has_staged_log()); 273 EXPECT_FALSE(log_manager->has_staged_log());
275 274
276 // Stage the log and retrieve it. 275 // Stage the log and retrieve it.
277 log_manager->StageNextLogForUpload(); 276 log_manager->StageNextLogForUpload();
278 EXPECT_TRUE(log_manager->has_staged_log()); 277 EXPECT_TRUE(log_manager->has_staged_log());
279 278
280 std::string uncompressed_log; 279 std::string uncompressed_log;
281 EXPECT_TRUE(metrics::GzipUncompress(log_manager->staged_log(), 280 EXPECT_TRUE(GzipUncompress(log_manager->staged_log(), &uncompressed_log));
282 &uncompressed_log));
283 281
284 metrics::ChromeUserMetricsExtension uma_log; 282 ChromeUserMetricsExtension uma_log;
285 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); 283 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log));
286 284
287 EXPECT_TRUE(uma_log.has_client_id()); 285 EXPECT_TRUE(uma_log.has_client_id());
288 EXPECT_TRUE(uma_log.has_session_id()); 286 EXPECT_TRUE(uma_log.has_session_id());
289 EXPECT_TRUE(uma_log.has_system_profile()); 287 EXPECT_TRUE(uma_log.has_system_profile());
290 EXPECT_EQ(0, uma_log.user_action_event_size()); 288 EXPECT_EQ(0, uma_log.user_action_event_size());
291 EXPECT_EQ(0, uma_log.omnibox_event_size()); 289 EXPECT_EQ(0, uma_log.omnibox_event_size());
292 EXPECT_EQ(0, uma_log.histogram_event_size()); 290 EXPECT_EQ(0, uma_log.histogram_event_size());
293 EXPECT_EQ(0, uma_log.profiler_event_size()); 291 EXPECT_EQ(0, uma_log.profiler_event_size());
294 EXPECT_EQ(0, uma_log.perf_data_size()); 292 EXPECT_EQ(0, uma_log.perf_data_size());
295 293
296 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count()); 294 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count());
297 } 295 }
298 296
299 TEST_F(MetricsServiceTest, RegisterSyntheticTrial) { 297 TEST_F(MetricsServiceTest, RegisterSyntheticTrial) {
300 metrics::TestMetricsServiceClient client; 298 TestMetricsServiceClient client;
301 MetricsService service(GetMetricsStateManager(), &client, GetLocalState()); 299 MetricsService service(GetMetricsStateManager(), &client, GetLocalState());
302 300
303 // Add two synthetic trials and confirm that they show up in the list. 301 // Add two synthetic trials and confirm that they show up in the list.
304 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), 302 SyntheticTrialGroup trial1(HashName("TestTrial1"), HashName("Group1"));
305 metrics::HashName("Group1"));
306 service.RegisterSyntheticFieldTrial(trial1); 303 service.RegisterSyntheticFieldTrial(trial1);
307 304
308 SyntheticTrialGroup trial2(metrics::HashName("TestTrial2"), 305 SyntheticTrialGroup trial2(HashName("TestTrial2"), HashName("Group2"));
309 metrics::HashName("Group2"));
310 service.RegisterSyntheticFieldTrial(trial2); 306 service.RegisterSyntheticFieldTrial(trial2);
311 // Ensure that time has advanced by at least a tick before proceeding. 307 // Ensure that time has advanced by at least a tick before proceeding.
312 WaitUntilTimeChanges(base::TimeTicks::Now()); 308 WaitUntilTimeChanges(base::TimeTicks::Now());
313 309
314 service.log_manager_.BeginLoggingWithLog(scoped_ptr<MetricsLog>( 310 service.log_manager_.BeginLoggingWithLog(scoped_ptr<MetricsLog>(
315 new MetricsLog("clientID", 311 new MetricsLog("clientID",
316 1, 312 1,
317 MetricsLog::INITIAL_STABILITY_LOG, 313 MetricsLog::INITIAL_STABILITY_LOG,
318 &client, 314 &client,
319 GetLocalState()))); 315 GetLocalState())));
320 // Save the time when the log was started (it's okay for this to be greater 316 // Save the time when the log was started (it's okay for this to be greater
321 // than the time recorded by the above call since it's used to ensure the 317 // than the time recorded by the above call since it's used to ensure the
322 // value changes). 318 // value changes).
323 const base::TimeTicks begin_log_time = base::TimeTicks::Now(); 319 const base::TimeTicks begin_log_time = base::TimeTicks::Now();
324 320
325 std::vector<variations::ActiveGroupId> synthetic_trials; 321 std::vector<variations::ActiveGroupId> synthetic_trials;
326 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 322 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
327 EXPECT_EQ(2U, synthetic_trials.size()); 323 EXPECT_EQ(2U, synthetic_trials.size());
328 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group1")); 324 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group1"));
329 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 325 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
330 326
331 // Ensure that time has advanced by at least a tick before proceeding. 327 // Ensure that time has advanced by at least a tick before proceeding.
332 WaitUntilTimeChanges(begin_log_time); 328 WaitUntilTimeChanges(begin_log_time);
333 329
334 // Change the group for the first trial after the log started. 330 // Change the group for the first trial after the log started.
335 SyntheticTrialGroup trial3(metrics::HashName("TestTrial1"), 331 SyntheticTrialGroup trial3(HashName("TestTrial1"), HashName("Group2"));
336 metrics::HashName("Group2"));
337 service.RegisterSyntheticFieldTrial(trial3); 332 service.RegisterSyntheticFieldTrial(trial3);
338 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 333 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
339 EXPECT_EQ(1U, synthetic_trials.size()); 334 EXPECT_EQ(1U, synthetic_trials.size());
340 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 335 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
341 336
342 // Add a new trial after the log started and confirm that it doesn't show up. 337 // Add a new trial after the log started and confirm that it doesn't show up.
343 SyntheticTrialGroup trial4(metrics::HashName("TestTrial3"), 338 SyntheticTrialGroup trial4(HashName("TestTrial3"), HashName("Group3"));
344 metrics::HashName("Group3"));
345 service.RegisterSyntheticFieldTrial(trial4); 339 service.RegisterSyntheticFieldTrial(trial4);
346 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 340 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
347 EXPECT_EQ(1U, synthetic_trials.size()); 341 EXPECT_EQ(1U, synthetic_trials.size());
348 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 342 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
349 343
350 // Ensure that time has advanced by at least a tick before proceeding. 344 // Ensure that time has advanced by at least a tick before proceeding.
351 WaitUntilTimeChanges(base::TimeTicks::Now()); 345 WaitUntilTimeChanges(base::TimeTicks::Now());
352 346
353 // Start a new log and ensure all three trials appear in it. 347 // Start a new log and ensure all three trials appear in it.
354 service.log_manager_.FinishCurrentLog(); 348 service.log_manager_.FinishCurrentLog();
355 service.log_manager_.BeginLoggingWithLog( 349 service.log_manager_.BeginLoggingWithLog(
356 scoped_ptr<MetricsLog>(new MetricsLog( 350 scoped_ptr<MetricsLog>(new MetricsLog(
357 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState()))); 351 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState())));
358 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 352 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
359 EXPECT_EQ(3U, synthetic_trials.size()); 353 EXPECT_EQ(3U, synthetic_trials.size());
360 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); 354 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2"));
361 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 355 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
362 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); 356 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3"));
363 service.log_manager_.FinishCurrentLog(); 357 service.log_manager_.FinishCurrentLog();
364 } 358 }
365 359
366 } // namespace metrics 360 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698