| OLD | NEW |
| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 class TestMetricsService : public MetricsService { | 47 class TestMetricsService : public MetricsService { |
| 48 public: | 48 public: |
| 49 TestMetricsService(MetricsStateManager* state_manager, | 49 TestMetricsService(MetricsStateManager* state_manager, |
| 50 MetricsServiceClient* client, | 50 MetricsServiceClient* client, |
| 51 PrefService* local_state) | 51 PrefService* local_state) |
| 52 : MetricsService(state_manager, client, local_state) {} | 52 : MetricsService(state_manager, client, local_state) {} |
| 53 ~TestMetricsService() override {} | 53 ~TestMetricsService() override {} |
| 54 | 54 |
| 55 using MetricsService::log_manager; | 55 using MetricsService::log_manager; |
| 56 using MetricsService::log_store; |
| 56 | 57 |
| 57 private: | 58 private: |
| 58 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); | 59 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); |
| 59 }; | 60 }; |
| 60 | 61 |
| 61 class TestMetricsLog : public MetricsLog { | 62 class TestMetricsLog : public MetricsLog { |
| 62 public: | 63 public: |
| 63 TestMetricsLog(const std::string& client_id, | 64 TestMetricsLog(const std::string& client_id, |
| 64 int session_id, | 65 int session_id, |
| 65 MetricsServiceClient* client, | 66 MetricsServiceClient* client, |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 TestMetricsService service( | 182 TestMetricsService service( |
| 182 GetMetricsStateManager(), &client, GetLocalState()); | 183 GetMetricsStateManager(), &client, GetLocalState()); |
| 183 | 184 |
| 184 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 185 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 185 service.RegisterMetricsProvider( | 186 service.RegisterMetricsProvider( |
| 186 std::unique_ptr<MetricsProvider>(test_provider)); | 187 std::unique_ptr<MetricsProvider>(test_provider)); |
| 187 | 188 |
| 188 service.InitializeMetricsRecordingState(); | 189 service.InitializeMetricsRecordingState(); |
| 189 | 190 |
| 190 // No initial stability log should be generated. | 191 // No initial stability log should be generated. |
| 191 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); | 192 EXPECT_FALSE(service.log_store()->has_unsent_logs()); |
| 192 EXPECT_FALSE(service.log_manager()->has_staged_log()); | 193 EXPECT_FALSE(service.log_store()->has_staged_log()); |
| 193 | 194 |
| 194 // Ensure that HasInitialStabilityMetrics() is always called on providers, | 195 // Ensure that HasInitialStabilityMetrics() is always called on providers, |
| 195 // for consistency, even if other conditions already indicate their presence. | 196 // for consistency, even if other conditions already indicate their presence. |
| 196 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); | 197 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); |
| 197 | 198 |
| 198 // The test provider should not have been called upon to provide initial | 199 // The test provider should not have been called upon to provide initial |
| 199 // stability nor regular stability metrics. | 200 // stability nor regular stability metrics. |
| 200 EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called()); | 201 EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called()); |
| 201 EXPECT_FALSE(test_provider->provide_stability_metrics_called()); | 202 EXPECT_FALSE(test_provider->provide_stability_metrics_called()); |
| 202 } | 203 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 225 GetMetricsStateManager(), &client, GetLocalState()); | 226 GetMetricsStateManager(), &client, GetLocalState()); |
| 226 // Add a metrics provider that requests a stability log. | 227 // Add a metrics provider that requests a stability log. |
| 227 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 228 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 228 test_provider->set_has_initial_stability_metrics(true); | 229 test_provider->set_has_initial_stability_metrics(true); |
| 229 service.RegisterMetricsProvider( | 230 service.RegisterMetricsProvider( |
| 230 std::unique_ptr<MetricsProvider>(test_provider)); | 231 std::unique_ptr<MetricsProvider>(test_provider)); |
| 231 | 232 |
| 232 service.InitializeMetricsRecordingState(); | 233 service.InitializeMetricsRecordingState(); |
| 233 | 234 |
| 234 // The initial stability log should be generated and persisted in unsent logs. | 235 // The initial stability log should be generated and persisted in unsent logs. |
| 235 MetricsLogManager* log_manager = service.log_manager(); | 236 MetricsLogStore* log_store = service.log_store(); |
| 236 EXPECT_TRUE(log_manager->has_unsent_logs()); | 237 EXPECT_TRUE(log_store->has_unsent_logs()); |
| 237 EXPECT_FALSE(log_manager->has_staged_log()); | 238 EXPECT_FALSE(log_store->has_staged_log()); |
| 238 | 239 |
| 239 // Ensure that HasInitialStabilityMetrics() is always called on providers, | 240 // Ensure that HasInitialStabilityMetrics() is always called on providers, |
| 240 // for consistency, even if other conditions already indicate their presence. | 241 // for consistency, even if other conditions already indicate their presence. |
| 241 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); | 242 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); |
| 242 | 243 |
| 243 // The test provider should have been called upon to provide initial | 244 // The test provider should have been called upon to provide initial |
| 244 // stability and regular stability metrics. | 245 // stability and regular stability metrics. |
| 245 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); | 246 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); |
| 246 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); | 247 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); |
| 247 | 248 |
| 248 // Stage the log and retrieve it. | 249 // Stage the log and retrieve it. |
| 249 log_manager->StageNextLogForUpload(); | 250 log_store->StageNextLog(); |
| 250 EXPECT_TRUE(log_manager->has_staged_log()); | 251 EXPECT_TRUE(log_store->has_staged_log()); |
| 251 | 252 |
| 252 std::string uncompressed_log; | 253 std::string uncompressed_log; |
| 253 EXPECT_TRUE(compression::GzipUncompress(log_manager->staged_log(), | 254 EXPECT_TRUE( |
| 254 &uncompressed_log)); | 255 compression::GzipUncompress(log_store->staged_log(), &uncompressed_log)); |
| 255 | 256 |
| 256 ChromeUserMetricsExtension uma_log; | 257 ChromeUserMetricsExtension uma_log; |
| 257 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); | 258 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); |
| 258 | 259 |
| 259 EXPECT_TRUE(uma_log.has_client_id()); | 260 EXPECT_TRUE(uma_log.has_client_id()); |
| 260 EXPECT_TRUE(uma_log.has_session_id()); | 261 EXPECT_TRUE(uma_log.has_session_id()); |
| 261 EXPECT_TRUE(uma_log.has_system_profile()); | 262 EXPECT_TRUE(uma_log.has_system_profile()); |
| 262 EXPECT_EQ(0, uma_log.user_action_event_size()); | 263 EXPECT_EQ(0, uma_log.user_action_event_size()); |
| 263 EXPECT_EQ(0, uma_log.omnibox_event_size()); | 264 EXPECT_EQ(0, uma_log.omnibox_event_size()); |
| 264 EXPECT_EQ(0, uma_log.profiler_event_size()); | 265 EXPECT_EQ(0, uma_log.profiler_event_size()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 292 | 293 |
| 293 TestMetricsService service( | 294 TestMetricsService service( |
| 294 GetMetricsStateManager(), &client, GetLocalState()); | 295 GetMetricsStateManager(), &client, GetLocalState()); |
| 295 // Add a provider. | 296 // Add a provider. |
| 296 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 297 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 297 service.RegisterMetricsProvider( | 298 service.RegisterMetricsProvider( |
| 298 std::unique_ptr<MetricsProvider>(test_provider)); | 299 std::unique_ptr<MetricsProvider>(test_provider)); |
| 299 service.InitializeMetricsRecordingState(); | 300 service.InitializeMetricsRecordingState(); |
| 300 | 301 |
| 301 // The initial stability log should be generated and persisted in unsent logs. | 302 // The initial stability log should be generated and persisted in unsent logs. |
| 302 MetricsLogManager* log_manager = service.log_manager(); | 303 MetricsLogStore* log_store = service.log_store(); |
| 303 EXPECT_TRUE(log_manager->has_unsent_logs()); | 304 EXPECT_TRUE(log_store->has_unsent_logs()); |
| 304 EXPECT_FALSE(log_manager->has_staged_log()); | 305 EXPECT_FALSE(log_store->has_staged_log()); |
| 305 | 306 |
| 306 // Ensure that HasInitialStabilityMetrics() is always called on providers, | 307 // Ensure that HasInitialStabilityMetrics() is always called on providers, |
| 307 // for consistency, even if other conditions already indicate their presence. | 308 // for consistency, even if other conditions already indicate their presence. |
| 308 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); | 309 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); |
| 309 | 310 |
| 310 // The test provider should have been called upon to provide initial | 311 // The test provider should have been called upon to provide initial |
| 311 // stability and regular stability metrics. | 312 // stability and regular stability metrics. |
| 312 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); | 313 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); |
| 313 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); | 314 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); |
| 314 | 315 |
| 315 // Stage the log and retrieve it. | 316 // Stage the log and retrieve it. |
| 316 log_manager->StageNextLogForUpload(); | 317 log_store->StageNextLog(); |
| 317 EXPECT_TRUE(log_manager->has_staged_log()); | 318 EXPECT_TRUE(log_store->has_staged_log()); |
| 318 | 319 |
| 319 std::string uncompressed_log; | 320 std::string uncompressed_log; |
| 320 EXPECT_TRUE(compression::GzipUncompress(log_manager->staged_log(), | 321 EXPECT_TRUE( |
| 321 &uncompressed_log)); | 322 compression::GzipUncompress(log_store->staged_log(), &uncompressed_log)); |
| 322 | 323 |
| 323 ChromeUserMetricsExtension uma_log; | 324 ChromeUserMetricsExtension uma_log; |
| 324 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); | 325 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); |
| 325 | 326 |
| 326 EXPECT_TRUE(uma_log.has_client_id()); | 327 EXPECT_TRUE(uma_log.has_client_id()); |
| 327 EXPECT_TRUE(uma_log.has_session_id()); | 328 EXPECT_TRUE(uma_log.has_session_id()); |
| 328 EXPECT_TRUE(uma_log.has_system_profile()); | 329 EXPECT_TRUE(uma_log.has_system_profile()); |
| 329 EXPECT_EQ(0, uma_log.user_action_event_size()); | 330 EXPECT_EQ(0, uma_log.user_action_event_size()); |
| 330 EXPECT_EQ(0, uma_log.omnibox_event_size()); | 331 EXPECT_EQ(0, uma_log.omnibox_event_size()); |
| 331 EXPECT_EQ(0, uma_log.profiler_event_size()); | 332 EXPECT_EQ(0, uma_log.profiler_event_size()); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 HashName("Group3")); | 382 HashName("Group3")); |
| 382 service.RegisterSyntheticFieldTrial(trial4); | 383 service.RegisterSyntheticFieldTrial(trial4); |
| 383 service.GetSyntheticFieldTrialsOlderThan(begin_log_time, &synthetic_trials); | 384 service.GetSyntheticFieldTrialsOlderThan(begin_log_time, &synthetic_trials); |
| 384 EXPECT_EQ(1U, synthetic_trials.size()); | 385 EXPECT_EQ(1U, synthetic_trials.size()); |
| 385 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 386 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
| 386 | 387 |
| 387 // Ensure that time has advanced by at least a tick before proceeding. | 388 // Ensure that time has advanced by at least a tick before proceeding. |
| 388 WaitUntilTimeChanges(base::TimeTicks::Now()); | 389 WaitUntilTimeChanges(base::TimeTicks::Now()); |
| 389 | 390 |
| 390 // Start a new log and ensure all three trials appear in it. | 391 // Start a new log and ensure all three trials appear in it. |
| 391 service.log_manager_.FinishCurrentLog(); | 392 service.log_manager_.FinishCurrentLog(service.log_store()); |
| 392 service.log_manager_.BeginLoggingWithLog( | 393 service.log_manager_.BeginLoggingWithLog( |
| 393 std::unique_ptr<MetricsLog>(new MetricsLog( | 394 std::unique_ptr<MetricsLog>(new MetricsLog( |
| 394 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState()))); | 395 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState()))); |
| 395 service.GetSyntheticFieldTrialsOlderThan( | 396 service.GetSyntheticFieldTrialsOlderThan( |
| 396 service.log_manager_.current_log()->creation_time(), &synthetic_trials); | 397 service.log_manager_.current_log()->creation_time(), &synthetic_trials); |
| 397 EXPECT_EQ(3U, synthetic_trials.size()); | 398 EXPECT_EQ(3U, synthetic_trials.size()); |
| 398 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); | 399 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); |
| 399 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 400 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
| 400 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); | 401 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); |
| 401 service.log_manager_.FinishCurrentLog(); | 402 service.log_manager_.FinishCurrentLog(service.log_store()); |
| 402 } | 403 } |
| 403 | 404 |
| 404 TEST_F(MetricsServiceTest, RegisterSyntheticMultiGroupFieldTrial) { | 405 TEST_F(MetricsServiceTest, RegisterSyntheticMultiGroupFieldTrial) { |
| 405 TestMetricsServiceClient client; | 406 TestMetricsServiceClient client; |
| 406 MetricsService service(GetMetricsStateManager(), &client, GetLocalState()); | 407 MetricsService service(GetMetricsStateManager(), &client, GetLocalState()); |
| 407 | 408 |
| 408 // Register a synthetic trial TestTrial1 with groups A and B. | 409 // Register a synthetic trial TestTrial1 with groups A and B. |
| 409 uint32_t trial_name_hash = HashName("TestTrial1"); | 410 uint32_t trial_name_hash = HashName("TestTrial1"); |
| 410 std::vector<uint32_t> group_name_hashes = {HashName("A"), HashName("B")}; | 411 std::vector<uint32_t> group_name_hashes = {HashName("A"), HashName("B")}; |
| 411 service.RegisterSyntheticMultiGroupFieldTrial(trial_name_hash, | 412 service.RegisterSyntheticMultiGroupFieldTrial(trial_name_hash, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 438 | 439 |
| 439 // Register a trial with no groups, which should effectively remove the trial. | 440 // Register a trial with no groups, which should effectively remove the trial. |
| 440 group_name_hashes.clear(); | 441 group_name_hashes.clear(); |
| 441 service.RegisterSyntheticMultiGroupFieldTrial(trial_name_hash, | 442 service.RegisterSyntheticMultiGroupFieldTrial(trial_name_hash, |
| 442 group_name_hashes); | 443 group_name_hashes); |
| 443 // Ensure that time has advanced by at least a tick before proceeding. | 444 // Ensure that time has advanced by at least a tick before proceeding. |
| 444 WaitUntilTimeChanges(base::TimeTicks::Now()); | 445 WaitUntilTimeChanges(base::TimeTicks::Now()); |
| 445 | 446 |
| 446 service.GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(), | 447 service.GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(), |
| 447 &synthetic_trials); | 448 &synthetic_trials); |
| 448 service.log_manager_.FinishCurrentLog(); | 449 service.log_manager_.FinishCurrentLog(service.log_store()); |
| 449 } | 450 } |
| 450 | 451 |
| 451 TEST_F(MetricsServiceTest, | 452 TEST_F(MetricsServiceTest, |
| 452 MetricsProviderOnRecordingDisabledCalledOnInitialStop) { | 453 MetricsProviderOnRecordingDisabledCalledOnInitialStop) { |
| 453 TestMetricsServiceClient client; | 454 TestMetricsServiceClient client; |
| 454 TestMetricsService service( | 455 TestMetricsService service( |
| 455 GetMetricsStateManager(), &client, GetLocalState()); | 456 GetMetricsStateManager(), &client, GetLocalState()); |
| 456 | 457 |
| 457 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 458 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 458 service.RegisterMetricsProvider( | 459 service.RegisterMetricsProvider( |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 client.uploader()->CompleteUpload(200); | 548 client.uploader()->CompleteUpload(200); |
| 548 EXPECT_FALSE(client.uploader()->is_uploading()); | 549 EXPECT_FALSE(client.uploader()->is_uploading()); |
| 549 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); | 550 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 550 // Upload should start. | 551 // Upload should start. |
| 551 task_runner_->RunPendingTasks(); | 552 task_runner_->RunPendingTasks(); |
| 552 EXPECT_TRUE(client.uploader()->is_uploading()); | 553 EXPECT_TRUE(client.uploader()->is_uploading()); |
| 553 EXPECT_EQ(0U, task_runner_->NumPendingTasks()); | 554 EXPECT_EQ(0U, task_runner_->NumPendingTasks()); |
| 554 } | 555 } |
| 555 | 556 |
| 556 } // namespace metrics | 557 } // namespace metrics |
| OLD | NEW |