OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/metrics/perf/perf_provider_chromeos.h" | 5 #include "chrome/browser/metrics/perf/perf_provider_chromeos.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/field_trial.h" |
12 #include "base/test/test_simple_task_runner.h" | 13 #include "base/test/test_simple_task_runner.h" |
13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
14 #include "chrome/browser/metrics/perf/windowed_incognito_observer.h" | 15 #include "chrome/browser/metrics/perf/windowed_incognito_observer.h" |
15 #include "chromeos/dbus/dbus_thread_manager.h" | 16 #include "chromeos/dbus/dbus_thread_manager.h" |
16 #include "chromeos/login/login_state.h" | 17 #include "chromeos/login/login_state.h" |
17 #include "components/metrics/proto/sampled_profile.pb.h" | 18 #include "components/metrics/proto/sampled_profile.pb.h" |
| 19 #include "components/variations/variations_associated_data.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 namespace metrics { | 22 namespace metrics { |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 // Return values for perf. | 26 // Return values for perf. |
25 const int kPerfSuccess = 0; | 27 const int kPerfSuccess = 0; |
26 const int kPerfFailure = 1; | 28 const int kPerfFailure = 1; |
27 | 29 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 observer->set_incognito_launched(incognito_launched); | 115 observer->set_incognito_launched(incognito_launched); |
114 return observer.Pass(); | 116 return observer.Pass(); |
115 } | 117 } |
116 | 118 |
117 private: | 119 private: |
118 TestIncognitoObserver() {} | 120 TestIncognitoObserver() {} |
119 | 121 |
120 DISALLOW_COPY_AND_ASSIGN(TestIncognitoObserver); | 122 DISALLOW_COPY_AND_ASSIGN(TestIncognitoObserver); |
121 }; | 123 }; |
122 | 124 |
123 // Allows access to PerfProvider::ParseOutputProtoIfValid() for testing. | 125 // Allows access to some private methods for testing. |
124 class TestPerfProvider : public PerfProvider { | 126 class TestPerfProvider : public PerfProvider { |
125 public: | 127 public: |
126 TestPerfProvider() {} | 128 TestPerfProvider() {} |
127 | 129 |
128 using PerfProvider::ParseOutputProtoIfValid; | 130 using PerfProvider::ParseOutputProtoIfValid; |
| 131 using PerfProvider::collection_params; |
| 132 using PerfProvider::command_selector; |
129 | 133 |
130 private: | 134 private: |
131 std::vector<SampledProfile> stored_profiles_; | 135 std::vector<SampledProfile> stored_profiles_; |
132 | 136 |
133 DISALLOW_COPY_AND_ASSIGN(TestPerfProvider); | 137 DISALLOW_COPY_AND_ASSIGN(TestPerfProvider); |
134 }; | 138 }; |
135 | 139 |
136 } // namespace | 140 } // namespace |
137 | 141 |
138 class PerfProviderTest : public testing::Test { | 142 class PerfProviderTest : public testing::Test { |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 cpuid.vendor = ""; | 579 cpuid.vendor = ""; |
576 cpuid.family = 0; | 580 cpuid.family = 0; |
577 cpuid.model = 0; | 581 cpuid.model = 0; |
578 cpuid.model_name = ""; | 582 cpuid.model_name = ""; |
579 std::vector<RandomSelector::WeightAndValue> cmds = | 583 std::vector<RandomSelector::WeightAndValue> cmds = |
580 internal::GetDefaultCommandsForCpu(cpuid); | 584 internal::GetDefaultCommandsForCpu(cpuid); |
581 EXPECT_EQ(1UL, cmds.size()); | 585 EXPECT_EQ(1UL, cmds.size()); |
582 EXPECT_EQ(cmds[0].value, kPerfRecordCyclesCmd); | 586 EXPECT_EQ(cmds[0].value, kPerfRecordCyclesCmd); |
583 } | 587 } |
584 | 588 |
| 589 TEST_F(PerfProviderTest, CommandMatching_Empty) { |
| 590 CPUIdentity cpuid = {}; |
| 591 std::map<std::string, std::string> params; |
| 592 EXPECT_EQ("", internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 593 } |
| 594 |
| 595 TEST_F(PerfProviderTest, CommandMatching_NoPerfCommands) { |
| 596 CPUIdentity cpuid = {}; |
| 597 std::map<std::string, std::string> params; |
| 598 using param_t = decltype(params)::value_type; |
| 599 params.insert(param_t("NotEvenClose", "")); |
| 600 params.insert(param_t("NotAPerfCommand", "")); |
| 601 params.insert(param_t("NotAPerfCommand::Really", "")); |
| 602 params.insert(param_t("NotAPerfCommand::Nope::0", "")); |
| 603 params.insert(param_t("PerfCommands::SoClose::0", "")); |
| 604 EXPECT_EQ("", internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 605 } |
| 606 |
| 607 TEST_F(PerfProviderTest, CommandMatching_NoMatch) { |
| 608 CPUIdentity cpuid; |
| 609 cpuid.arch = "x86_64"; |
| 610 cpuid.vendor = "GenuineIntel"; |
| 611 cpuid.family = 6; |
| 612 cpuid.model = 0x3a; // IvyBridge |
| 613 cpuid.model_name = "Xeon or somesuch"; |
| 614 std::map<std::string, std::string> params; |
| 615 using param_t = decltype(params)::value_type; |
| 616 params.insert(param_t("PerfCommand::armv7l::0", "perf command")); |
| 617 params.insert(param_t("PerfCommand::x86::0", "perf command")); |
| 618 params.insert(param_t("PerfCommand::x86::1", "perf command")); |
| 619 params.insert(param_t("PerfCommand::Broadwell::0", "perf command")); |
| 620 |
| 621 EXPECT_EQ("", internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 622 } |
| 623 |
| 624 TEST_F(PerfProviderTest, CommandMatching_default) { |
| 625 CPUIdentity cpuid; |
| 626 cpuid.arch = "x86_64"; |
| 627 cpuid.vendor = "GenuineIntel"; |
| 628 cpuid.family = 6; |
| 629 cpuid.model = 0x3a; // IvyBridge |
| 630 cpuid.model_name = "Xeon or somesuch"; |
| 631 std::map<std::string, std::string> params; |
| 632 using param_t = decltype(params)::value_type; |
| 633 params.insert(param_t("PerfCommand::default::0", "perf command")); |
| 634 params.insert(param_t("PerfCommand::armv7l::0", "perf command")); |
| 635 params.insert(param_t("PerfCommand::x86::0", "perf command")); |
| 636 params.insert(param_t("PerfCommand::x86::1", "perf command")); |
| 637 params.insert(param_t("PerfCommand::Broadwell::0", "perf command")); |
| 638 |
| 639 EXPECT_EQ("default", internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 640 } |
| 641 |
| 642 TEST_F(PerfProviderTest, CommandMatching_SystemArch) { |
| 643 CPUIdentity cpuid; |
| 644 cpuid.arch = "nothing_in_particular"; |
| 645 cpuid.vendor = ""; |
| 646 cpuid.family = 0; |
| 647 cpuid.model = 0; |
| 648 cpuid.model_name = ""; |
| 649 std::map<std::string, std::string> params; |
| 650 using param_t = decltype(params)::value_type; |
| 651 params.insert(param_t("PerfCommand::default::0", "perf command")); |
| 652 params.insert(param_t("PerfCommand::armv7l::0", "perf command")); |
| 653 params.insert(param_t("PerfCommand::x86::0", "perf command")); |
| 654 params.insert(param_t("PerfCommand::x86::1", "perf command")); |
| 655 params.insert(param_t("PerfCommand::x86_64::0", "perf command")); |
| 656 params.insert(param_t("PerfCommand::x86_64::xyz#$%", "perf command")); |
| 657 params.insert(param_t("PerfCommand::Broadwell::0", "perf command")); |
| 658 |
| 659 EXPECT_EQ("default", internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 660 |
| 661 cpuid.arch = "armv7l"; |
| 662 EXPECT_EQ("armv7l", internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 663 |
| 664 cpuid.arch = "x86"; |
| 665 EXPECT_EQ("x86", internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 666 |
| 667 cpuid.arch = "x86_64"; |
| 668 EXPECT_EQ("x86_64", internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 669 } |
| 670 |
| 671 TEST_F(PerfProviderTest, CommandMatching_Microarchitecture) { |
| 672 CPUIdentity cpuid; |
| 673 cpuid.arch = "x86_64"; |
| 674 cpuid.vendor = "GenuineIntel"; |
| 675 cpuid.family = 6; |
| 676 cpuid.model = 0x3D; // Broadwell |
| 677 cpuid.model_name = "Wrong Model CPU @ 0 Hz"; |
| 678 std::map<std::string, std::string> params; |
| 679 using param_t = decltype(params)::value_type; |
| 680 params.insert(param_t("PerfCommand::default::0", "perf command")); |
| 681 params.insert(param_t("PerfCommand::x86_64::0", "perf command")); |
| 682 params.insert(param_t("PerfCommand::Broadwell::0", "perf command")); |
| 683 params.insert(param_t("PerfCommand::interesting-model-500x::0", |
| 684 "perf command")); |
| 685 |
| 686 EXPECT_EQ("Broadwell", |
| 687 internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 688 } |
| 689 |
| 690 TEST_F(PerfProviderTest, CommandMatching_SpecificModel) { |
| 691 CPUIdentity cpuid; |
| 692 cpuid.arch = "x86_64"; |
| 693 cpuid.vendor = "GenuineIntel"; |
| 694 cpuid.family = 6; |
| 695 cpuid.model = 0x3D; // Broadwell |
| 696 cpuid.model_name = "An Interesting(R) Model(R) 500x CPU @ 1.2GHz"; |
| 697 std::map<std::string, std::string> params; |
| 698 using param_t = decltype(params)::value_type; |
| 699 params.insert(param_t("PerfCommand::default::0", "perf command")); |
| 700 params.insert(param_t("PerfCommand::x86_64::0", "perf command")); |
| 701 params.insert(param_t("PerfCommand::Broadwell::0", "perf command")); |
| 702 params.insert(param_t("PerfCommand::interesting-model-500x::0", |
| 703 "perf command")); |
| 704 |
| 705 EXPECT_EQ("interesting-model-500x", |
| 706 internal::FindBestCpuSpecifierFromParams(params, cpuid)); |
| 707 } |
| 708 |
| 709 |
| 710 class PerfProviderCollectionParamsTest : public testing::Test { |
| 711 public: |
| 712 PerfProviderCollectionParamsTest() |
| 713 : task_runner_(new base::TestSimpleTaskRunner), |
| 714 task_runner_handle_(task_runner_), |
| 715 field_trial_list_(nullptr) {} |
| 716 |
| 717 void SetUp() override { |
| 718 // PerfProvider requires chromeos::LoginState and |
| 719 // chromeos::DBusThreadManagerto be initialized. |
| 720 chromeos::LoginState::Initialize(); |
| 721 chromeos::DBusThreadManager::Initialize(); |
| 722 |
| 723 // PerfProvider requires the user to be logged in. |
| 724 chromeos::LoginState::Get()->SetLoggedInState( |
| 725 chromeos::LoginState::LOGGED_IN_ACTIVE, |
| 726 chromeos::LoginState::LOGGED_IN_USER_REGULAR); |
| 727 } |
| 728 |
| 729 void TearDown() override { |
| 730 perf_provider_.reset(); |
| 731 chromeos::DBusThreadManager::Shutdown(); |
| 732 chromeos::LoginState::Shutdown(); |
| 733 variations::testing::ClearAllVariationParams(); |
| 734 } |
| 735 |
| 736 protected: |
| 737 scoped_ptr<TestPerfProvider> perf_provider_; |
| 738 |
| 739 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 740 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 741 base::FieldTrialList field_trial_list_; |
| 742 |
| 743 DISALLOW_COPY_AND_ASSIGN(PerfProviderCollectionParamsTest); |
| 744 }; |
| 745 |
| 746 TEST_F(PerfProviderCollectionParamsTest, Commands_EmptyExperiment) { |
| 747 std::vector<RandomSelector::WeightAndValue> default_cmds = |
| 748 internal::GetDefaultCommandsForCpu(GetCPUIdentity()); |
| 749 std::map<std::string, std::string> params; |
| 750 ASSERT_TRUE(variations::AssociateVariationParams( |
| 751 "ChromeOSWideProfilingCollection", "group_name", params)); |
| 752 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 753 "ChromeOSWideProfilingCollection", "group_name")); |
| 754 perf_provider_.reset(new TestPerfProvider); |
| 755 |
| 756 EXPECT_EQ(default_cmds, perf_provider_->command_selector().odds()); |
| 757 } |
| 758 |
| 759 TEST_F(PerfProviderCollectionParamsTest, Commands_InvalidValues) { |
| 760 std::vector<RandomSelector::WeightAndValue> default_cmds = |
| 761 internal::GetDefaultCommandsForCpu(GetCPUIdentity()); |
| 762 std::map<std::string, std::string> params; |
| 763 using param_t = decltype(params)::value_type; |
| 764 // Use the "default" cpu specifier since we don't want to predict what CPU |
| 765 // this test is running on. (CPU detection is tested above.) |
| 766 params.insert(param_t("PerfCommand::default::0", "")); |
| 767 params.insert(param_t("PerfCommand::default::1", " ")); |
| 768 params.insert(param_t("PerfCommand::default::2", " leading space")); |
| 769 params.insert(param_t("PerfCommand::default::3", "no-spaces-or-numbers")); |
| 770 params.insert(param_t("PerfCommand::default::4", "NaN-trailing-space ")); |
| 771 params.insert(param_t("PerfCommand::default::5", "NaN x")); |
| 772 params.insert(param_t("PerfCommand::default::6", "perf command")); |
| 773 ASSERT_TRUE(variations::AssociateVariationParams( |
| 774 "ChromeOSWideProfilingCollection", "group_name", params)); |
| 775 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 776 "ChromeOSWideProfilingCollection", "group_name")); |
| 777 perf_provider_.reset(new TestPerfProvider); |
| 778 |
| 779 EXPECT_EQ(default_cmds, perf_provider_->command_selector().odds()); |
| 780 } |
| 781 |
| 782 TEST_F(PerfProviderCollectionParamsTest, Commands_Override) { |
| 783 using WeightAndValue = RandomSelector::WeightAndValue; |
| 784 std::vector<RandomSelector::WeightAndValue> default_cmds = |
| 785 internal::GetDefaultCommandsForCpu(GetCPUIdentity()); |
| 786 std::map<std::string, std::string> params; |
| 787 using param_t = decltype(params)::value_type; |
| 788 // Use the "default" cpu specifier since we don't want to predict what CPU |
| 789 // this test is running on. (CPU detection is tested above.) |
| 790 params.insert(param_t("PerfCommand::default::0", "50 perf record foo")); |
| 791 params.insert(param_t("PerfCommand::default::1", "25 perf record bar")); |
| 792 params.insert(param_t("PerfCommand::default::2", "25 perf record baz")); |
| 793 params.insert(param_t("PerfCommand::another-cpu::0", "7 perf record bar")); |
| 794 ASSERT_TRUE(variations::AssociateVariationParams( |
| 795 "ChromeOSWideProfilingCollection", "group_name", params)); |
| 796 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 797 "ChromeOSWideProfilingCollection", "group_name")); |
| 798 perf_provider_.reset(new TestPerfProvider); |
| 799 |
| 800 std::vector<WeightAndValue> expected_cmds; |
| 801 expected_cmds.push_back(WeightAndValue(50.0, "perf record foo")); |
| 802 expected_cmds.push_back(WeightAndValue(25.0, "perf record bar")); |
| 803 expected_cmds.push_back(WeightAndValue(25.0, "perf record baz")); |
| 804 |
| 805 EXPECT_EQ(expected_cmds, perf_provider_->command_selector().odds()); |
| 806 } |
| 807 |
| 808 TEST_F(PerfProviderCollectionParamsTest, Parameters_Override) { |
| 809 std::map<std::string, std::string> params; |
| 810 using param_t = decltype(params)::value_type; |
| 811 params.insert(param_t("ProfileCollectionDurationSec", "15")); |
| 812 params.insert(param_t("PeriodicProfilingIntervalMs","3600000")); |
| 813 params.insert(param_t("ResumeFromSuspend::SamplingFactor","1")); |
| 814 params.insert(param_t("ResumeFromSuspend::MaxCollectionDelaySec","10")); |
| 815 params.insert(param_t("RestoreSession::SamplingFactor","2")); |
| 816 params.insert(param_t("RestoreSession::MaxCollectionDelaySec","20")); |
| 817 ASSERT_TRUE(variations::AssociateVariationParams( |
| 818 "ChromeOSWideProfilingCollection", "group_name", params)); |
| 819 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 820 "ChromeOSWideProfilingCollection", "group_name")); |
| 821 perf_provider_.reset(new TestPerfProvider); |
| 822 |
| 823 const auto& parsed_params = perf_provider_->collection_params(); |
| 824 EXPECT_EQ(base::TimeDelta::FromSeconds(15), |
| 825 parsed_params.collection_duration()); |
| 826 EXPECT_EQ(base::TimeDelta::FromHours(1), |
| 827 parsed_params.periodic_interval()); |
| 828 EXPECT_EQ(1, parsed_params.resume_from_suspend().sampling_factor()); |
| 829 EXPECT_EQ(base::TimeDelta::FromSeconds(10), |
| 830 parsed_params.resume_from_suspend().max_collection_delay()); |
| 831 EXPECT_EQ(2, parsed_params.restore_session().sampling_factor()); |
| 832 EXPECT_EQ(base::TimeDelta::FromSeconds(20), |
| 833 parsed_params.restore_session().max_collection_delay()); |
| 834 } |
| 835 |
585 } // namespace metrics | 836 } // namespace metrics |
OLD | NEW |