Chromium Code Reviews| 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 TEST_F(PerfProviderTest, CommandMatching_SpecificModel_LongestMatch) { | |
| 710 CPUIdentity cpuid; | |
| 711 cpuid.arch = "x86_64"; | |
| 712 cpuid.vendor = "GenuineIntel"; | |
| 713 cpuid.family = 6; | |
| 714 cpuid.model = 0x3D; // Broadwell | |
| 715 cpuid.model_name = "An Interesting(R) Model(R) 500x CPU @ 1.2GHz"; | |
| 716 std::map<std::string, std::string> params; | |
| 717 using param_t = decltype(params)::value_type; | |
| 718 params.insert(param_t("PerfCommand::default::0", "perf command")); | |
| 719 params.insert(param_t("PerfCommand::x86_64::0", "perf command")); | |
| 720 params.insert(param_t("PerfCommand::Broadwell::0", "perf command")); | |
| 721 params.insert(param_t("PerfCommand::model-500x::0", | |
| 722 "perf command")); | |
| 723 params.insert(param_t("PerfCommand::interesting-model-500x::0", | |
| 724 "perf command")); | |
| 725 params.insert(param_t("PerfCommand::interesting-model::0", "perf command")); | |
| 726 | |
| 727 EXPECT_EQ("interesting-model-500x", | |
| 728 internal::FindBestCpuSpecifierFromParams(params, cpuid)); | |
| 729 } | |
| 730 | |
| 731 class PerfProviderCollectionParamsTest : public testing::Test { | |
| 732 public: | |
| 733 PerfProviderCollectionParamsTest() | |
| 734 : task_runner_(new base::TestSimpleTaskRunner), | |
| 735 task_runner_handle_(task_runner_), | |
| 736 field_trial_list_(nullptr) {} | |
| 737 | |
| 738 void SetUp() override { | |
| 739 // PerfProvider requires chromeos::LoginState and | |
| 740 // chromeos::DBusThreadManagerto be initialized. | |
| 741 chromeos::LoginState::Initialize(); | |
| 742 chromeos::DBusThreadManager::Initialize(); | |
| 743 | |
| 744 // PerfProvider requires the user to be logged in. | |
| 745 chromeos::LoginState::Get()->SetLoggedInState( | |
| 746 chromeos::LoginState::LOGGED_IN_ACTIVE, | |
| 747 chromeos::LoginState::LOGGED_IN_USER_REGULAR); | |
| 748 } | |
| 749 | |
| 750 void TearDown() override { | |
| 751 perf_provider_.reset(); | |
| 752 chromeos::DBusThreadManager::Shutdown(); | |
| 753 chromeos::LoginState::Shutdown(); | |
| 754 variations::testing::ClearAllVariationParams(); | |
| 755 } | |
| 756 | |
| 757 protected: | |
| 758 scoped_ptr<TestPerfProvider> perf_provider_; | |
|
Alexei Svitkine (slow)
2015/10/21 16:52:56
I don't think this needs to be a member variable.
dhsharp
2015/10/21 18:19:04
Done.
| |
| 759 | |
| 760 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 761 base::ThreadTaskRunnerHandle task_runner_handle_; | |
| 762 base::FieldTrialList field_trial_list_; | |
| 763 | |
| 764 DISALLOW_COPY_AND_ASSIGN(PerfProviderCollectionParamsTest); | |
|
Alexei Svitkine (slow)
2015/10/21 16:52:56
Nit: This should be in private: section. Suggest p
dhsharp
2015/10/21 18:19:04
Done.
| |
| 765 }; | |
| 766 | |
| 767 TEST_F(PerfProviderCollectionParamsTest, Commands_EmptyExperiment) { | |
| 768 std::vector<RandomSelector::WeightAndValue> default_cmds = | |
| 769 internal::GetDefaultCommandsForCpu(GetCPUIdentity()); | |
| 770 std::map<std::string, std::string> params; | |
| 771 ASSERT_TRUE(variations::AssociateVariationParams( | |
| 772 "ChromeOSWideProfilingCollection", "group_name", params)); | |
| 773 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 774 "ChromeOSWideProfilingCollection", "group_name")); | |
| 775 perf_provider_.reset(new TestPerfProvider); | |
| 776 | |
| 777 EXPECT_EQ(default_cmds, perf_provider_->command_selector().odds()); | |
| 778 } | |
| 779 | |
| 780 TEST_F(PerfProviderCollectionParamsTest, Commands_InvalidValues) { | |
| 781 std::vector<RandomSelector::WeightAndValue> default_cmds = | |
| 782 internal::GetDefaultCommandsForCpu(GetCPUIdentity()); | |
| 783 std::map<std::string, std::string> params; | |
| 784 using param_t = decltype(params)::value_type; | |
| 785 // Use the "default" cpu specifier since we don't want to predict what CPU | |
| 786 // this test is running on. (CPU detection is tested above.) | |
| 787 params.insert(param_t("PerfCommand::default::0", "")); | |
|
Alexei Svitkine (slow)
2015/10/21 16:52:56
Nit: Can you avoid the "using param_t" above if yo
dhsharp
2015/10/21 18:19:04
Yeah, that's better. I didn't think the type deduc
| |
| 788 params.insert(param_t("PerfCommand::default::1", " ")); | |
| 789 params.insert(param_t("PerfCommand::default::2", " leading space")); | |
| 790 params.insert(param_t("PerfCommand::default::3", "no-spaces-or-numbers")); | |
| 791 params.insert(param_t("PerfCommand::default::4", "NaN-trailing-space ")); | |
| 792 params.insert(param_t("PerfCommand::default::5", "NaN x")); | |
| 793 params.insert(param_t("PerfCommand::default::6", "perf command")); | |
| 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 EXPECT_EQ(default_cmds, perf_provider_->command_selector().odds()); | |
| 801 } | |
| 802 | |
| 803 TEST_F(PerfProviderCollectionParamsTest, Commands_Override) { | |
| 804 using WeightAndValue = RandomSelector::WeightAndValue; | |
| 805 std::vector<RandomSelector::WeightAndValue> default_cmds = | |
| 806 internal::GetDefaultCommandsForCpu(GetCPUIdentity()); | |
| 807 std::map<std::string, std::string> params; | |
| 808 using param_t = decltype(params)::value_type; | |
| 809 // Use the "default" cpu specifier since we don't want to predict what CPU | |
| 810 // this test is running on. (CPU detection is tested above.) | |
| 811 params.insert(param_t("PerfCommand::default::0", "50 perf record foo")); | |
| 812 params.insert(param_t("PerfCommand::default::1", "25 perf record bar")); | |
| 813 params.insert(param_t("PerfCommand::default::2", "25 perf record baz")); | |
| 814 params.insert(param_t("PerfCommand::another-cpu::0", "7 perf record bar")); | |
| 815 ASSERT_TRUE(variations::AssociateVariationParams( | |
| 816 "ChromeOSWideProfilingCollection", "group_name", params)); | |
| 817 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 818 "ChromeOSWideProfilingCollection", "group_name")); | |
| 819 perf_provider_.reset(new TestPerfProvider); | |
| 820 | |
| 821 std::vector<WeightAndValue> expected_cmds; | |
| 822 expected_cmds.push_back(WeightAndValue(50.0, "perf record foo")); | |
| 823 expected_cmds.push_back(WeightAndValue(25.0, "perf record bar")); | |
| 824 expected_cmds.push_back(WeightAndValue(25.0, "perf record baz")); | |
| 825 | |
| 826 EXPECT_EQ(expected_cmds, perf_provider_->command_selector().odds()); | |
| 827 } | |
| 828 | |
| 829 TEST_F(PerfProviderCollectionParamsTest, Parameters_Override) { | |
| 830 std::map<std::string, std::string> params; | |
| 831 using param_t = decltype(params)::value_type; | |
| 832 params.insert(param_t("ProfileCollectionDurationSec", "15")); | |
| 833 params.insert(param_t("PeriodicProfilingIntervalMs", "3600000")); | |
| 834 params.insert(param_t("ResumeFromSuspend::SamplingFactor", "1")); | |
| 835 params.insert(param_t("ResumeFromSuspend::MaxDelaySec", "10")); | |
| 836 params.insert(param_t("RestoreSession::SamplingFactor", "2")); | |
| 837 params.insert(param_t("RestoreSession::MaxDelaySec", "20")); | |
| 838 ASSERT_TRUE(variations::AssociateVariationParams( | |
| 839 "ChromeOSWideProfilingCollection", "group_name", params)); | |
| 840 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 841 "ChromeOSWideProfilingCollection", "group_name")); | |
| 842 perf_provider_.reset(new TestPerfProvider); | |
| 843 | |
| 844 const auto& parsed_params = perf_provider_->collection_params(); | |
| 845 EXPECT_EQ(base::TimeDelta::FromSeconds(15), | |
| 846 parsed_params.collection_duration()); | |
| 847 EXPECT_EQ(base::TimeDelta::FromHours(1), | |
| 848 parsed_params.periodic_interval()); | |
| 849 EXPECT_EQ(1, parsed_params.resume_from_suspend().sampling_factor()); | |
| 850 EXPECT_EQ(base::TimeDelta::FromSeconds(10), | |
| 851 parsed_params.resume_from_suspend().max_collection_delay()); | |
| 852 EXPECT_EQ(2, parsed_params.restore_session().sampling_factor()); | |
| 853 EXPECT_EQ(base::TimeDelta::FromSeconds(20), | |
| 854 parsed_params.restore_session().max_collection_delay()); | |
| 855 } | |
| 856 | |
| 585 } // namespace metrics | 857 } // namespace metrics |
| OLD | NEW |